1 Replies - 4494 Views - Last Post: 11 May 2009 - 06:59 PM Rate Topic: -----

#1 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

PHP searching database posting back to same page

Posted 11 May 2009 - 06:07 PM

I am working on a script to search a mysql database, I am trying to post it back to the same page (different frame) is this even possible, or is there a better way?

here is the code for the search frame:
<html> 
	  <head> 
		<meta  http-equiv="Content-Type" content="text/html;  charset=iso-8859-1"> 
		<title>Search Catalog</title> 
	  </head> 
	  <body> 
		<h3>Search Catalog Contents</h3> 
		This will search both name and description<br>
		To see all listings type 'ALL' 
		<form  method="post" action="search.php?go"  id="searchform"> 
			  <input  type="text" name="name" >
			  <input  type="submit" name="submit" value="Search"> 
			</form> 
	  </body> 
	</html> 



and here is the code for the search.php page that I would like to display in the other frame:

	<?php 
	  if(isset($_POST['submit'])){ 
	  if(isset($_GET['go'])){ 
	  if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){ 
	  $name=$_POST['name'];  
	  $db=mysql_connect  ("localhost", "fred",  "dog") or die ('I cannot connect to the database  because: ' . mysql_error());  
	  $mydb=mysql_select_db("catalog"); 
	  if ($name=="ALL")
	  {$sql="SELECT * FROM catalog ORDER BY name";}
	  else
	  {$sql="SELECT * FROM catalog WHERE name LIKE '%" . $name .  "%' OR short_description LIKE '%" . $name ."%'"; }
	  $result=mysql_query($sql); 
?>
 <table style='width: 100%'>
 <?php
 while ($row = mysql_fetch_array($result)) {
?>
		<tr>
			<td style="width:100px"><img width="100" height="100" alt="<?php echo $row['name'] ?>"src="images/<?php echo $row['picture'] ?>" ></td>
			<td style="width: 150px"><?php echo $row['name'] ?></td>
			<td style="width: 350px"><?php echo $row['short_description'] ?></td>
			<td style="width: 70px">$<?php echo $row['price'] ?></td>
			<td style="width: 80px; font-size:small"><a style="font-size:small" href="cart.php?action=add&id=<?php echo $row['product_id'] ?>">Add to cart</a></td>
		</tr>
		
<?php
		}
	
echo '</table>';
}}}
?>
 




Am I even going in the right direction? or do I need to use something else? Any assistance is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP searching database posting back to same page

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3778
  • View blog
  • Posts: 13,687
  • Joined: 08-August 08

Re: PHP searching database posting back to same page

Posted 11 May 2009 - 06:59 PM

View Postd.buckner, on 11 May, 2009 - 09:07 PM, said:

I am working on a script to search a mysql database, I am trying to post it back to the same page (different frame) is this even possible, or is there a better way?

Of course it's possible. All you need to do is use if statements.

Pseudo code:
if(database has been searched) {
 display results here
} else {
display form
}

Doing it this way, you can have everything in one php file.

Here's an object oriented example:
<?php
class example {
	var $x1 = "Choose an item and click submit:";
	var $y1 = array("This is response 1","This is response 2","This is response 3","This is response 4");
	var $y2 = array("Item 1","Item 2","Item 3","Item 4");
	
	function Displayform() {
		echo $this->x1."<br>";
		echo '<form method="post" action='.$_SERVER['php_self'].'>';
		echo '<select name= "myform">';
		for($i=0;$i< count($this->y2); $i++) {
			echo "<option value='".$i."' >".$this->y2[$i].'</option>';
		}
		echo '</select><br>';
		echo '<input type="submit" name="Any" /></form>';
	}
	
	function Displayresult() {
		$i = (int)$_POST['myform'];
		echo "<h1>".$this->y1[$i]."</h1>";
	}
}


// Above is the class definition, below is the controlling code which 
// uses the if statement to determine what to display


$page = new example;

if(isset($_POST['myform'])) {
	$page->Displayresult();
	$page->Displayform();
} else {
	echo "<h1>Hello</h1>";
	$page->Displayform();
}
?>

This post has been edited by CTphpnwb: 11 May 2009 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1