Results 1 to 3 of 3

Thread: Javascript Redirect Passing PHP Variables

  1. #1
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Redirect Passing PHP Variables

    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

  2. #2
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Its because your ecoing $model and not the value of $model.

    Code:
    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

  3. #3
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •