Can someone look at this and tell my why it's not working like it should? It gets the data from the database fine, just wont post to search.php

<?php

 
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root",""); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("test1") or die("Unable to select database"); //select which database we're using


$sql="SELECT LastName, FirstName FROM persons";
$result=mysql_query($sql);

$optionsfname="";
$optionslname="";

while ($row=mysql_fetch_array($result)) {

    $id=$row["ID"];
    $fname=$row["FirstName"];
	$lname=$row["LastName"];
   $optionsfname.="<OPTION VALUE=\"$id\">".$fname.'</option>';
   $optionslname.="<OPTION VALUE=\"$id\">".$lname.'</option>';

}

?>


<form action="search.php" method="GET">

<select name="name">
<OPTION VALUE=0>First Name
<?=$optionsfname?>
</SELECT>
<input type="Submit" value="Search"/>
</form>

search.php

<?php

  // Get the search variable from URL
  $varfname = @$_GET['fname'] ;
  $varname = @$_GET['name'] ;
  $trimmedfname = trim($varfname);
  $trimmedname = trim($varname); //trim whitespace from the stored variable
// rows to return
$limit=50; 

// check for an empty name and display a message.
if ($trimmedname  == "") 
  {
  echo "<p>A last name is required...</p>";
  exit;
  }

When I click submit it gives me the message A last name is required.

Recommended Answers

All 3 Replies

try this you for got the "l" in front of lnames

<?php

  // Get the search variable from URL
  $varfname = @$_GET['fname'] ;
  $varlname = @$_GET['lname'] ;
  $trimmedfname = trim($varfname);
  $trimmedname = trim($varlname); //trim whitespace from the stored variable
// rows to return
$limit=50; 

// check for an empty name and display a message.
if ($trimmedname  == "") 
  {
  echo "<p>A last name is required...</p>";
  exit;
  }

try this you for got the "l" in front of lnames

<?php

  // Get the search variable from URL
  $varfname = @$_GET['fname'] ;
  $varlname = @$_GET['lname'] ;
  $trimmedfname = trim($varfname);
  $trimmedname = trim($varlname); //trim whitespace from the stored variable
// rows to return
$limit=50; 

// check for an empty name and display a message.
if ($trimmedname  == "") 
  {
  echo "<p>A last name is required...</p>";
  exit;
  }

Thanks for your reply! Again!

I intentionally did that, varname and trimmedname = last name. Only last name is required. I added the first name option later on.

Is the form inside the form tags correct?

this select name inside the form is equal to 'name'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.