0

I'm a php noob and I have a feeling that there is a simple solution to my question.

I have a php mysql table search that returns a result sucessfully. What I would like to be able to do is select one of the results to be inserted into a form. Basically when filling out a form there is a hyper link next to one of the inserts that opens a new window that has a search. I would like the user to be able to select one of the results from the search and have it automatically fill in the form item. Here is the code for the search (database info omitted).

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['search'])){
  $search=$_POST['search'];
  
  $db=mysql_connect  ("", "",  "") or die ('I cannot connect to the database  because: ' . mysql_error());
 
  $mydb=mysql_select_db("");
  
  $sql="SELECT CompanyID, ZipCat FROM A WHERE ZipCat LIKE '%" . $search .  "%'";
  
  $result=mysql_query($sql);

echo "<table border='1'>  
<tr>
<th>Company ID</th>
<th>ZipCat</th>
</tr>";  

  while($row=mysql_fetch_array($result)){
       echo "<tr>";
  echo "<td>" . $row['CompanyID'] . "</td>";
  echo "<td>" . $row['ZipCat'] . "</td>";
  echo "</tr>";    
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
  }
?>

The ZipCat is the section of the form that I would like to insert the search result (ZipCat) into. Any help would be appreciated!

4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by ptara1
0

Do you mean: how can you attribute values to form elements before the form is delivered to the user?
Basically, your hyperlink should contain the ZipCat element as a get parameter to the php script which contains the form. Example:
Change echo "<td>" . $row['ZipCat'] . "</td>"; to echo "<td><a href='myscript.php?Zipcat=" . $row['ZipCat'] . "'>" . $row['ZipCat'] . "</td>"; and in myscript.php use the value of $_GET to fill a form field: echo "<input type='text' name='ZipCat' value='" . $_GET['ZipCat'] . "'/>";

0

This is an example of what I think you want to. I have a database, mm, which contains a table, try. The try table has the fields id and age. I also have two pages you.php and me.php. On the you.php page, all the id's are pulled from the try table and listed on the page with a link(read more) for each id. When the user clicks on the link, he/she is taken to the me.php page where the id and the corresponding age are displayed. Here are the codes.

you.php

<?php
	$con = mysql_pconnect("localhost", "root", "password") or die(mysql_error());
	mysql_select_db("mm") or die(mysql_error());
	$query = "SELECT id FROM try";
	$result = mysql_query($query) or die(mysql_error());
	echo "<table align = 'center' cellpadding = '4' cellspacing = '4'>";
		echo "<tr>";
			echo "<td>ID</td>";
			echo "<td>OPERATION</td>";
		echo "</tr>";
		while($record = mysql_fetch_object($result))
		{
			echo "<tr>";
				echo "<td>".$record->id."</td>";
				echo "<td><a href = 'me.php?id=".urlencode($record->id)."'>read more</a></td>";
			echo "</tr>";
		}
	echo "</table>";
?>

me.php

<?php
	$con = mysql_pconnect("localhost", "root", "password") or die(mysql_error());
	mysql_select_db("mm") or die(mysql_error());
	$query = "SELECT * FROM try WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
	$result = mysql_query($query) or die(mysql_error());
	$record = mysql_fetch_object($result);
	
	echo "<table align = 'center' cellpadding = '4' cellspacing = '4'>";
		echo "<tr>";
			echo "<td colspan = '2'><b>Details of ".$record->id."</b></td>";
		echo "</tr>";
		echo "<tr><td>ID : </td><td>".$record->id."</td></tr>";
		echo "<tr><td>AGE : </td><td>".$record->age." yr(s)</td></tr>";
		echo "</tr>";
	echo "</table>";
?>

I don't want to edit your code, I want you to understand this code and use it to make your code work for.

0

Slightly off topic, you should clean the contents of $_POST before using it in the SQL query as $search=$_POST represents a security risk

0

Thanks for your reply. I'm getting closer.

The user is filling out the form. They need to look up the zip cat or enter it manually into the form element. I have an html form that they are filling out: aworkorder.html. If they need to look it up they click on a hyperlink that opens a new window with the ZipCat search, searchzc.html. This is the the updated code for searchzc.php taking into account your changes:

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['search'])){
  $search=$_POST['search'];
  
  $db=mysql_connect  ("", "",  "") or die ('I cannot connect to the database  because: ' . mysql_error());
 
  $mydb=mysql_select_db("");
  
  $sql="SELECT CompanyID, ZipCat FROM A WHERE ZipCat LIKE '%" . $search .  "%'";
  
  $result=mysql_query($sql);

echo "<table border='1'>  
<tr>
<th>Company ID</th>
<th>ZipCat</th>
</tr>";  

  while($row=mysql_fetch_array($result)){
       echo "<tr>";
  echo "<td>" . $row['CompanyID'] . "</td>";
  echo "<td><a href='insertzipcat.php?ZipCat=" . $row['ZipCat'] . "'>" . $row['ZipCat'] . "</td>";
  echo "</tr>";    
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
  }
?>

I put in "insertzipcat.php with the idea that that script would enter that ZipCat into the form element on aworkorder.html. However I have no idea how I should go about writing insertzipcat.php. Does that work or do I need to change aworkorder.html to a php script?
Currently it isn't so would it work if I just put php tags around the $_GET ?

Do you mean: how can you attribute values to form elements before the form is delivered to the user?
Basically, your hyperlink should contain the ZipCat element as a get parameter to the php script which contains the form. Example:
Change echo "<td>" . $row['ZipCat'] . "</td>"; to echo "<td><a href='myscript.php?Zipcat=" . $row['ZipCat'] . "'>" . $row['ZipCat'] . "</td>"; and in myscript.php use the value of $_GET to fill a form field: echo "<input type='text' name='ZipCat' value='" . $_GET['ZipCat'] . "'/>";

0

You do not "insert" a value into a form. A form is an HTML structure which is delivered to the browser. You build it using HTML and PHP. You use PHP for setting the dynamic parts of the form, in this case the "value" attribute.
Like in <form action="POST"><input type='text' name='ZipCat' value='<?php echo $_GET['ZipCat'];?>'/></form>

0

Solved. 2 for 2 smantscheff

However with the html form

You do not "insert" a value into a form. A form is an HTML structure which is delivered to the browser. You build it using HTML and PHP. You use PHP for setting the dynamic parts of the form, in this case the "value" attribute.
Like in <form action="POST"><input type='text' name='ZipCat' value='<?php echo $_GET['ZipCat'];?>'/></form>

0

Sorry a little premature. I did it your way and got the zipcat to be inserted into a new form. Is there anyway to get it inserted back into the original, the one that you initiated the search from? I believe I'll have to change something in this section of code.

while($row=mysql_fetch_array($result)){
       echo "<tr>";
  echo "<td>" . $row['CompanyID'] . "</td>";
  echo "<td><a href='aworkorder.php?ZipCat=" . $row['ZipCat'] . "'>" . $row['ZipCat'] . "</td>";
  echo "</tr>";    
  }

You do not "insert" a value into a form. A form is an HTML structure which is delivered to the browser. You build it using HTML and PHP. You use PHP for setting the dynamic parts of the form, in this case the "value" attribute.
Like in <form action="POST"><input type='text' name='ZipCat' value='<?php echo $_GET['ZipCat'];?>'/></form>

0

You are confusing a lot of terms.
You don't insert data into a form, you set instead the values of some form input elements.
Your "original" is not a form element, but a hyperlink. What do you want to change it to?
Do you want a change in the "original" without reloading it? Then you'd have to use JavaScript.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.