How to show php results in JS?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
najn
Forum Newbie
Posts: 4
Joined: Sat Feb 14, 2009 10:44 am

How to show php results in JS?

Post by najn »

Hello,

Next php code shows how many reg. users I have:

Code: Select all

 
<?PHP
 
include("includes/configuration.php");
 
$sql = 'SELECT COUNT(userid) FROM user';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$r[0].' users';
 
?>

I would like to show results in Javascript scroller:

Code: Select all

<script type="text/javascript">
 
var delay = 2000; //set delay between message change (in miliseconds)
var maxsteps=30; // number of steps to take to change from start color to endcolor
var stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
var startcolor= new Array(181,224,74); // start color (red, green, blue)
var endcolor=new Array(96,96,96); // end color (red, green, blue)
 
var fcontent=new Array();
begintag='<div style="font: normal 12px Arial; padding: 4px;">'; //set opening tag, such as font declarations
fcontent[0]="TEKST 1";
fcontent[1]="TEKST 2";
[b]fcontent[2]="TEXT HEREEEEEEEEEEEE";[/b]
fcontent[3]="TEKST 4";
closetag='</div>';
 
var fwidth='550px'; //set scroller width
var fheight='24px'; //set scroller height
...
 
I dont know how to do this?


If I use this code for the solution:

Code: Select all

Header("content-type: application/x-javascript");
I get error because I already have define header in index.php file.
Header from index.php file:

Code: Select all

header("location:".$requestedurl[count($requestedurl)-1]);
Need help!
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: How to show php results in JS?

Post by sparrrow »

You display PHP data inside Javascript the same way you display it in HTML.

Code: Select all

$foobar = "I'm a PHP string!";
 
<script language="javascript">
alert('I am Javascript, and here is some PHP: <?php echo $foobar; ?>');
</script>
najn
Forum Newbie
Posts: 4
Joined: Sat Feb 14, 2009 10:44 am

Re: How to show php results in JS?

Post by najn »

Thx sparrow but I didn't understand!

I would like to show results of

Code: Select all

<?PHP
include("includes/configuration.php");
$sql = 'SELECT COUNT(userid) FROM user';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$r[0].' users'; 
?>
 
in this JavaScript place

Code: Select all

var fcontent=new Array();
begintag='<div style="font: normal 12px Arial; padding: 4px;">'; //set opening tag, such as font declarations
fcontent[0]="MESSAGE 1";
fcontent[1]="MESSAGE FROM PHP CODE";
closetag='</div>';
instead "MESSAGE FROM PHP CODE".
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: How to show php results in JS?

Post by sparrrow »

I told you exactly how to do it. If you have a PHP variable and you want it's value printed ANYWHERE in your code, just break into php, echo the string, then break out of php. Like this exactly, but use your own var name: <?php echo $foobar; ?>

Also remember, the code executes serially. So you can't use the php variable inside the JS unless you already set it in the lines above.

See below.
najn wrote:Thx sparrow but I didn't understand!

I would like to show results of

Code: Select all

<?PHP
include("includes/configuration.php");
$sql = 'SELECT COUNT(userid) FROM user';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$r[0].' users'; 
?>
 
in this JavaScript place

Code: Select all

var fcontent=new Array();
begintag='<div style="font: normal 12px Arial; padding: 4px;">'; //set opening tag, such as font declarations
fcontent[0]="MESSAGE 1";
fcontent[1]="[color=#FF0000][b]<?php echo $r[0]; ?>[/b][/color]";
closetag='</div>';
instead "MESSAGE FROM PHP CODE".
najn
Forum Newbie
Posts: 4
Joined: Sat Feb 14, 2009 10:44 am

Re: How to show php results in JS?

Post by najn »

Thx sparrow. Now It's working.

But!!! Now I get results on two places:
1st on the place where is PHP code, and
2nd in the JS

I want to show results only in the JS, how to do it?
--

If I have PHP code like this:

Code: Select all

<?PHP
session_start();
require_once("includes/configuration.php");
    
//1
$sql = 'SELECT COUNT(userid) FROM user';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$r[0].' users';
 
//2
$sql = 'SELECT COUNT(questionid) FROM question';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$p[0].' questions';  
 
//3
$sql = 'SELECT COUNT(answerid) FROM answer';
$upit = mysql_query($sql) or die();
$r = mysql_fetch_array($upit);
echo 'Total '.$s[0].' answers';  
 
?>
More results. How to show all them separate?
I tried this, but wont work:

Code: Select all

<?php echo $r[0]; ?>
<?php echo $r[1]; ?>
<?php echo $r[2]; ?>
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: How to show php results in JS?

Post by sparrrow »

On lines 8, 14, and 20, you are assigning your result set to $r, but it looks like you are trying to access results from $p and $s from lines 15 and 21, even though those variables are empty.

Please read about echo in the PHP manual. It is used to print data to the screen. So remove, change, or comment any lines that you have that seem to be printing data that you do not want printed. (9, 15, 21).

In the 2nd code block, you are looking for array keys inside the $r result set that do not exist. When you assign a mysql result to a variable, it will only contain as much data as that single query returned. You could create a new array and assign the result from each mysql query to a new key in it. Then you could access the data in the way you are attempting to in the 2nd code block.

So on lines 8, 15, and 21, you could insert a line similar to the following, which would put each result into a new array key:

Code: Select all

$message[] = 'Total '.$r[0].' questions';
najn
Forum Newbie
Posts: 4
Joined: Sat Feb 14, 2009 10:44 am

Re: How to show php results in JS?

Post by najn »

Thank you very much! You solved my problem. :D
davis
Forum Newbie
Posts: 6
Joined: Tue Feb 17, 2009 5:29 am

Re: How to show php results in JS?

Post by davis »

The following code given by 'Sparrow' is not working:

$foobar = "I'm a PHP string!";

<script language="javascript">
alert('I am Javascript, and here is some PHP: <?php echo $foobar; ?>');
</script>

I would like to know if anything is missing or any changes.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: How to show php results in JS?

Post by sparrrow »

Code: Select all

 
<?php
$foobar = "I am a PHP string!";
//$foobar = addslashes("I'm a PHP string!"); //This works too
?>
 
<script language="javascript">
<!--
alert('I am Javascript, and here is some PHP: <?php echo $foobar; ?>');
-->
</script>
The code was just fine, but the specific test phrase I picked was what broke it. Must escape out Javascript special characters that may be used in the php string. :P Edited above.
Post Reply