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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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