View Full Version : php/javascript - js finds external php variable value every so often
woody8624 11-18-2005, 06:22 PM i tend to post my whold question in the title line. hmm...
php/javascript - js finds external php variable value every so often
i need to have a loop running that checks an external php file's variables.
in the js-
if (somePHPvariable == "something"){
do some kinda alert() or confirm() here.
}
anybody... have anything... ...?
sasha 11-18-2005, 10:05 PM if ("<?=$somePHPvariable;?>" == "something"){
maybe ?
localhost127 11-19-2005, 01:01 AM Am i missing something?
PHP is serverside, and as far as i remember javascript is clientside
sasha's post is the best solution i could think of but there is techincally no way to reference a php var from javascript without making the script generate the javascript code with the desired variable embedded.
woody8624 11-19-2005, 01:37 AM can a js call a php, execute it, and collect it's variables every so often?
i need the js to run a loop setTimeou and keep checking a MySQL query without reloading the page.
if num rows > 0 i want to alert the user with an alert or confirm message.
i don't know if that is any better explained...
localhost127 11-19-2005, 01:46 AM PHP is a server side language, meaning the page is generated on the server and then sent to the client.
Javascript is a cient side language, meaning the client's web browser interprets it and does whatever the code says.
Due to this there is no way for the javascript code to access php variables.
The best you could do is write the contents of the variables into the javascript section as sasha said.
I'm not a javascript expert so there might be a way for a javascript function to get the contents of a web page so you can interpret the variables that way, but it is somewhat unlikely.
Burhan 11-19-2005, 03:03 AM You can use embedded clients ie "AJAX" for this. Basically you write your Ajax code in javascript that calls your PHP script and then stores the results. Your PHP script will simply echo out whatever variable you want (this is the simple case scenario -- generally the script being called returns XML; however this would be overkill in your situation). So if you want your javascript to store the value of lets say some calculation, you simple echo the result in your PHP script:
<?php
$result = 2*7*9*18;
echo $result;
?>
You then call this PHP script internally using javascript. You may want to integrate more functionality as you see fit. Here is a very simple ajax framework that you can use:
<script type="text/javascript">
// -- Create the remote object
function createRO()
{
var ro;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer")
{
ro = new ActiveXObject("Microsoft.XMLHTTP");
} else {
ro = new XMLHttpRequest();
}
return ro;
}
var http = createRO();
// -- Sends a request
function sendRequest()
{
http.open('get','http://some.domain.com/script.php');
http.onreadystatechange = handleResponse;
http.send(null);
}
// -- Handles a response
function handleResponse()
{
if (http.readyState == 4)
{
var response = http.responseText;
alert(response);
// -- do other stuff with response
}
}
</script>
If you combine the javascript with the PHP script, you will get an alert with 2268 in it. You can then add a loop/timer and call the sendRequest() function at whatever interval you like.
Edit:
You also have a bad habit of starting two thread with the same question. Don't do this if you want to continue recieving my help.
woody8624 11-23-2005, 01:26 PM sry.
the last thread i had created has not yet been answered so i created a new thread asking the same question differently in hopes of getting new viewers and different responses.
Burhan 11-23-2005, 05:20 PM sry.
the last thread i had created has not yet been answered so i created a new thread asking the same question differently in hopes of getting new viewers and different responses.
Yes, this is my point. DO NOT DO THIS AGAIN.
woody8624 11-24-2005, 03:26 PM ill try not to...
im just wondering why its a bad thing
Dan L 11-24-2005, 03:59 PM It's bad since if no one answers your first, chances are they aren't going to answer the second. It's bad netiquette.
If you want to re-word your question, update the old thread, don't start a new one.
woody8624 11-24-2005, 04:04 PM oh.. good point taken :)
--
so, fyrestrtr, you're saying that im gonna need to know some ajax to do this?
you know of no php/js way to execute another php file on a setTimeOut() in js and send an alert if the php file... says it's okay.
woody8624 11-28-2005, 11:04 AM doesn't WHT use something like this to notify their users of new messages if there site is loaded in the current browser?
Burhan 11-28-2005, 03:07 PM No, but they use this to power the quick-reply functionality. If you note, new message popup only comes up when you refresh a page, not if you are just viewing a page.
So, if I sent you a pm now, and you didn't refresh your page, you would not see the popup.
woody8624 11-28-2005, 03:18 PM :(
that stinks...
i can't think of any simple way to tell a user that they have a new message without interupting what they are doing.
|