Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how to submit multiple values in a table please help me im using this code this code submit only 1 entry in database but i want to enter multiples value in database how can i do this

live demo http://jsfiddle.net/eruZC/3/

this is entry.html

      <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

  <link rel="stylesheet" type="text/css" href="http://fiddle.jshell.net/css/result-light.css">

  <style type='text/css'>

  </style>



<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function(){
    var id=1;

    $("#butsend").click(function(){
        $("#table1").append('<tr valign="top"><td width="100px">'+(id++)+'</td><td width="100px">'+$("#sname").val()+'</td><td width="100px">'+$("#age").val()+'</td><td width="100px"><a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
    });

    $("#table1").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});
});//]]>  

</script>


    </head>
    <body>
      <form action="submit.php" d="form1" name="form1" method="post">
    <label>Student Name</label><input type="text" name="sname" id="sname"></br>
    <label>Student Age</label><input type="text" name="age" id="age"></br>
    <input type="button" name="send" value="Add" id="butsend"></br>
     <input type="button" name="submit" value="Submit" ></br>
    </form>
    <table id="table1" name="table1" border="">
    <tbody>
    <tr><th width="100px">ID</th><th width="100px">Name</th><th width="100px">Age</th><th width="100px"></th><tr>

    </tbody>
    </table>

    </body>


    </html>

this is php page submit.php

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($sname, $age, $error)
 {
 ?>


 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 




 <?php 
 }




 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['age']));



 // check to make sure both fields are entered
 if ($name == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($sname, $age, $error);
 }
 else
 {
 // save the data to the database
  mysql_query("INSERT stock SET sname='$sname', age='$age'")
 or die(mysql_error()); 

 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','');
 }

         ?>
share|improve this question
 
is it true that you are actually only inserting the values of the labels? The values of the names and ages that you append in the table is not a field and not part of the form in order to get submitted, to your php code. Also take a look here it may help you stackoverflow.com/questions/10489046/… –  melc Dec 30 '13 at 10:35
add comment

3 Answers

if u want to add multiple value from single form add comma seprated value in input box and then use explode eg:

 $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
$namearray = explode(',',$sname);
foreach ($namearray as $sname){
 mysql_query("INSERT stock SET sname='$sname', age='$age'")
 or die(mysql_error()); 
}
share|improve this answer
add comment

Why don't you try the .post() jQuery function ? it's very simple to implement

$.post( "example.php", { name: "John", time: "2pm", etc, etc, etc });
   .done(function() {
          alert( "second success" );
    })
   .fail(function() {
          alert( "error" );
   })
   .always(function() {
         alert( "finished" );
   });

Then in your php, you just have to insert into mysql with a loop like a foreach :

<?php 
       foreach ( $_POST as $p ){
           mysql_query( YOUR QUERY );
       }
       return ( 'DONE !' ); // your return values
?>
share|improve this answer
add comment

You can use foreach or for loop to execute multiple insert sql query.

You should submit your form values to get those values in php code.This question is exactly related to your question. How to insert into MYSQL row from multiple $_POST arrays

use your code like this fiddle example http://jsfiddle.net/cjramki/mPR45/

I hope this link will useful to you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.