Need to submit form multiple times with javascript












0















As the title says I need to submit a form multiple times, form's action is an external php file. The form submits once but I need it to submit once with each loop.



   function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
var receivers_array = receivers.split(',');
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
document.getElementById("smsForm").submit();


}
}


What the code is basically supposed to do is split up phone numbers that the user enters and send a text message to each of the numbers. It submits and a text is sent to the first number in the array but not the other entered numbers. Spliting and everything works(I've used console.log on everything to check). The loop isn't interrupted either because it console logged each receiver value after assigning it inside the loop.



UPDATE: Got it working like this



  function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
console.log(receivers);
var receivers_array = receivers.split(',');
console.log(receivers_array[0]);
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
console.log(document.getElementById('receiver').value);
//document.getElementById("smsForm").submit();
$.ajax({
url:'../API/sendsms.php',
type:'post',
data:$('#smsForm').serialize(),
success:function(){
alert("worked");
}
});


}
}









share|improve this question

























  • try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

    – joyBlanks
    Nov 26 '18 at 10:01
















0















As the title says I need to submit a form multiple times, form's action is an external php file. The form submits once but I need it to submit once with each loop.



   function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
var receivers_array = receivers.split(',');
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
document.getElementById("smsForm").submit();


}
}


What the code is basically supposed to do is split up phone numbers that the user enters and send a text message to each of the numbers. It submits and a text is sent to the first number in the array but not the other entered numbers. Spliting and everything works(I've used console.log on everything to check). The loop isn't interrupted either because it console logged each receiver value after assigning it inside the loop.



UPDATE: Got it working like this



  function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
console.log(receivers);
var receivers_array = receivers.split(',');
console.log(receivers_array[0]);
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
console.log(document.getElementById('receiver').value);
//document.getElementById("smsForm").submit();
$.ajax({
url:'../API/sendsms.php',
type:'post',
data:$('#smsForm').serialize(),
success:function(){
alert("worked");
}
});


}
}









share|improve this question

























  • try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

    – joyBlanks
    Nov 26 '18 at 10:01














0












0








0








As the title says I need to submit a form multiple times, form's action is an external php file. The form submits once but I need it to submit once with each loop.



   function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
var receivers_array = receivers.split(',');
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
document.getElementById("smsForm").submit();


}
}


What the code is basically supposed to do is split up phone numbers that the user enters and send a text message to each of the numbers. It submits and a text is sent to the first number in the array but not the other entered numbers. Spliting and everything works(I've used console.log on everything to check). The loop isn't interrupted either because it console logged each receiver value after assigning it inside the loop.



UPDATE: Got it working like this



  function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
console.log(receivers);
var receivers_array = receivers.split(',');
console.log(receivers_array[0]);
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
console.log(document.getElementById('receiver').value);
//document.getElementById("smsForm").submit();
$.ajax({
url:'../API/sendsms.php',
type:'post',
data:$('#smsForm').serialize(),
success:function(){
alert("worked");
}
});


}
}









share|improve this question
















As the title says I need to submit a form multiple times, form's action is an external php file. The form submits once but I need it to submit once with each loop.



   function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
var receivers_array = receivers.split(',');
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
document.getElementById("smsForm").submit();


}
}


What the code is basically supposed to do is split up phone numbers that the user enters and send a text message to each of the numbers. It submits and a text is sent to the first number in the array but not the other entered numbers. Spliting and everything works(I've used console.log on everything to check). The loop isn't interrupted either because it console logged each receiver value after assigning it inside the loop.



UPDATE: Got it working like this



  function send_sms(){
var receivers = document.getElementById('receivers').value.toString();
console.log(receivers);
var receivers_array = receivers.split(',');
console.log(receivers_array[0]);
for(var i = 0; i < receivers_array.length; i++) {
// Trim the excess whitespace.
receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
document.getElementById('receiver').value = receivers_array[i];
console.log(document.getElementById('receiver').value);
//document.getElementById("smsForm").submit();
$.ajax({
url:'../API/sendsms.php',
type:'post',
data:$('#smsForm').serialize(),
success:function(){
alert("worked");
}
});


}
}






javascript php forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 10:48







tillinips12

















asked Nov 26 '18 at 9:58









tillinips12tillinips12

386




386













  • try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

    – joyBlanks
    Nov 26 '18 at 10:01



















  • try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

    – joyBlanks
    Nov 26 '18 at 10:01

















try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

– joyBlanks
Nov 26 '18 at 10:01





try ajax. While submit reroutes to php all pending js will be halted. Better do it with ajax. Mind the async logic and all :)

