I have a file which is of php type. And I have a combination of HTML elements, javascript functions and some PHP scripts as well. I want to rerun the php script say some part of the script again and again. lets take this example:
<html>
<?php
$connection = mysql_connect('localhost','root','root');
$db = mysql_select_db('messenger');
if ($db == null)
{
echo "hello";
}
$messagecheck = "select * from Messages where destination = '$user' && status = 'ACTIVE'";
$result = mysql_query($messagecheck);
$no_rows = mysql_num_rows($result);
............
?>
<body>
........
<form>
<input type="submit">
.......
</form>
</body>
</html>
I want to run the above php script continuously for every 1 min from the same file. When I make use of location.reload() I find that complete document gets reloaded. I just want the affect the part of the page which php script accesses and not the whole doc.
How can I do that? Please help.
You can't do that out of the box, since php is called when the page is loaded.
You should take a look at Ajax or frameworks like JQuery, that embed Ajax.
As previously said by blue112 you need to use Ajax in order to get what you want.
If I did understand well what you need, a simple solution is using an iframe where you point to a file with only the php code you want to execute, and reload it every time you want.
I hope this helps
Related
I have a database which contains the link to audio files. I am trying to play them one after the other using the HTML audio tag. Presently I am using PHP. My code -
$sql = "SELECT * FROM table";
$r = mysql_query($sql);
while($row = mysql_fetch_array($r)) {
$id = $row["audID"];
$audsq = "SELECT * FROM table2 WHERE id ='$id'";
$resultaud = mysql_query($audsq);
//Here I want to write a code to clear div="test"
PlayAud(resultaud);
sleep(2); ///Not Sure
}
I am passing the link to aud file for the html to load and in
<?php
PlayAud($res) {
$raud = mysql_fetch_array($res); ?>
<div id="test">
<audio> ... </audio>
</div>
<?php }?>
Can some one tell me if I am doing it right ? also each clip is of 2 seconds hence I need to wait in the while loop for 2 seconds. If there is any other way can you suggest me?
Thank You
You cannot clear a div from php. The reason is that php is running on the server, and the div is echoed out to the client, where it is unreachable by php.
A solution here could be to use Client side scripting (with JavaScript). You'd program a function, which replaces the audio tag or its contents, and it would probably be a good idea to implement some preloading-mechanism as well.
Greetings,
Jost
You can NOT do what you want in PHP. You will have to use javascript. This question will help you:
Update content of div auto
Hmm.... to clear to div you have to let the browser handle it, and you can do that through javascript.
echo "<script>document.getElementById('test').innerHTML = '';</script>";
//Then do what you needed to do
I'm trying to assign value of JavaScript variable to php session. Please see my code below -
<script type="text/javascript">
<?php $_SESSION['historyClass'] = "";?>
var myClass = $(this).attr("class");
if(myClass == 'trigger'){
<?php $_SESSION['historyClass'] = "trigger"; ?>
}
else{
<?php $_SESSION['historyClass'] = "trigger active"; ?>
}
alert('<?php echo $_SESSION['historyClass']; ?>')
</script>
In myClass variable, i'm getting 2 values
1) trigger
2) trigger active
Whatever the value I'll get, I want to store it in php session. But when I alert the session value it is always giving me "trigger active". It means it is always going to else part. I have checked the 'if' condition by alerting in it, the control is going properly in "If" and "else" part.
What is the problem? Am I doing something wrong?
Thanks in advance.
PHP is processed first, and then javascript is executed, so it's impossible to directly assign values to php variables.
instead you could send http requests from javascript (Ajax) to php scripts to save your data.
This won't work. PHP is executed on the server, and JS on the client. That means that the php is running before the JS is parsed. I suggest you look at Ajax if you want to run PHP from javascript (specifically the jQuery library and its ".get()" function).
What's happening is that the PHP is parsed, and doesn't see any JS, so it runs as normal, and sets the session to trigger, and then trigger active. Then javascript comes along on the client, and the client doesn't see any PHP code,so doesn't run it.
this wont work. the php code runs on serverside, js - on client. so php is executed before the page is shown to the user. it means that first is executed $_SESSION['historyClass'] = "trigger"; than $_SESSION['historyClass'] = "trigger active"; and obviously the value will be trigger active
yes you are doing something wrong.
javascript runs on client side and php on server side. so your code runs first on server side and then on the client.
thats why you can't do it like you did. a common way to transfer javascript data to a php script is, writing the value to a hidden field, so that it gets submitted to the server.
just create a hidden field and fill it with a value from javascript
<script type="text/javascript">
function valToPHP(name,value){
document.getElementById(name).value = value;
}
</script>
...
</head>
<body>
<input type="hidden" id="myField" name="myField" value="" />
...
you can then read it in your php script like that
$_GET["myField"] or $_POST["myField"]
this depends on your method of the form
I made a simple login using JavaScript, but record the username in a PHP session.It's a simple web chat, so I want when the chat page is loaded the user to be forced to pick an username, but after that I want to store that info ina a PHP session and if the page is reloaded for some reason to do a check if $_SESSION['UserName'] is empty and if it's not to stop the script from executing again.I put my login JS in <body onload..> and the code looks like this:
<body onload = "showUser(), showChat(), <?php if ($_SESSION['UserName']==""){
{
Login();
}?>">
I'm just learning now, so I gues I have some newb mistakes, like I'm almost sure that I'm not calling the Login() function right (it's JS function), but that's my strating point.Could anyone explain to me, how should I do the check properly and add the JS function in my PHP code?
P.S And I don't know if this really matter but if I remove the PHP code and leave liek this:
<body onload = "showUser(), showChat(),Login()">
My script is executed properly and I get everything that should be shown when the page is loaded, but when I add the PHP script and try to load the page I get blank page.I really wonder what's the reason for this too?
You cant call a js function from php. instead we have to print the js function as string in php so it will be executed as js along the html.
<body onload = "showUser(); showChat(); <?php if ($_SESSION['UserName']==""){ echo 'Login();'; }?>">
To Write efficient php code:
<body onload = "showUser(); showChat(); <?php echo (($_SESSION['UserName']=='')?'Login();':''); ?>">
You get blank page because of fatal error. which is occurred due to the javascript function you called in php (ERROR: Undefined function login() ).
You must echo the text Login(); from PHP - PHP does not execute Javascript, but you can use it to control which Javascript functions are called.
<body onload="showUser(), showChat(), <?php
if(isset(!$_SESSION['UserName']) || $_SESSION['UserName'] == ""){
echo 'Login();';
}?>">
You are effectively using PHP to create two different body tags - one for logged in users, and one for others.
Side note: it's good practice to check that an array item exists before attempting to access it: isset($_SESSION['UserName']) || $_SESSION['UserName'] checks first if $_SESSION['UserName'] has been set, then checks if it has been set to something other than an empty string.
You can simply call this as:
<?php
if($_SESSION['UserName'] == ""){
?>
<body onload="showUser(), showChat()">
<?php
}
else
{
?>
<body onload="showUser(), showChat(), Login();">
<?php
}
?>
I have a php script that is a bit of a mess and after a form entry, I need to get an address, and display it on a google map. The html and php is crammed into the same script so I essentially need to call the JavaScript as the PHP is happening. Is there a way to do this?
Thanks,
Alex
You can POST your from to a different frame (or iframe), so your page would not reload. The response of your PHP file which comes back to that frame can contain JavaScript code, which will be executed. Something like:
echo('<script type="text/javascript"> alert("Executed on client side"); </script>');
No, PHP executed by the server and returns the full response to the browser. JavaScript in the page is then executed by the client.
You can't call Javascript functions from PHP. You can set the Javascript to run when the page loads instead.
What you want is something like this:
<script type="text/javascript"></script>
var userAddress = "<?php echo $_POST['address']; ?>";
doSomethingWithAddress(userAddress);
</script>
If that code is on the page which you are POSTing the address to, it would take the address from the user, and write it into a javascript tag. The PHP will get executed first on the server, before building the HTML document. This new document has the variable available to the javascript.
I don't know how you would go about doing that, but this seems like a good place to start looking:
http://code.google.com/intl/en/
I want to display the photos according to the album selected. But, I don't want to post the page, I want to just change the div.
This is my script:
<script type="text/javascript">
function replaceContent(divName, contentS) {
document.getElementById(divName).innerHTML = <?php echo get_pictures_from_album($fb, $albums, contentS); ?>;
}
</script>
And this is the select tag that invokes it:
<select name="album" size= "1" style="width:210;" onchange="replaceContent('photos', this.options[this.selectedIndex].value);">
<?php get_albums_select_list($albums); ?>
</select>
<div id = "photos">
<?php echo get_profile_pictures($fb, $albums); ?>
</div>
I understand from a reading that I have done that the problem might be connected to javascript Vs php variable types.
Please advise.
Looks like you are looking for an AJax call to an PHP script that retrives the data for the appropriate album selected and THEN update the div with the callback function.
Ajax + PHP basics
You are mixing Clientside and Serverside Code here. The function replaceContent is called after the page (and the php code) was loaded. You would need an Ajax Call for that if you need more information about that:
Ajax Tutorials on Google
What you are doing is not possible because PHP code runs before (on the server because PHP is server-side language) javascript code.
You will have to resort ot AJAX for that.