Page 1 of 1
How to show php results in JS?
Posted: Sat Feb 14, 2009 1:00 pm
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!
Re: How to show php results in JS?
Posted: Sat Feb 14, 2009 10:05 pm
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>
Re: How to show php results in JS?
Posted: Sun Feb 15, 2009 2:41 am
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".
Re: How to show php results in JS?
Posted: Sun Feb 15, 2009 1:04 pm
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".
Re: How to show php results in JS?
Posted: Mon Feb 16, 2009 9:27 am
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]; ?>
Re: How to show php results in JS?
Posted: Mon Feb 16, 2009 11:31 am
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';
Re: How to show php results in JS?
Posted: Tue Feb 17, 2009 5:02 am
by najn
Thank you very much! You solved my problem.

Re: How to show php results in JS?
Posted: Tue Feb 17, 2009 5:37 am
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.
Re: How to show php results in JS?
Posted: Wed Feb 18, 2009 3:59 pm
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.

Edited above.