Log in

View Full Version : Form variable not passing to dynamicaly loaded page, please help



djvk87
03-30-2011, 02:04 AM
Hi, I have a PHP page with a search input form that dynamically loads a results page through Dynamic Ajax Content (script from here (http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm)) where a MySQL database is searched for the search variable from the form and results displayed as tabular data. The 2 pages source code can be viewed below.

search.php

<img src="/common/images/yanmar/spareparts/YanmarSpareParts.png" align="center">
<br />Please use the below form to search for the Yanmar Part you're looking for.<br />
<br />
<form name="yspsearch" action="javascript:ajaxpage('/yanmar/ajaxpages/searchresults.php',%20'contentarea')" method="get">
Search: <input type="text" name="term" />
<input type="submit" name="Submit" value="Search" />
</form>

searchresults.php

<?php

// Get the search variable from URL

$var = @$_GET['term'];
$trimmed = trim($var); // Trim whitespace from the stored variable


// Check for an empty string and display message
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// Check for a search parameter
if (!isset($var))
{
echo "<p>We don't seem to have a search parameter!</p>";
exit;
}

// Connect to database
mysql_connect ("host","username","password"); //(host,username,password)

// Specify database
mysql_select_db ("host_database") or die(mysql_error());

// Build SQL Query
$query = "select * from database where Heading like \"%$term%\" or SubHeading like \"%$term%\" or Description like \"%$term%\" or PartNo like \"%$term%\"
order by Heading";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, display message

if ($numrows == 0)
{
echo "<h1>Search Results</h1>";
echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
exit;
}

// Next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// Get Results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die(mysql_error());

// Display what the person searched for
echo "<p>You searched for: &quot;" . $var . "$quot;</p>";

// Begin to show results
echo "<h1>Search Results</h1>";

// Show Results in table
while($row= mysql_fetch_array( $result)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"".$row['Image']."\">";
echo "</td><td>";
echo $row['SubHeading'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['PartNo'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
}

echo "</table>";

The problem I am having is the search variable is not passing to the dynamically loaded page and so it always displays "Please enter a search...".

What am I doing wrong here?

A working online example can be found by clicking the search button at the top left of this page (http://woodenboatshop.com.au/yanmar/spareparts.php).

Thanks

bluewalrus
03-30-2011, 02:39 AM
Is this javascript:ajaxpage('/yanmar/ajaxpages/searchresults.php',%20'contentarea') sending?

djvk87
03-30-2011, 02:47 AM
That is the javascript to load the external searchresults.php page dynamically within a div on the page.
I have thought it may be this method that is causing the variable to not pass although I don't know why.
Usually the form would send directly to another page but I need it to be dynamically loaded on the same page.

bluewalrus
03-30-2011, 03:13 AM
Can you post the code for your function 'ajaxpage'?

djvk87
03-30-2011, 03:45 AM
The functions for Dynamic Ajax Content are here:


<script type="text/javascript">

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

</script>

As I mentioned it's a script provided here on Dynamic Drive.

bluewalrus
03-30-2011, 04:07 AM
Well that js is above my knowledge. Can you try adding this to see if the value is being sent correctly?



<?php
// Get the search variable from URL

print_r($_GET);

$var = @$_GET['term'];
$trimmed = trim($var); // Trim whitespace from the stored variable

djvk87
03-30-2011, 05:13 AM
Ok, I have done that.
It now displays 'Array ( [1301461934681] => )' (the number changes) followed by the usual 'Please enter a search...'

bluewalrus
03-30-2011, 02:48 PM
That's the problem the $var is being set to '' because the $_GET['term'] is not defined somewhere in that js the array key ie being swapped from 'term' to another unit (unix time plus something?). If you input data is it displayed like

Array ( [1301461934681] => 'DATA INPUTTED')