#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15

    Question Passing PHP Variable to Javascript


    I am attempting to show two records at a time from a database query. I have previous and next buttons that work. However, I am wanting to include a "jump to" pull down menu as part of the functionality.

    To get this to work, I am needing to pass a variable stating what page I am on through a JavaScript onChange function. If I print the variable to the page outside of the onChange function, it shows the correct page number. If I use the variable inside of the onChange function it is not coming out correctly. It is holding the value of the previous page instead of the current page.

    Am I missing something or is there a different way to include a variable in a JavaScript function. Your insight is appreciated.

    Code:

    <?
    $marker = 1;
    if ($offset == 0)
    {$marker = 1;}
    else
    {$marker = ($offset/2) + 1;}

    ?>
    <form>
    <?
    print($marker);
    ?>

    <br><br>

    <select name="PageJump" onChange="top.location.href='sample.phtml?page=<? print($marker); ?>&offset='+ this.options[selectedIndex].value">

    <?
    for ($i=1;$i<=$pages;$i++){
    $newoffset=$limit*($i-1);
    print("<option value=" . $newoffset . " >page " . $i . "</option>");
    }
    ?>
    </select>
    </form>

    The first print($marker) is correct. The print($marker) located inside the JavaScript is not.

    Thank you for any help.
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    bramsey-

    Every .selectedIndex is a property of a select object rather than a global (window.) variable; try

    <select name="PageJump" onChange="top.location.href='sample.phtml?page=<?print($marker); ?>&offset='+ this.options[this.selectedIndex].value">
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15

    Passing PHP variable to Javascript


    Thanks for the input. I figured it out earlier this morning right after I posted this. That is exactly what I did. It worked perfectly.

    Thank you for your response.

IMN logo majestic logo threadwatch logo seochat tools logo