Jump to content

Form Handling with php


coder1

Recommended Posts

I have 2 forms, in 2 different pages. when user first enters the site, they get a search page (search.php), which is a form with various droplist and few radio buttons. when they hit submit, they are directed to searchresult.php, which displays of course the search result (I have page with 3 column layout), the left column, search result is displayed and the right column has the form same as the 1 in search.php page with perivously selected values. when i m in the searchresult.php, i want change something on form (perhaps selcet something different) and hit submit, i don't get any results, even though there are results to be returned.

 

on the searchresult.php am post to variables to build my query string. I have seen one example used on the rightmove website, an trying use same technique.

 


$_POST['test;

 

 

Please, any help or advise will be much apperciated.

 

 

Link to comment
Share on other sites

on the searchresult.php I have the following:

 

I build the query string like this from post. and form, its just normal html form, in both forms the input types are named the same. I get any errors as such on the second page when submit, no rows are returned from db.

 

$searchquery ='select agency, address1, address2, address3, pobox, telephone, location, bedrooms, `type`, id, price, userid, shortdescription, photo1, country, sale, id from mytesttable where currency=\''.$lclcurrency.'\' and `location` like \'%%';

$searchquery =$searchquery .' and `type` = \''.$_POST["ptype"].'\'';

Link to comment
Share on other sites

Thanks for that advise, but it did'nt work.

 

here's example of my form.

 

search.php (the first one.)

 

<BODY>
  <FORM METHOD="POST" ACTION="result.php">
<TABLE>
<TR>
	<TD>Label1</TD>
	<TD>
	<SELECT NAME="Label1">
		<OPTION VALUE="Label1">Label1</OPTION>
	</SELECT>

	</TD>
</TR>
<TR>
	<TD>Label2</TD>
	<TD>
		<SELECT NAME="Label2">
			<OPTION VALUE="Label2">Label2</label>
		</SELECT>

	</TD>
</TR>
<TR>
	<TD>Label3</TD>
	<TD>
		<SELECT NAME="Label3">
			<OPTION VALUE="Label3">Label3<OPTION VALUE="">
		</SELECT>

	</TD>
</TR>
<TR>
	<TD>label4</TD>
	<TD>
		<SELECT NAME="">
			<OPTION VALUE="label4">label4</option>
		</SELECT>
	</TD>
</TR>
<TR>
	<TD>radio</TD>
	<TD><INPUT TYPE="radio" NAME="radio" id="radio"></TD>
</TR>
<TR>
	<TD>radio1</TD>
	<TD><INPUT TYPE="radio" NAME="radio1" id="radio1"></TD>
</TR>
</TABLE>
<INPUT TYPE="submit">
  </FORM>

 

 

in the result.php I have following.

 

<div id="header">header</div>

<div id="leftcol">
$searchstring='select *  from testtable where currency=\''.$lclcurrency.'\' and `location` like \'%%';

if ($_POST["lable1"]!=""){
   $searchstring=$searchstring.$_POST["lable1"];
}

if ($_POST["lable2"]!=""){
   $searchstring=$searchstring.$_POST["lable2"];
}


if ($_POST["lable3"]!=""){
   $searchstring=$searchstring.$_POST["lable3"];
}

am doing the above for the form fields.

//executing the query.
$query_Recordset3 = $searchstring;
$Recordset3 = mysql_query($query_Recordset3, $mydb) or die(mysql_error());
$nume = mysql_num_rows($Recordset3);

the above does work, as first time i come from search.php, it bring resultset.


//Paging stuff.(basic, display 10 records per page)
$page_name="result.php"; //  If you use this code with a different page ( or file ) name then change this 

if(!isset($start)) {  // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0); 
$limit = 10;            // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 


$query= $searchstring." limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();


if($nume == '0'){

echo '<b>No data found</b>';

}else{
    display result set.

}

//below is the paging navigation.

if($nume > $limit ){ // Let us display bottom links if sufficient records are there for paging

/////////////// Start the bottom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) {
print "<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>";
}
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}


echo "</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume) {
print "<a href='$page_name?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td></tr></table>";

}// end of if checking sufficient records are there to display bottom navigational link.

</div>

<div id="right>
<!--the same form as the 1 in search.php-->
  <FORM METHOD="POST" ACTION="result.php">
<TABLE>
<TR>
	<TD>Label1</TD>
	<TD>
	<SELECT NAME="Label1">
		<OPTION VALUE="Label1">Label1</OPTION>
	</SELECT>

	</TD>
</TR>
<TR>
	<TD>Label2</TD>
	<TD>
		<SELECT NAME="Label2">
			<OPTION VALUE="Label2">Label2</label>
		</SELECT>

	</TD>
</TR>
<TR>
	<TD>Label3</TD>
	<TD>
		<SELECT NAME="Label3">
			<OPTION VALUE="Label3">Label3<OPTION VALUE="">
		</SELECT>

	</TD>
</TR>
<TR>
	<TD>label4</TD>
	<TD>
		<SELECT NAME="">
			<OPTION VALUE="label4">label4</option>
		</SELECT>
	</TD>