– joyBlanks
Nov 26 '18 at 10:01












2 Answers
2






active

oldest

votes


















0














You can try collect all numbers and send them in one request, after this parse them on backend side and send sms






share|improve this answer































    0














    Submit triggers page reload/redirect to your action. You have to add ajax listener for your form submit so it won't refresh the page every time the submit triggers.






    $(document).on('submit', 'form#smsForm', function() {
    $.ajax({
    url: $(this).attr('action'),
    type: $(this).attr('method'),
    dataType: 'json',
    data: $(this).serialize(),
    success: function(data) {
    console.log('Submitted');
    },
    error: function(xhr, err) {
    console.log('Error');
    }
    });
    return false;
    });

    function send_sms() {
    var receivers = document.getElementById('receivers').value.toString();
    var receivers_array = receivers.split(',');
    for (var i = 0; i < receivers_array.length; i++) {
    // Trim the excess whitespace.
    receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
    document.getElementById('receiver').value = receivers_array[i];
    document.getElementById("smsForm").submit();


    }
    }








    share|improve this answer
























    • This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

      – tillinips12
      Nov 26 '18 at 10:22













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478615%2fneed-to-submit-form-multiple-times-with-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can try collect all numbers and send them in one request, after this parse them on backend side and send sms






    share|improve this answer




























      0














      You can try collect all numbers and send them in one request, after this parse them on backend side and send sms






      share|improve this answer


























        0












        0








        0







        You can try collect all numbers and send them in one request, after this parse them on backend side and send sms






        share|improve this answer













        You can try collect all numbers and send them in one request, after this parse them on backend side and send sms







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 10:04









        Alexander KarpAlexander Karp

        278




        278

























            0














            Submit triggers page reload/redirect to your action. You have to add ajax listener for your form submit so it won't refresh the page every time the submit triggers.






            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }








            share|improve this answer
























            • This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

              – tillinips12
              Nov 26 '18 at 10:22


















            0














            Submit triggers page reload/redirect to your action. You have to add ajax listener for your form submit so it won't refresh the page every time the submit triggers.






            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }








            share|improve this answer
























            • This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

              – tillinips12
              Nov 26 '18 at 10:22
















            0












            0








            0







            Submit triggers page reload/redirect to your action. You have to add ajax listener for your form submit so it won't refresh the page every time the submit triggers.






            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }








            share|improve this answer













            Submit triggers page reload/redirect to your action. You have to add ajax listener for your form submit so it won't refresh the page every time the submit triggers.






            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }








            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }





            $(document).on('submit', 'form#smsForm', function() {
            $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            dataType: 'json',
            data: $(this).serialize(),
            success: function(data) {
            console.log('Submitted');
            },
            error: function(xhr, err) {
            console.log('Error');
            }
            });
            return false;
            });

            function send_sms() {
            var receivers = document.getElementById('receivers').value.toString();
            var receivers_array = receivers.split(',');
            for (var i = 0; i < receivers_array.length; i++) {
            // Trim the excess whitespace.
            receivers_array[i] = receivers_array[i].replace(/^s*/, "").replace(/s*$/, "");
            document.getElementById('receiver').value = receivers_array[i];
            document.getElementById("smsForm").submit();


            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 10:05









            ACDACD

            8941112




            8941112













            • This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

              – tillinips12
              Nov 26 '18 at 10:22





















            • This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

              – tillinips12
              Nov 26 '18 at 10:22



















            This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

            – tillinips12
            Nov 26 '18 at 10:22







            This solution seems like what I'm looking for but it doesn't seem to work unfortunately, nothing gets logged in the console either. Still only submits the first number.

            – tillinips12
            Nov 26 '18 at 10:22




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478615%2fneed-to-submit-form-multiple-times-with-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks