Jump to content

posting form data to php file


doforumda

Recommended Posts

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

<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.

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>

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();
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.