kbios
asked on
Need Help with onUnload=js() in php script
I need to run a javascript function after some php code without using any onclick or submit button. I am trying to use the onUnload in the body of the php program but am not having any luck. Please view the snipet. Any help would be greatly appreciated.
Note: I am harcoding the localStorage variable for testing. The plan is to capture a php $var and use it in the javascript code.
<body onUnload="vload()">
<?php
... php stuff here ....
?>
<script type="text/javascript" language="javascript">
function vload(){
localStorage.vCtr="5";
}
</script>
</body>
Note: I am harcoding the localStorage variable for testing. The plan is to capture a php $var and use it in the javascript code.
<body onUnload="vload()">
<?php
... php stuff here ....
?>
<script type="text/javascript" language="javascript">
function vload(){
localStorage.vCtr="5";
}
</script>
</body>
If you want to run JS after the PHP has completed, why not just run it onLoad()? By that time PHP has already rendered the HTML.
Try
<html>
<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
localStorage.vCtr="5";
});
</script>
</head>
<body onUnload="vload()">
<?php
... php stuff here ....
?>
</body>
</html>
ASKER
Perhaps it's my linear thinking that is the problem. I'm somewhat new to JS and PHP. Within the php code I am writing data to a mysql table. Within the php code I will capture the record count of the number of items loaded ($vcount).
When the PHP code is finished I want to launch the JS to load the $vcount into a localStorage variable. I am doing this very thing in another PHP, BUT the JS is getting launched via a submit button. I'm looking for a way to automatically launch the JS.
When the PHP code is finished I want to launch the JS to load the $vcount into a localStorage variable. I am doing this very thing in another PHP, BUT the JS is getting launched via a submit button. I'm looking for a way to automatically launch the JS.
ASKER
sonawanekiran:
I tried the code but it didn't work.
Question: In this test I hardcoded the value. In your example would I be able to use the following?localStorage.vCt r = '<?php echo $vcount; ?>';
I want to put into localStorage a value from the PHP logic.
I tried the code but it didn't work.
Question: In this test I hardcoded the value. In your example would I be able to use the following?localStorage.vCt
I want to put into localStorage a value from the PHP logic.
See http://www.laprbass.com/RAY_temp_kbios.php
Follow the $num variable in this script. Is that something that helps with the design pattern?
Follow the $num variable in this script. Is that something that helps with the design pattern?
<?php // RAY_temp_kbios.php
error_reporting(E_ALL);
// SIMULATE GETTING A NUMBER OF RECORDS FROM A DATA BASE
$num = rand(3,9);
?>
<html>
<head>
<title>kbios</title>
<script type="text/javascript" language="javascript">
function vload(){
localStorage.vCtr="<?php echo $num; ?>";
alert("We got <?php echo $num; ?> records!");
return TRUE;
}
</script>
</head>
<body onLoad="vload()">
<h2>Hello World</h2>
<?php
echo "<p>This confirms that the number of records shown in the JS alert is really $num.</p>";
?>
</body>
</html>
ASKER
Ray:
I implemented your suggestions but it's still not working. Yes, $num helps, I am capturing $vcount that represents the number of records loaded. I had only hardcoded a vlaue for quick testing.
I am not getting any of the display messages to appear. I know the mysql code within the PHP is working because the table is getting updated but neither of the messages appear and the localStorage variable is still not getting set.
Any quick suggestions/ideas as to why the messages do not appear?
I implemented your suggestions but it's still not working. Yes, $num helps, I am capturing $vcount that represents the number of records loaded. I had only hardcoded a vlaue for quick testing.
I am not getting any of the display messages to appear. I know the mysql code within the PHP is working because the table is getting updated but neither of the messages appear and the localStorage variable is still not getting set.
Any quick suggestions/ideas as to why the messages do not appear?
Can you echo $vcount into a JS alert like on line 15 of the snippet above? If so you can see if the number is getting from PHP into the DOM
ASKER
Ray: No echo or alert works. Let me ask this, keep in mind I'm somewhat new to this, does it matter HOW the PHP is launched? I am using some AJAX code to pass data to the PHP in question. The code below is how the PHP is loaded and it's this PHP that I cannot get the display to work. Does this not work due to the asynchronous nature of AJAX?
Here is the code that loads the PHP. The PHP is loaded for every item in my shopping cart.
/* process localStorage items and create itemdtl*/
for( var i = 1; i<= val; i++)
{
if ( localStorage["item" + i] != "X" )
{
$.ajax({
type: "POST",
url: "sendcartitem.php",
data: "ukey="+ localStorage.ukey + "&item="+ localStorage["item" + i] +
"&showserver="+ localStorage.ShowServer,
success: function(){
$('#progress').remove();
$('body').append('<div id="progress">Cart Sent - Verify</div>');
$('body').append('<div id="rightbutton"><a href="javascript:vupload )">Verify</a></div>');
$('#leftnav').remove();
}
});
}
}
Here is the code that loads the PHP. The PHP is loaded for every item in my shopping cart.
/* process localStorage items and create itemdtl*/
for( var i = 1; i<= val; i++)
{
if ( localStorage["item" + i] != "X" )
{
$.ajax({
type: "POST",
url: "sendcartitem.php",
data: "ukey="+ localStorage.ukey + "&item="+ localStorage["item" + i] +
"&showserver="+ localStorage.ShowServer,
success: function(){
$('#progress').remove();
$('body').append('<div id="progress">Cart Sent - Verify</div>');
$('body').append('<div id="rightbutton"><a href="javascript:vupload )">Verify</a></div>');
$('#leftnav').remove();
}
});
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ray:
I appreciate the time taken to offer the detailed explanation. Parts of it make sense and other parts need to be digested more thoroughly. I will need to read and try some testing.
As I mentioned earlier I'm calling JS from PHP and writing to localStorage from a different PHP. The only differences that I see are a) the JS is being launched via a submit button and b) the PHP is not being called via ajax. One or both of those things are not allowing this to work. I'm going to have to rethink my approach. I was trying to go this route to eliminate a user step.
Anyway, Thanks for sharing your expertise.
I appreciate the time taken to offer the detailed explanation. Parts of it make sense and other parts need to be digested more thoroughly. I will need to read and try some testing.
As I mentioned earlier I'm calling JS from PHP and writing to localStorage from a different PHP. The only differences that I see are a) the JS is being launched via a submit button and b) the PHP is not being called via ajax. One or both of those things are not allowing this to work. I'm going to have to rethink my approach. I was trying to go this route to eliminate a user step.
Anyway, Thanks for sharing your expertise.
Thanks for the points. Are you using jQuery? If not, have a look. It implements many of the good parts of JavaScript.
http://docs.jquery.com/Tutorials
All the best, ~Ray
http://docs.jquery.com/Tutorials
All the best, ~Ray