...

View Full Version : Using PHP Inside Javascript



Troy297
01-31-2007, 07:01 PM
This may seem very newbish to some but I have never tried to use php inside javascript before and am having problems now that I am trying it.....

I have a slide out menu as seen here (http://www.dynamicdrive.com/dynamicindex1/anylinkvertical.htm) and as one of the menu options that pops out I am trying to have it say "View Inbox(#)" and where the # is I use php to count the messages in the mysql database that are for them. So here is my code... how can I do this?


<script type="text/javascript">
//other stuff here
var menu3=new Array()
menu3[0]='<?php
echo "<a href='inbox.php'>";
$result = mysql_query("SELECT COUNT(id) FROM rp_pm WHERE todj='$_SESSION[username]'");
$total = mysql_fetch_array($result);
echo "View Inbox (". $total['COUNT(id)'] .")</a>";
?>'
//other stuff here
</script>

So there is the php code I am trying to use... now I realize that I am not allowed to use any ' inside the php because the menu= uses them... but even when I tried taking them it out didn't work.

Any help would be great! Thanks so much!

david_kw
01-31-2007, 07:22 PM
I've done almost nothing in PHP but maybe this?



<script type="text/javascript">
//other stuff here
var menu3=new Array()
menu3[0]='<?php
echo "<a href=\"inbox.php\">";
$result = mysql_query("SELECT COUNT(id) FROM rp_pm WHERE todj='$_SESSION[username]'");
$total = mysql_fetch_array($result);
echo "View Inbox (". $total['COUNT(id)'] .")</a>";
?>'
//other stuff here
</script>


david_kw

Troy297
01-31-2007, 07:25 PM
Hi David,

Nope sorry, I had already tried that in a variety of different ways with slashes all over but then the php just started giving me parse errors... but thats ok...

I have figured out a way to code my layout so I don't need the view inbox thing inside the actual js menu, but thxs anyways!

Ancora
01-31-2007, 08:46 PM
Troy:

Echo the $messageCount to a hidden field. onload, set the array element value to the hidden field value:



<?php

$messageCount = "";

$query = mysql_query("SELECT COUNT(id) FROM rp_pm WHERE todj='$_SESSION[username]'");
$result = mysql_fetch_array($query);
if ($result)
{
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
$messageCount = $row[0];
}
}
//echo $messageCount;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Manage Tunes</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var menu3 = [];

function init(){

menu3[0]= document.getElementById('msg').value;
}

onload=init;

</script>


<style type="text/css">

body {margin-top:20px;background-color:#eae3c6}

</style>
</head>
<body>
<input type='hidden' id='msg' value="<?php echo $messageCount; ?>">
</body>
</html>

neomaximus2k
02-01-2007, 12:42 PM
your code should read like this....



<script type="text/javascript">
//other stuff here
var menu3=new Array()
menu3[0]='<?php
echo '<a href="inbox.php">';
$result = mysql_query("SELECT COUNT(id) FROM rp_pm WHERE todj='$_SESSION[username]'");
$total = mysql_fetch_array($result);
echo "View Inbox (". $total['COUNT(id)'] .")</a>";
?>'
//other stuff here
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum