doforumda Posted February 6, 2010 Share Posted February 6, 2010 hi i create a dynamic form using jquery now i want to post all the data to php file. how can i post data to php file. here is my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script> <script> var id = 1; function addFormField() { //document.getElementById("id").value; id++; $("#divTxt").append( "<p id='row" + id + "'><label for='txt" + id + "'>Relation " + id + " <select name='txt[]' id='txt" + id + "'><option value='b'>Brother</option><option value='s'>Sister</option><option value='f'>Father</option><option value='m'>Mother</option></select> <a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Remove</a></p>"); } function removeFormField(id) { $(id).remove(); } </script> </head> <body> <p><a href="#" onClick="addFormField(); return false;">Add</a></p> <form action="#" method="post" id="form1"> <select name="select" id="select"> <option value="b">Brother</option> <option value="s">Sister</option> <option value="m">Mother</option> <option value="f">Father</option> <option value="d">Daughter</option> <option value="s">Son</option> </select> <div id="divTxt"></div> <p><input type="submit" value="Submit" name="submit"> <input type="reset" value="Reset" name="reset"></p> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/ Share on other sites More sharing options...
trq Posted February 6, 2010 Share Posted February 6, 2010 <form action="phpscript.php" method="post" id="form1"> Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007765 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 <form action="phpscript.php" method="post" id="form1"> i know that but how can i get these data on php file to display it. actually i want to add that data into database. For learning purpose i need you show me how can i get this data on php file and display it. Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007766 Share on other sites More sharing options...
trq Posted February 6, 2010 Share Posted February 6, 2010 You would need to loop through the $_POST['txt'] array. Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007767 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 can you please give me example how can i do this here? Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007770 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 You would need to loop through the $_POST['txt'] array. can you please give me example how can i do this here? Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007774 Share on other sites More sharing options...
trq Posted February 6, 2010 Share Posted February 6, 2010 foreach ($_POST['txt'] as $key => $val) { echo "$key was submitted with a value of $val<br />"; } Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007776 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 foreach ($_POST['txt'] as $key => $val) { echo "$key was submitted with a value of $val<br />"; } thanks for your reply. This code is working but now i make some changes but due to these changes my code doesnt work. Now i add extra textfield and on php side i am trying to add this data to db. while doing this only the last data gets inserted in db. how can i make this code to insert all the data? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script> <script> var id = 0; function addFormField() { //document.getElementById("id").value; id++; $("#divTxt").append( "<p id='row" + id + "'><label for='txt" + id + "'>Relation<select name='relation[]' id='relation" + id + "'><option value='brother'>Brother</option><option value='sister'>Sister</option><option value='father'>Father</option><option value='mother'>Mother</option></select> <input type='text' name='name[]' /> <a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Remove</a></p>" ); } function removeFormField(id) { $(id).remove(); } </script> </head> <body> <form action="addDynamicFields.php" method="post" id="form1"> <label>Relation</label><select name="relation[]" id="relation0"> <option value="brother">Brother</option> <option value="sister">Sister</option> <option value="mother">Mother</option> <option value="father">Father</option> <option value="daughter">Daughter</option> <option value="son">Son</option> </select> <input type="text" name="name[]" /> <div id="divTxt"></div> <p><input type="submit" value="Submit" name="submit"> <!-- <input type="reset" value="Reset" name="reset"> --></p> </form> <p><a href="#" onClick="addFormField(); return false;">Add</a></p> </body> </html> php file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $relation = $_POST['relation']; $name = $_POST['name']; foreach ($relation as $key => $val) { $relationship = $val; echo $relationship."<br>"; //echo "$key was submitted with a value of $val<br />"; } foreach ($name as $key => $val) { $relativename = $val; echo $relativename."<br>"; //echo "$key was submitted with a value of $val<br />"; } $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); $query = mysql_query("INSERT INTO test VALUES ('','$relationship','$relativename')"); echo "Data added."; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007794 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 I am still waiting please help me Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007818 Share on other sites More sharing options...
wildteen88 Posted February 6, 2010 Share Posted February 6, 2010 It is because you're overwriting your variables here foreach ($relation as $key => $val) { $relationship = $val; echo $relationship."<br>"; //echo "$key was submitted with a value of $val<br />"; } foreach ($name as $key => $val) { $relativename = $val; echo $relativename."<br>"; //echo "$key was submitted with a value of $val<br />"; } You'll want to merge the two loops together and run your insert query on each iteration of the loop. A bit like this $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); $insertDataset = array(); $relation = $_POST['relation']; $name = $_POST['name']; foreach ($relation as $key => $relationship) { $insertDataset[] = sprintf("('%s', '%s')", $relationship, $name[$key]); echo '<p>Name: ' . $name[$key] . '<br />Relationship: ' . $relationship . '</p>'; } $query = 'INSERT INTO test (relationship, name) VALUES ' . implode(",\n", $insertDataset); echo "<p>Generated Query: <pre>$query</pre>"; $result = mysql_query($query); if($result) { echo 'Added '. mysql_affected_rows() . ' new records!'; } else { echo 'Oops there is an error!<br />'.mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007851 Share on other sites More sharing options...
doforumda Posted February 6, 2010 Author Share Posted February 6, 2010 i am trying to execute query but it is not inserting data into db $exe = mysql_query($query); Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1007883 Share on other sites More sharing options...
doforumda Posted February 7, 2010 Author Share Posted February 7, 2010 thanks for helping me but what if i want to use more than two input fields in html file. than how can i handle all of them. any idea or practical example would be more appreciated. Link to comment https://forums.phpfreaks.com/topic/191122-posting-form-data-to-php-file/#findComment-1008181 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.