Web Hosting Talk







View Full Version : Javascript and PHP


Atomic Haven
05-03-2005, 06:37 PM
Is there anyway to make this work?



<SCRIPT Language="Javascript">
ponumber= prompt ("<?PHP echo mssql_num_rows($result)?> records found. How many do you wish to view?");
document.write ("<?PHP $ponumber = " +ponumber+ " ?>");
</SCRIPT>

error404
05-03-2005, 07:07 PM
No, by the time the javascript is executed the PHP is already completed. You can't intersperse them because one is running on the client and the other on the server; the communication is essentially one-way. The only way to do that would be to submit a form that then returns the desired results, or use more advanced Javascript to call PHP to get the data and then format it in JS.

saghir69
05-04-2005, 07:22 AM
maybe you can do something like this

echo '<SCRIPT Language="Javascript">
ponumber= prompt ("<?PHP echo mssql_num_rows($result)?> records found. How many do you wish to view?");
document.write ("<?PHP $ponumber = " +ponumber+ " ?>");
</SCRIPT>
';

Syphic
05-05-2005, 03:08 AM
Here are some ideas on how to recall javascript and have php load in new inf

Check out the code below to give you an idea.


<script language="javascript">

<?php $mytickround = 5; ?>

var myticksec = <?php echo 59 - strftime("%S"); ?>;
var mytickmin = <?php echo $mytickround - 1 - (strftime("%M") - (floor(strftime("%M") / $mytickround) * $mytickround)); ?>;

var rtsec = <? echo strftime("%S") + 1; ?>;
var rtmin = <? echo strftime("%M"); ?>;

function myticktime()
{
if(myticksec <= 0 && mytickmin <= 0)
{
myticksec = 0;
mytickmin = <?php echo $mytickround; ?>;
}
if(myticksec <= 0)
{
mytickmin = mytickmin - 1;
myticksec = 60;
}

myticksec = myticksec - 1;

if(rtsec >= 59 && rtmin >= 59)
{
rtsec = 0;
rtmin = 0;
}
if(rtsec >= 59)
{
rtmin = rtmin + 1;
rtsec = 0;
}

rtsec = rtsec + 1;

if(myticksec < 10) {document.getElementById('tick_timer').innerHTML = mytickmin + ":0" + myticksec + " - " + rtmin + ":" + rtsec};
if(mytickmin < 10) {document.getElementById('tick_timer').innerHTML = "0" + mytickmin + ":" + myticksec + " - " + rtmin + ":" + rtsec};
if(myticksec < 10 && mytickmin < 10) {document.getElementById('tick_timer').innerHTML = "0" + mytickmin + ":0" + myticksec + " - " + rtmin + ":" + rtsec};
}

window.setInterval("myticktime()", 1000);
</script>


The above script might be a bit complex but it basicly counts down from 5 mins to 0 and then starts over again and it does it according to the time that php has. Now, i am no rocket scientist but I would think that if you just place all this code and put the php information you are wanting to change within the actual function that does the look each "etc" amount of miliseconds. I would think you could pull out what ever php info you need each go around.

If that doesnt work another idea is to make a mime type for php which has the extenion of js and then within the external js file put your php stuff. Then when you call the js script each time it will load the new information.

Either way, give both of these a try and let me know how it works out.

altano
05-05-2005, 11:29 AM
The fact that you asked that question shows that you need to understand PHP better, but when you do, you might want to take a look at AJAX (a framework for client side javascript applications that interface with server side languages using XML and HTTP).

SAJAX is one solution for using AJAX with PHP.

Dumb Genius
05-06-2005, 07:58 PM
Originally posted by error404
No, by the time the javascript is executed the PHP is already completed. You can't intersperse them because one is running on the client and the other on the server; the communication is essentially one-way. The only way to do that would be to submit a form that then returns the desired results, or use more advanced Javascript to call PHP to get the data and then format it in JS.


True, you have to make sure to parse the JS 'dynamic' script before sending it to the client.

Hurga
05-06-2005, 09:59 PM
I'm learning C at the moment. About how fast would you think I could learn java/javascript? I also am looking to learn PHP as well. I know HTML already, so would that be pretty easy?

gbulmash
05-07-2005, 01:42 AM
Originally posted by Atomic Haven
Is there anyway to make this work?...

The way I got around this is to create a PHP script that fills in all the variables and then outputs the javascript.

<script language="javascript" source="myscript.php">

myscript.php basically is...

<?php

[php code that sets all the values of the variables]

$sendout = <<< EOM

[all your javascript code here, using PHP variables where needed]

EOM;

echo $sendout;

?>

If you need to customize this based on the page the user is viewing, you can always use the $_SERVER['HTTP_REFERER'] variable to determine which page is sending the request to myscript.php and customize based on that. Or if you need to pass variables, you can call "myscript.php?variable1&&variable2" then use $_SERVER['QUERY_STRING'] to get the variables.

- Greg

Dumb Genius
05-07-2005, 04:40 AM
Originally posted by Hurga
I'm learning C at the moment. About how fast would you think I could learn java/javascript? I also am looking to learn PHP as well. I know HTML already, so would that be pretty easy?

I leant C++ in college, I spend few days on HTML, PHP was a piece of cake to begin with. After it's like all programming language, it comes with experience and lot of practice.