Note that there are some explanatory texts on larger screens.

plurals
  1. POajax and php to enter multiple forms input to database
    text
    copied!<p>I have a php generated form with multiple input fields the number of which is determined by user select. I would like to use an ajax function to enter all the data to database. The problem is that I am new to ajax and not sure ho to go about it. The ajax javascript function below is a sample of what I would like to achieve and I know it is not correct. Can someone point me in the right direction. I have looked around and from what I see, Json may be a solution but I know nothing about it and reading up on it I still don't get it.</p> <p>sample ajax:</p> <pre><code>function MyFunction(){ var i = 1; var x = $('#num_to_enter').val(); while (i &lt;= x){ var name = $('#fname[i]').val(); var lname = $('#lname[i]').val(); var email = $('#Email[i]').val(); i++; } $('#SuccessDiv').html('Entering Info.&lt;img src="images/processing.gif" /&gt;'); $.ajax({url : 'process.php', type:"POST", while (i &lt;= x){ data: "fname[i]=" + name[i] + "&amp;lname[i]=" + lname[i] + "&amp;email[i]=" + email[i], i++; } success : function(data){ window.setTimeout(function() { $('#SuccessDiv').html('Info Added!'); $('#data').css("display","block"); $('#data').html(data); }, 2000); } }); return false; } </code></pre> <p>A sample of form :</p> <pre><code>&lt;?php echo "&lt;form method='post'&gt;"; $i=1; while($i &lt;= $num_to_enter){ $form_output .= "First Name: &lt;input id='fname' type='text' name='fname[$i]'&gt;&lt;br /&gt; Last Name: &lt;input id='lname' type='text' name='lname[$i]'&gt;&lt;br /&gt; Email: &lt;input id='Email' type='text' name='Email[$i]'&gt;&lt;br /&gt; $i++; } echo"&lt;input type='button' value='SUBMIT' onClick='MyFunction()'&gt;&lt;/form&gt;"; ?&gt; Then DB MySQL Sample &lt;?php while ($i &lt;= $x){ $x = $_POST['num_to_enter']; $fname = $_POST['fname[$i]']; $fname = $_POST['fname[$i]']; $fname = $_POST['email[$i]']; $sql = "INSERT INTO `mytable` (`firstname`, `lastname`, `email`) VALUES ('$fname[$i]', '$lname[$i]', '$email[$i]');"; $i++; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload