#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
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    If that is all of your code, what you say is happening should not be possible. Run this page, right click and select 'view source', tehn paste the source of the outputted html here so we can see it please.
  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


    Here is the html source from right clicking on the page:
    Below is my full php code.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Title</title>
    </head>

    <body>
    <P>Fang&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</P>
    <P>Fluffy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f</P>

    <a href="sample.phtml?offset=2&page=1">PREV</a> &nbsp;
    <a href="sample.phtml?offset=0&page=1">1</a> &nbsp;
    2 &nbsp;
    <a href="sample.phtml?offset=4&page=3">3</a> &nbsp;
    <a href="sample.phtml?offset=6&page=4">4</a> &nbsp;
    <a href="sample.phtml?offset=8&page=5">5</a> &nbsp;
    <a href="sample.phtml?offset=6&page=3">NEXT</a><p>

    <br><br>
    <form>
    3
    <br><br>

    <select name="PageJump" onChange="top.location.href='sample.phtml?page=3&offset='+ this.options[selectedIndex].value">

    <option value=0 >page 1</option><option value=2 >page 2</option><option value=4 >page 3</option><option value=6 >page 4</option><option value=8 >page 5</option></select>
    </form>

    </body>
    </html>

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Title</title>
    </head>

    <body>
    <?
    $dbcnx = @mysql_connect("localhost", "", "");

    if (!$dbcnx)
    { echo( "<P>Unable to connect to the " .
    "database server at this time.</P>" );
    exit();
    }

    mysql_select_db("menagerie", $dbcnx);

    $limit=2; // rows to return
    $numresults=mysql_query("SELECT * FROM pet ORDER BY name");
    $numrows=mysql_num_rows($numresults);

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

    // get results
    $result=mysql_query("select * ".
    "from pet ".
    "order by name limit $offset,$limit");

    // now you can display the results returned
    while ($data=mysql_fetch_array($result)) {
    print("<P>" . $data["name"] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data["sex"] . "</P>");
    }

    // next we need to do the links to other results
    if ($page == "")
    {$iPageCurrent = 1;}
    else
    {$iPageCurrent = $page;}

    $iPagePrevious = $iPageCurrent - 1;
    $iPageNext = $iPageCurrent + 1;


    if ($offset>0) { // bypass PREV link if offset is 0
    $prevoffset=$offset-2;
    print "<a href=\"sample.phtml?offset=$prevoffset&page=$iPagePrevious\">PREV</a> &nbsp; \n";
    }

    // calculate number of pages needing links
    $pages=intval($numrows/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($numrows%$limit) {
    // has remainder so add one page
    $pages++;
    }

    //for ($i=1;$i<=$pages;$i++) { // loop thru
    // $newoffset=$limit*($i-1);
    // print "<a href=\"sample.phtml?offset=$newoffset\">$i</a> &nbsp; \n";
    //}

    for ($i=1;$i<=$pages;$i++) { // loop thru
    $newoffset=$limit*($i-1);
    if ($i == $iPageCurrent)
    {print "$i &nbsp;";}
    else
    {print "<a href=\"sample.phtml?offset=$newoffset&page=$i\">$i</a> &nbsp; \n";}
    }

    // check to see if last page
    if (!(($offset/$limit)==$pages-1) && $pages!=1) {
    // not last page so give NEXT link
    $newoffset=$offset+$limit;
    print "<a href=\"sample.phtml?offset=$newoffset&page=$iPageNext\">NEXT</a><p>\n";
    }
    ?>

    <br><br>
    <?
    $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>

    </body>
    </html>

    Thanks.
  6. #4
  7. 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


    If I look at the code by right clicking, the correct variable shows up in the code, but it is not being passed to the querystring. The variable showing up in the querystring is the previous page.

    How can one variable show in the source and another in the querystring???
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I am confused. Is what you said here incorrect?
    The first print($marker) is correct. The print($marker) located inside the JavaScript is not.
    because I see this in your html

    <form>
    3
    <br><br>

    <select name="PageJump" onChange="top.location.href='sample.phtml?page=3&offset='+ this.options[selectedIndex].value">



    I do not have time to look at the rest of your code, but I would check the url that appears in your address bar after clicking this. Paste it here also. It looks as if you are getting that value sent, your error must be located somewhere else. GL, I'm off to the dentist.

    EDIT: I see that you seem to be using frames. That puts a crimp in it. Use an alert and instead of assigning that to the location, pop it up in an alert to see what is being passed.
    Last edited by Nemi; September 5th, 2001 at 09:19 AM.
  10. #6
  11. 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


    Here is the URL

    http://localhost/database/sample.phtml?page=2&offset=4

    It shows page 2 instead of page 3. I am very confused. I don't understand how it can show 3 in the code and 2 in the querystring.
  12. #7
  13. 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


    Figured it out. I am going to pass all variable through the option values:

    print("<option value=" . $newoffset . "&page=" . $i . ">page " . $i . "</option>");

    Thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo