JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 5th, 2001, 08:30 AM
bramsey bramsey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: USA
Posts: 226 bramsey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 35 sec
Reputation Power: 13
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.

Reply With Quote
  #2  
Old September 5th, 2001, 08:57 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 39 m 5 sec
Reputation Power: 116
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.

Reply With Quote
  #3  
Old September 5th, 2001, 09:05 AM
bramsey bramsey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: USA
Posts: 226 bramsey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 35 sec
Reputation Power: 13
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.

Reply With Quote
  #4  
Old September 5th, 2001, 09:10 AM
bramsey bramsey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: USA
Posts: 226 bramsey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 35 sec
Reputation Power: 13
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???

Reply With Quote
  #5  
Old September 5th, 2001, 09:16 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 39 m 5 sec
Reputation Power: 116
I am confused. Is what you said here incorrect?
Quote:
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.

Reply With Quote
  #6  
Old September 5th, 2001, 09:20 AM
bramsey bramsey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: USA
Posts: 226 bramsey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 35 sec
Reputation Power: 13
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.

Reply With Quote
  #7  
Old September 5th, 2001, 09:34 AM
bramsey bramsey is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: USA
Posts: 226 bramsey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 35 sec
Reputation Power: 13
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Passing PHP Variable to Javascript

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap