Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Using PHP Inside Javascript

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

    Code:
    <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!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've done almost nothing in PHP but maybe this?

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

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    Troy:

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

    Code:
    <?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>
    Last edited by Ancora; 02-01-2007 at 08:33 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your code should read like this....

    Code:
    <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>
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)


  •  

    Posting Permissions

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