Javascript part of the code is not posted to the database





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to enter data to my database with dynamic textboxes. The dynamic textboxes are created with Javascript. When I try to send the data to my database I see that my PHP script is posting only the first row into the database



<div id="form">
<form name="reaction" id="reaction" method="post" action="./post.php">
//The first row is created in HTML
<input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
</div>

//The other rows are created in Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
var final_total_amt = $('#final_total_amt').text();
var count = 1;

$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
});

$('#form').append(html_code);
</script>

<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>


In the PHP script I am trying to post the multiple textboxes with:



foreach($_POST['firstname'] as $i => $item) {
$db3 = new PDO('mysql:host=localhost;dbname=db', 'root', 'pass');
$query3= "INSERT INTO scu_db(firstname) VALUES (:firstname)";
$stmt3 = $db3->prepare($query3);
$exec3 = $stmt3->execute(array(
':firstname' => $_POST["firstname"][$i]
));
}


The script only posts the first row that is created in HTML to my database.



Does someone know how I can solve this?



Result of var_dump on $_POST['firstname']:



array(1) { [0]=> string(2) "1" }









share|improve this question

























  • kidnly upload the foreach full code

    – Akhtar Munir
    Nov 24 '18 at 14:28











  • I updated my post with the foreach full code

    – John
    Nov 24 '18 at 14:32











  • first echo only $item if you are receiving multiple data comment your insertion for a while

    – Akhtar Munir
    Nov 24 '18 at 14:34











  • try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

    – rakesh shrestha
    Nov 24 '18 at 14:35











  • if you are receiving correct data then assigned your $item to your :firstname

    – Akhtar Munir
    Nov 24 '18 at 14:37


















1















I am trying to enter data to my database with dynamic textboxes. The dynamic textboxes are created with Javascript. When I try to send the data to my database I see that my PHP script is posting only the first row into the database



<div id="form">
<form name="reaction" id="reaction" method="post" action="./post.php">
//The first row is created in HTML
<input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
</div>

//The other rows are created in Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
var final_total_amt = $('#final_total_amt').text();
var count = 1;

$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
});

$('#form').append(html_code);
</script>

<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>


In the PHP script I am trying to post the multiple textboxes with:



foreach($_POST['firstname'] as $i => $item) {
$db3 = new PDO('mysql:host=localhost;dbname=db', 'root', 'pass');
$query3= "INSERT INTO scu_db(firstname) VALUES (:firstname)";
$stmt3 = $db3->prepare($query3);
$exec3 = $stmt3->execute(array(
':firstname' => $_POST["firstname"][$i]
));
}


The script only posts the first row that is created in HTML to my database.



Does someone know how I can solve this?



Result of var_dump on $_POST['firstname']:



array(1) { [0]=> string(2) "1" }









share|improve this question

























  • kidnly upload the foreach full code

    – Akhtar Munir
    Nov 24 '18 at 14:28











  • I updated my post with the foreach full code

    – John
    Nov 24 '18 at 14:32











  • first echo only $item if you are receiving multiple data comment your insertion for a while

    – Akhtar Munir
    Nov 24 '18 at 14:34











  • try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

    – rakesh shrestha
    Nov 24 '18 at 14:35











  • if you are receiving correct data then assigned your $item to your :firstname

    – Akhtar Munir
    Nov 24 '18 at 14:37














1












1








1


1






I am trying to enter data to my database with dynamic textboxes. The dynamic textboxes are created with Javascript. When I try to send the data to my database I see that my PHP script is posting only the first row into the database



<div id="form">
<form name="reaction" id="reaction" method="post" action="./post.php">
//The first row is created in HTML
<input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
</div>

//The other rows are created in Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
var final_total_amt = $('#final_total_amt').text();
var count = 1;

$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
});

$('#form').append(html_code);
</script>

<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>


In the PHP script I am trying to post the multiple textboxes with:



foreach($_POST['firstname'] as $i => $item) {
$db3 = new PDO('mysql:host=localhost;dbname=db', 'root', 'pass');
$query3= "INSERT INTO scu_db(firstname) VALUES (:firstname)";
$stmt3 = $db3->prepare($query3);
$exec3 = $stmt3->execute(array(
':firstname' => $_POST["firstname"][$i]
));
}


The script only posts the first row that is created in HTML to my database.



Does someone know how I can solve this?



Result of var_dump on $_POST['firstname']:



array(1) { [0]=> string(2) "1" }









share|improve this question
















I am trying to enter data to my database with dynamic textboxes. The dynamic textboxes are created with Javascript. When I try to send the data to my database I see that my PHP script is posting only the first row into the database



<div id="form">
<form name="reaction" id="reaction" method="post" action="./post.php">
//The first row is created in HTML
<input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
</div>

//The other rows are created in Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
var final_total_amt = $('#final_total_amt').text();
var count = 1;

$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
});

$('#form').append(html_code);
</script>

<button type="submit" class="btn btn-primary" name="send">Save</button>
</form>


In the PHP script I am trying to post the multiple textboxes with:



foreach($_POST['firstname'] as $i => $item) {
$db3 = new PDO('mysql:host=localhost;dbname=db', 'root', 'pass');
$query3= "INSERT INTO scu_db(firstname) VALUES (:firstname)";
$stmt3 = $db3->prepare($query3);
$exec3 = $stmt3->execute(array(
':firstname' => $_POST["firstname"][$i]
));
}


The script only posts the first row that is created in HTML to my database.



Does someone know how I can solve this?



Result of var_dump on $_POST['firstname']:



array(1) { [0]=> string(2) "1" }






javascript php html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:43







John

















asked Nov 24 '18 at 14:26









JohnJohn

802925




802925













  • kidnly upload the foreach full code

    – Akhtar Munir
    Nov 24 '18 at 14:28











  • I updated my post with the foreach full code

    – John
    Nov 24 '18 at 14:32











  • first echo only $item if you are receiving multiple data comment your insertion for a while

    – Akhtar Munir
    Nov 24 '18 at 14:34











  • try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

    – rakesh shrestha
    Nov 24 '18 at 14:35











  • if you are receiving correct data then assigned your $item to your :firstname

    – Akhtar Munir
    Nov 24 '18 at 14:37



















  • kidnly upload the foreach full code

    – Akhtar Munir
    Nov 24 '18 at 14:28











  • I updated my post with the foreach full code

    – John
    Nov 24 '18 at 14:32











  • first echo only $item if you are receiving multiple data comment your insertion for a while

    – Akhtar Munir
    Nov 24 '18 at 14:34











  • try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

    – rakesh shrestha
    Nov 24 '18 at 14:35











  • if you are receiving correct data then assigned your $item to your :firstname

    – Akhtar Munir
    Nov 24 '18 at 14:37

















kidnly upload the foreach full code

– Akhtar Munir
Nov 24 '18 at 14:28





kidnly upload the foreach full code

– Akhtar Munir
Nov 24 '18 at 14:28













I updated my post with the foreach full code

– John
Nov 24 '18 at 14:32





I updated my post with the foreach full code

– John
Nov 24 '18 at 14:32













first echo only $item if you are receiving multiple data comment your insertion for a while

– Akhtar Munir
Nov 24 '18 at 14:34





first echo only $item if you are receiving multiple data comment your insertion for a while

– Akhtar Munir
Nov 24 '18 at 14:34













try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

– rakesh shrestha
Nov 24 '18 at 14:35





try inserting with $item... .. have your dumped? $_POST? can you edit it and upload the result

– rakesh shrestha
Nov 24 '18 at 14:35













if you are receiving correct data then assigned your $item to your :firstname

– Akhtar Munir
Nov 24 '18 at 14:37





if you are receiving correct data then assigned your $item to your :firstname

– Akhtar Munir
Nov 24 '18 at 14:37












2 Answers
2






active

oldest

votes


















0





+100









Your problem was caused primarily by improperly nested HTML tags. Switch the positions of your div#form tag with your form like below



<form name="reaction" id="reaction" method="post" action="./post.php">
<div id="form">


Remember to close out your $(document).ready() method declaration, and also place your $('#form').append(html_code) code within the scope of your $(document).on('click') event.



See below for a working sample



<?php
var_dump($_POST);
?>

<form name="reaction" id="reaction" method="post" action="">
<div id="form">
//The first row is created in HTML
<input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
</div>

//The other rows are created in Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var final_total_amt = $('#final_total_amt').text();
var count = 1;

$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
$('#form').append(html_code);
});
});
</script>

<button type="submit" class="btn btn-primary" name="send">Save</button>
<button type="button" class="btn btn-primary" id="add_row">Add Row</button>
</form>





share|improve this answer
























  • It worked. Thanks

    – John
    Dec 26 '18 at 22:44



















0














@John in my case I had done it like this, it is working fine. It has two buttons to add and/or remove fields from the other fields. If you need this then I can send you both files in email.



 addNewRow();

$("#add").click(function(){
addNewRow();
})

function addNewRow(){
$.ajax({
url : DOMAIN+"/includes/process.php",
method : "POST",
data : {getNewOrderItem:1},
success : function(data){
$("#invoice_item").append(data);
var n = 0;
$(".number").each(function(){
$(this).html(++n);
})
}
})
}

$("#remove").click(function(){
$("#invoice_item").children("tr:last").remove();
})





share|improve this answer
























    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%2f53459147%2fjavascript-part-of-the-code-is-not-posted-to-the-database%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





    +100









    Your problem was caused primarily by improperly nested HTML tags. Switch the positions of your div#form tag with your form like below



    <form name="reaction" id="reaction" method="post" action="./post.php">
    <div id="form">


    Remember to close out your $(document).ready() method declaration, and also place your $('#form').append(html_code) code within the scope of your $(document).on('click') event.



    See below for a working sample



    <?php
    var_dump($_POST);
    ?>

    <form name="reaction" id="reaction" method="post" action="">
    <div id="form">
    //The first row is created in HTML
    <input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
    </div>

    //The other rows are created in Javascript
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var final_total_amt = $('#final_total_amt').text();
    var count = 1;

    $(document).on('click', '#add_row', function(){
    count++;
    $('#total_item').val(count);
    var html_code = '';
    html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
    $('#form').append(html_code);
    });
    });
    </script>

    <button type="submit" class="btn btn-primary" name="send">Save</button>
    <button type="button" class="btn btn-primary" id="add_row">Add Row</button>
    </form>





    share|improve this answer
























    • It worked. Thanks

      – John
      Dec 26 '18 at 22:44
















    0





    +100









    Your problem was caused primarily by improperly nested HTML tags. Switch the positions of your div#form tag with your form like below



    <form name="reaction" id="reaction" method="post" action="./post.php">
    <div id="form">


    Remember to close out your $(document).ready() method declaration, and also place your $('#form').append(html_code) code within the scope of your $(document).on('click') event.



    See below for a working sample



    <?php
    var_dump($_POST);
    ?>

    <form name="reaction" id="reaction" method="post" action="">
    <div id="form">
    //The first row is created in HTML
    <input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
    </div>

    //The other rows are created in Javascript
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var final_total_amt = $('#final_total_amt').text();
    var count = 1;

    $(document).on('click', '#add_row', function(){
    count++;
    $('#total_item').val(count);
    var html_code = '';
    html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
    $('#form').append(html_code);
    });
    });
    </script>

    <button type="submit" class="btn btn-primary" name="send">Save</button>
    <button type="button" class="btn btn-primary" id="add_row">Add Row</button>
    </form>





    share|improve this answer
























    • It worked. Thanks

      – John
      Dec 26 '18 at 22:44














    0





    +100







    0





    +100



    0




    +100





    Your problem was caused primarily by improperly nested HTML tags. Switch the positions of your div#form tag with your form like below



    <form name="reaction" id="reaction" method="post" action="./post.php">
    <div id="form">


    Remember to close out your $(document).ready() method declaration, and also place your $('#form').append(html_code) code within the scope of your $(document).on('click') event.



    See below for a working sample



    <?php
    var_dump($_POST);
    ?>

    <form name="reaction" id="reaction" method="post" action="">
    <div id="form">
    //The first row is created in HTML
    <input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
    </div>

    //The other rows are created in Javascript
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var final_total_amt = $('#final_total_amt').text();
    var count = 1;

    $(document).on('click', '#add_row', function(){
    count++;
    $('#total_item').val(count);
    var html_code = '';
    html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
    $('#form').append(html_code);
    });
    });
    </script>

    <button type="submit" class="btn btn-primary" name="send">Save</button>
    <button type="button" class="btn btn-primary" id="add_row">Add Row</button>
    </form>





    share|improve this answer













    Your problem was caused primarily by improperly nested HTML tags. Switch the positions of your div#form tag with your form like below



    <form name="reaction" id="reaction" method="post" action="./post.php">
    <div id="form">


    Remember to close out your $(document).ready() method declaration, and also place your $('#form').append(html_code) code within the scope of your $(document).on('click') event.



    See below for a working sample



    <?php
    var_dump($_POST);
    ?>

    <form name="reaction" id="reaction" method="post" action="">
    <div id="form">
    //The first row is created in HTML
    <input type="text" name="firstname" id="firstname1" placeholder="Firstname" /> <br />
    </div>

    //The other rows are created in Javascript
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var final_total_amt = $('#final_total_amt').text();
    var count = 1;

    $(document).on('click', '#add_row', function(){
    count++;
    $('#total_item').val(count);
    var html_code = '';
    html_code += '<input type="text" placeholder="Firstname'+count+'" name="firstname" id="firstname'+count+'" data-srno="'+count+'" /><br />';
    $('#form').append(html_code);
    });
    });
    </script>

    <button type="submit" class="btn btn-primary" name="send">Save</button>
    <button type="button" class="btn btn-primary" id="add_row">Add Row</button>
    </form>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 26 '18 at 7:18









    Solomon A.Solomon A.

    44125




    44125













    • It worked. Thanks

      – John
      Dec 26 '18 at 22:44



















    • It worked. Thanks

      – John
      Dec 26 '18 at 22:44

















    It worked. Thanks

    – John
    Dec 26 '18 at 22:44





    It worked. Thanks

    – John
    Dec 26 '18 at 22:44













    0














    @John in my case I had done it like this, it is working fine. It has two buttons to add and/or remove fields from the other fields. If you need this then I can send you both files in email.



     addNewRow();

    $("#add").click(function(){
    addNewRow();
    })

    function addNewRow(){
    $.ajax({
    url : DOMAIN+"/includes/process.php",
    method : "POST",
    data : {getNewOrderItem:1},
    success : function(data){
    $("#invoice_item").append(data);
    var n = 0;
    $(".number").each(function(){
    $(this).html(++n);
    })
    }
    })
    }

    $("#remove").click(function(){
    $("#invoice_item").children("tr:last").remove();
    })





    share|improve this answer




























      0














      @John in my case I had done it like this, it is working fine. It has two buttons to add and/or remove fields from the other fields. If you need this then I can send you both files in email.



       addNewRow();

      $("#add").click(function(){
      addNewRow();
      })

      function addNewRow(){
      $.ajax({
      url : DOMAIN+"/includes/process.php",
      method : "POST",
      data : {getNewOrderItem:1},
      success : function(data){
      $("#invoice_item").append(data);
      var n = 0;
      $(".number").each(function(){
      $(this).html(++n);
      })
      }
      })
      }

      $("#remove").click(function(){
      $("#invoice_item").children("tr:last").remove();
      })





      share|improve this answer


























        0












        0








        0







        @John in my case I had done it like this, it is working fine. It has two buttons to add and/or remove fields from the other fields. If you need this then I can send you both files in email.



         addNewRow();

        $("#add").click(function(){
        addNewRow();
        })

        function addNewRow(){
        $.ajax({
        url : DOMAIN+"/includes/process.php",
        method : "POST",
        data : {getNewOrderItem:1},
        success : function(data){
        $("#invoice_item").append(data);
        var n = 0;
        $(".number").each(function(){
        $(this).html(++n);
        })
        }
        })
        }

        $("#remove").click(function(){
        $("#invoice_item").children("tr:last").remove();
        })





        share|improve this answer













        @John in my case I had done it like this, it is working fine. It has two buttons to add and/or remove fields from the other fields. If you need this then I can send you both files in email.



         addNewRow();

        $("#add").click(function(){
        addNewRow();
        })

        function addNewRow(){
        $.ajax({
        url : DOMAIN+"/includes/process.php",
        method : "POST",
        data : {getNewOrderItem:1},
        success : function(data){
        $("#invoice_item").append(data);
        var n = 0;
        $(".number").each(function(){
        $(this).html(++n);
        })
        }
        })
        }

        $("#remove").click(function(){
        $("#invoice_item").children("tr:last").remove();
        })






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 16:40









        Akhtar MunirAkhtar Munir

        6612




        6612






























            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%2f53459147%2fjavascript-part-of-the-code-is-not-posted-to-the-database%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings