Page 1 of 1

Getting Retrieved Data To Post

Posted: Tue Jan 04, 2011 2:12 pm
by pseudz
Hello

I know I am new but my regular forum couldn't help me.

I am having a bit of trouble in working this out. I have two drop down boxes which the user can choose a selection from either one. Once a selection is made, just under the drop downs a small table will show with the appropriate data. This is so the user can confirm the selection.

After that there are a few other fields to fill in and then the user submits the form and the data is stored in a database. All of that is fine, except I can't get the data in the table from the selection to post as well.

I did find a script online so not too sure what to do about it.
Any help would be greatly appreciated.

This is just a sample of what I want to achieve
index.php

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>
<body>
<form name="testform" method='POST' action='mainck.php' onSubmit="return checkdate(this.mydate)">
  <p>Person:
    <select name="users" onChange="showUser(this.value)">
      <option value="">Select a person:</option>
      <option value="1">Peter Griffin</option>
      <option value="2">Lois Griffin</option>
    </select>
    <div id="txtHint">Person info will be listed here.</div>
    <br>
    Date:
  <input type="text" name="mydate" />
  <br>
  <input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" />
  </p>
</form>
</body>
</html>
The php page
getuser.php

Code: Select all

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'name', 'pass');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
I was wondering if the php code was on the same page and in the form it will post?

Thanks in advance

Re: Getting Retrieved Data To Post

Posted: Tue Jan 04, 2011 4:14 pm
by social_experiment
pseudz wrote:All of that is fine, except I can't get the data in the table from the selection to post as well.
Your script contains no INSERT query. Is this code that you pasted all there is to it?

Re: Getting Retrieved Data To Post

Posted: Wed Jan 05, 2011 2:43 am
by pseudz
I have my insert code on another page. The form posts to mainck.php. I know how to insert things, I just want the values from the table that appears after the drop down to post as well, which I can't seem to get right.

Code: Select all

<?php

        
     	$host="localhost"; 
        $username="name";
        $password="pass";
        $db_name="db";
        $tbl_name="tbls";
        
       mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
        mysql_select_db("$db_name")or die("cannot select DB");
        
        $Firstname=$_POST['FirstName'];
        $mydate=$_POST['mydate'];
	
        $sql="INSERT INTO $tbl_name(FirstName, mydate)VALUES('$FirstName', '$mydate')";
        $result=mysql_query($sql);
        
        if($result){
        echo "Successful";
        echo "<BR>";
        }
        
        else {
        echo "ERROR";
        }
        
        mysql_close();
?>

Re: Getting Retrieved Data To Post

Posted: Wed Jan 05, 2011 4:37 am
by social_experiment
pseudz wrote:All of that is fine, except I can't get the data in the table from the selection to post as well.
Ok, my bad for misreading. From your question you want this information (below) to be submitted to the database

Code: Select all

<?php
echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
?>
Use a form with hidden fields to pass the information along.

Code: Select all

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'name', 'pass');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);
echo '<form action="yourpage.here" method="post" >
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  // place all your rows in hidden fields
  echo '<input type="hidden" name="firstName" value="'. $row['FirstName'] .'" />
  echo '<input type="hidden" name="lastName" value="'. $row['LastName'] .'" />
  // etc
   }
echo '<input type="submit" name="submit" value="Submit" />';

echo "</table>";

mysql_close($con);
?>

Re: Getting Retrieved Data To Post

Posted: Wed Jan 05, 2011 12:54 pm
by pseudz
Ah thank you very much. I don't know why I didn't think of that.

Just one thing though, when I un-hide the input boxes and they show with the table on the original page it doesn't display the FirstName, LastName ect in the text boxes.
This is on the index page along side the the table that displays after the drop down selection has been made.