</TR>
<TR>
	<TD>radio</TD>
	<TD><INPUT TYPE="radio" NAME="radio" id="radio"></TD>
</TR>
<TR>
	<TD>radio1</TD>
	<TD><INPUT TYPE="radio" NAME="radio1" id="radio1"></TD>
</TR>
</TABLE>
<INPUT TYPE="submit">
  </FORM>

</div>

 

 

So basically on the result.php on the right column i have the samee form, I echo out the stuff selected from the pervious page. if the form is submitted from search.php, it all works well. however if i submit the form on the result.php. I get no data found, even though there is data.

Link to comment
Share on other sites

Well the first things I see, is why are you using id for the radios... shouldn't you be setting the value not the id...

Second... I personally would use an if/else statement to transfer the $_POST[''] variables to their own variables, and it would be better that way so you can at least somewhat make sure your not going go get sql injections and xss and such (i.e. $Lable1 = stripslashes(strip_tags($_POST['label1'])); ) Then make your query variable dependent.... (i.e. $query = "select * from testtable where currency='$what' label1='$label1'"; [Also you have an extra space after your * in your query, could be a problem also with useing the double quotes to start your string "" you do not have to use \' in order to use a variable just use single quote varaible single quote '$var' ])

 

Also your result.php or which ever page has the code... I see no begining or ending php tags That could be why your code doesn't work right...

Link to comment
Share on other sites

burn1337,

 

thank you for looking into my post, firstly I wanted  get it working as is, I will be using stored procedure, hence I did'nt bother with saving myself from sql injection (this just me playing around, see if i can get it working first), secondly i only sent segment of my code that am having problem with  i forgot to add the php tags.

 

Most importantly, perhaps I have'nt explained myself properly.

 

1) I have 2 search form that are identical - 1 page called search.php which is a basic html form.

which when submitted goes to a page called result.php (which displays result of query based on the search critera)

 

in result.php (directed from search.php) everything is fine, it (result.php) executes the query based on the query string and results are displayed. I looked at the results, now I want to change my serach citeria, so instead of hitting the back button, I put the same form (as the one in serach.php) inside the result.php, I hit submit, I get nothing.

 

I wanted know to why? what am I doing wrong? I know it possible submit a form to it self and and do what whatever processing. If I don't use the form on the result.php, but instead go back and cahnge my serach citeria on (search.php) again every displays perfectly on result.php.

 

 

Anyways I m not sure if I have explained myself properly. I would really apperciate if anyone help. I saw an expamle of this rightmove website, and I wanted implement it something similer.

 

Many thanks.

 

 

 

Link to comment
Share on other sites

if nothing else, your <OPTION>s are screwed up in spots. i'd fix that first. can't be sure what's going on with options like that.

<SELECT NAME="Label2">
			<OPTION VALUE="Label2">Label2</label>
		</SELECT>

	</TD>
</TR>
<TR>
	<TD>Label3</TD>
	<TD>
		<SELECT NAME="Label3">
			<OPTION VALUE="Label3">Label3<OPTION VALUE="">
		</SELECT>

 

 

Link to comment
Share on other sites

I said in my post, i m just send example of my form..

 

its is actually like this:

 

search form is this.

 

 

<form  name="drop_list" id="drop_list" action="<?php echo htmlentities($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="return OnSubmitForm();">

<fieldset>
<legend>search form</legend>

	<TABLE>
	<TR>
		<TD>  </TD>
		<TD><INPUT TYPE="radio" name="transtype" value ="sale" <?php if($ttype != 'rent') echo 'checked="checked"';?> onClick='setSaleSelection();'> Sale  <INPUT TYPE="radio" name="transtype" value ="rent" <?php if($ttype=='rent') echo 'checked="checked"';?> onClick='setRentDefault();'> Rent</TD>
	</TR>
	<TR>
		<TD>Location:</TD>
		<TD>

		<SELECT name="location"  alt="Location" title="Location">
			<option value="0" SELECTED>Location</option>
			<option value="" <?php if (!(strcmp("", $_POST["location"]))) {echo "selected=\"selected\"";} ?>>Select Location</option>
			<?php do { ?>
			<option value="<?php echo $row_Recordset1['location']?>"<?php if (!(strcmp($row_Recordset1['location'], $_POST["location"]))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['location']?></option>

			<?php
			} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));

			$rows = mysql_num_rows($Recordset1);
			if($rows > 0) {
				mysql_data_seek($Recordset1, 0);
				$row_Recordset1 = mysql_fetch_assoc($Recordset1);
			  }
			?>
		</SELECT></TD>
	</TR>
	<TR>
		<TD>Bedrooms:</TD>
		<TD>
			<select name="bedrooms"  style="width:150px;"alt="Bedrooms" title="Bedrooms">
			<option value="0" SELECTED>Bedrooms</option>
			  <?php 

				for($x = 0; $x < count($bedroomarray); $x++){
				   // write "selected" if the value matches the one posted
				   if($bedroomarray[$x] == $_POST['bedrooms']){
					  $selected = 'selected';
				   }else{
					   $selected = '';
				   }
					// print the option
					echo '<option value="'.$bedroomarray[$x].'"'.$selected.'>'.$bedroomarray[$x].'</option>';
				   }
	             ?>
		        </SELECT>
		    </TD>
	</TR>
	<TR>
		<TD>Price from:</TD>
		<TD>
			<select name="pricefrom"  alt="Price from" title="Price from">
				<option value="">Price from</option>
			</select>

		</TD>
	</TR>
	<TR>
		<TD>Price to:</TD>
		<TD>

		<select name="priceto"  alt="Price to" title="Price to">
			<option value="">Price to</option>
        </select>
		</TD>
	</TR>
	<TR>
		<TD>Property Type:</TD>
		<TD>

		<SELECT name="propertytype" alt="Property type" title="Property type">
			<option value="" <?php if (!(strcmp("", $_POST["propertytype"]))) {echo "selected=\"selected\"";} ?>>Choose...</option>
			<?php do {  ?>
			<option value="<?php echo $row_Recordset2['propertytype']?>"<?php if (!(strcmp($row_Recordset2['propertytype'], $_POST["propertytype"]))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset2['propertytype']?></option>
						  <?php
			} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
			$rows = mysql_num_rows($Recordset2);

			if($rows > 0) {
				mysql_data_seek($Recordset2, 0);
				$row_Recordset2 = mysql_fetch_assoc($Recordset2);
			  }
			?>
		</SELECT>
		</TD>
	</TR>
	<TR>
		<TD>Country:</TD>
		<TD>
		<SELECT NAME="country"  alt="Country" title="Country">
			<option value="0">Dubai</option>
				<?php 
				for($x = 0; $x < count($countryarray); $x++){
					// write "selected" if the value matches the one posted
					if($countryarray[$x] == $_POST['country']){
						$selected = 'selected';
					}else{
						$selected = '';
					}
					// print the option
						echo '<option value="'.$countryarray[$x].'"'.$selected.'>'.$countryarray[$x].'</option>';
				}
			 ?>

		</SELECT>
		</TD>
	</TR>
	<TR>
		<TD>Off plan:</TD>
		<TD><input <?php if (!(strcmp($_POST["offplan"],"-1"))) {echo "checked=\"checked\"";} ?> name="offplan" type="radio" value="-1"></TD>
	</TR>
	<TR>
		<TD>Completed:</TD>
		<TD><input <?php if (!(strcmp($_POST["offplan"],"0"))) {echo "checked=\"checked\"";} ?> name="offplan" type="radio" value="0"></TD>
	</TR>
	<TR>
		<TD>Added to site:</TD>
		<TD>
		<select name="addedtosite" alt="Added to site" title="Added to site">
			<option value="9999" <?php if (!(strcmp(9999, $_POST["addedtosite"]))) {echo "selected=\"selected\"";} ?>>Anytime</option>
			<option value="1" <?php if (!(strcmp(1, $_POST["addedtosite"]))) {echo "selected=\"selected\"";} ?>>Last 24 Hours</option>
			<option value="3" <?php if (!(strcmp(3, $_POST["addedtosite"]))) {echo "selected=\"selected\"";} ?>>Last 3 days</option>
			<option value="7" <?php if (!(strcmp(7, $_POST["addedtosite"]))) {echo "selected=\"selected\"";} ?>>Last 7 days</option>
			<option value="14" <?php if (!(strcmp(14, $_POST["addedtosite"]))) {echo "selected=\"selected\"";} ?>>Last 14 days</option>
		</select>
		</TD>
	</TR>
	<TR>
		<TD>Sort by:</TD>
		<TD>
		<select name="sortby" alt="Sort by" title="Sort by">
			<option value="desc" <?php if (!(strcmp("desc", $_POST["sortby"]))) {echo "selected=\"selected\"";} ?>>Highest Price</option>
			<option value="asc" <?php if (!(strcmp("asc", $_POST["sortby"]))) {echo "selected=\"selected\"";} ?>>Lowest Price</option>
		</select>
		</TD>
	</TR>
	<TR>
		<TD></TD>
		<TD>
			<input type="submit"  id="submit" name="submit"  value="Search properties">
		</TD>
	</TR>
	</TABLE>
</fieldset>
</form>

 

 

the above is my actual form on both pages. when I submit the form i get no data found message, this makes think that the problem lies with the paging stuff

 

<?php

$query_Recordset3 = $searchstring;
$Recordset3 = mysql_query($query_Recordset3, $mydb) or die(mysql_error());
//$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$nume = mysql_num_rows($Recordset3);


$page_name="result.php"; //  If you use this code with a different page ( or file ) name then change this 

if(!isset($start)) { // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0); 
$limit = 10;        // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 

$query= $searchstring." limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();


?>
<?php

if($nume == '0'){

echo '<b>No data found</b>';

}else{
//display records here

}

?>

 

when i submit the form from result.php i get the above message. If i submit the form from search.php then it displays records.

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.