PDA

View Full Version : Javascript Redirect Passing PHP Variables



timunit
08-01-2007, 08:47 PM
Hi,

I am asking this question here, but it might be more of a PHP question. I have a .php page that returns the results from a search of my database. I want the page to redirect to a new page if no results are returned. The new page is a form that the user can complete to request a quote. I have the redirect working fine but the tricky part is, I want some of the fields on the form on the new page to be automatically populated with values from the search.

As an example, this is a website for an auto dealer. If a user does a search for a Cadillac CTS (make:Cadillac, model:CTS) and there are none in the database, I want the user to be redirected to a new page with a form where Make would be populated with “Cadillac” and Model with “CTS”.

Here in essence is my code:

if(mysql_num_rows($result) == 0) {

echo '<script type="text/javascript">

<!--
window.location = "http://www.mywebsite.com/quote.php?mak=$make&$mod=model"
//-->
</script>';


}


The code above redirects correctly but the Make and Model fields are populated with “$make” and “$model” instead of the values of those variables.

I am using Javascript to redirect because the header is already set and using a PHP redirect returns an error.

Any suggestions?

Thanks.

Tim

Tabo
08-01-2007, 09:56 PM
Its because your ecoing $model and not the value of $model.



if(mysql_num_rows($result) == 0) {

echo '<script type="text/javascript">\n<!--\nwindow.location = "http://www.mywebsite.com/quote.php?mak=';
echo $make;
echo '&';
echo $mod; //is this misstyped?
echo '=model"'; //is this misstyped?
echo '//-->\n</script>';


Something like that but im not sure i dont use php much

timunit
08-01-2007, 11:38 PM
Tabo,

Thanks. That was exactly it. Rookie mistake.

Here is my modified code which is now working:

if(mysql_num_rows($result) == 0){

echo '<script type="text/javascript">

<!--
window.location = "http://www.mywebsite.com/quote.php?mak='.$make.'&mod='.$model.'"
//-->
</script>';


}


Tim