PHP + AJAX search - change POST to GET?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davil
Forum Newbie
Posts: 6
Joined: Fri Jul 13, 2007 5:41 am

PHP + AJAX search - change POST to GET?

Post by davil »

This might sound like the stupidest question ever but here goes.

I'm doing a property website and it's almost complete. I have a simple form with some select boxes where you select a county then with AJAX and PHP it loads back all the cities and towns in that county for the next select box then the user submits their search...
It works fine and loads the next PHP which shows all the results {viewresults} but the problem is that the data is all being sent to the next PHP file using POST. this works fine but as you know it means that the users can't just click back or it will say that they either have to RE-POST the data or whatever. I had thought about simply redirecting them to a 3rd file, i.e. the 2nd file gets all the variables together and just sends them to the third file in the URL using GET but I thought that clicking back would just get you back to where you started from. I am guessing but I have seen this behaviour before on other pages on the interwebness.
So before I went ahead and did it I thought I'd ask the pros.

Is there an easier way to do this? (i.e. to use GET to put variables in URL so users can click back etc.)

Here's the main PHP

Code: Select all

<?php
include ("ajaxheader.inc.php");
?>

<form enctype="multipart/form-data" method="POST" action="index.php?locate=viewresults">

<select id="cp_county" name="cp_county" onchange="getCityList(this)" size="1" style="background-color:#CFBFCF;width:74%;">
<option selected value="%">All Counties</option>
<?php include("area.inc.php"); ?>	
</select>
<br><br>

<select id="cp_town" name="cp_town" size="1" style="background-color:#CFBFCF;width:74%;">
<option value="%">All Towns & Cities</option>
</select>
<br><br>

<select name="minprice" size="1" style="background-color:#CFBFCF;width:74%;">
<?php include ("min_price.inc.php"); ?>
</select>
<br><br>

<select name="maxprice" size="1" style="background-color:#CFBFCF;width:74%;">
<?php include ("max_price.inc.php"); ?>
</select>

<input type="hidden" name="srch_property_type" value="%">
<input type="hidden" name="minbeds" value="1">
<input type="hidden" name="maxbeds" value="99">


<br>
<br>
<input type="submit" value="Search NOW!"><br>

</form>
Here's ajaxheader.inc.php

Code: Select all

<?php
ECHO <<<HERE
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">

var ajax = new sack();

function getCityList(sel)
{
	var countyCode = sel.options[sel.selectedIndex].value;
	document.getElementById('cp_town').options.length = 0;	// Empty city select box
	if(countyCode.length>0){
		ajax.requestFile = 'gettown.php?countyCode='+countyCode;	// Specifying which file to get
		ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}


function createCities()
{
	var obj = document.getElementById('cp_town');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

		
</script>
HERE;
?>
Here's gettown.php

Code: Select all

<?php
require ("config.php");

if(isset($_GET['countyCode'])){
$countycode=$_GET['countyCode'];

// ----------------------------------
//      find out county name
$sqlstart = "SELECT `county` FROM `townsandcounties` WHERE `countycode` = '" . $countycode . "' LIMIT 1";
$resultone = mysql_query($sqlstart) or die(mysql_error());
$rowone = mysql_fetch_array($resultone);
$countya = $rowone['county'];
// ----------------------------------

$sql = "SELECT * FROM `townsandcounties` WHERE `countycode` = '" . $countycode . "'";

$result = mysql_query($sql) or die(mysql_error());

	$start = "All areas in ". $countya;
	echo "obj.options[obj.options.length] = new Option('" . $start. "','" . 0 . "'); \n";

	while($row = mysql_fetch_array($result)){
	$j=$j+1;

	echo "obj.options[obj.options.length] = new Option('" . $row['town']. "','" . $j . "'); \n";

	}
 
}

?>
I won't post up viewresults.php just yet unless needed as it is quite complex and the most important part for us is just requesting the variables from the POST and creating and SQL query to find the specific properties based on search criteria.... there's not much to it....


Anyway any help would be greatly appreciated.
Last edited by davil on Fri Jul 13, 2007 6:58 am, edited 3 times in total.
davil
Forum Newbie
Posts: 6
Joined: Fri Jul 13, 2007 5:41 am

holy cakes!

Post by davil »

I think I've solved it already
here's the new main code

Code: Select all

<?php
include ("ajaxheader.inc.php");
?>

<form enctype="multipart/form-data" method="GET" action="index.php">
<input type="hidden" name="locate" value="viewresults">

<select id="cp_county" name="cp_county" onchange="getCityList(this)" size="1" style="background-color:#CFBFCF;width:74%;">
<option selected value="%">All Counties</option>
<?php include("area.inc.php"); ?>   
</select>
<br><br>

<select id="cp_town" name="cp_town" size="1" style="background-color:#CFBFCF;width:74%;">
<option value="%">All Towns & Cities</option>
</select>
<br><br>

<select name="minprice" size="1" style="background-color:#CFBFCF;width:74%;">
<?php include ("min_price.inc.php"); ?>
</select>
<br><br>

<select name="maxprice" size="1" style="background-color:#CFBFCF;width:74%;">
<?php include ("max_price.inc.php"); ?>
</select>

<input type="hidden" name="srch_property_type" value="%">
<input type="hidden" name="minbeds" value="1">
<input type="hidden" name="maxbeds" value="99">


<br>
<br>
<input type="submit" value="Search NOW!"><br>

</form>
yep this seems to be working... although I would appreciate any comments on the code....
Post Reply