My question is simply if i have a javascript function onload like so
<body onload="myFunction()">
will this function be executed BEFORE the PHP in the rest of the body ?
No.
onload doesn't get executed until the entire document, including images, has been rendered
PHP runs on the server (before the browser receives the file)
It is possible for some of the output of a PHP script to arrive in the browser before the entire script has finished executing, and you could even set things up so that the HTTP response finishes before the script has finished — but that takes some complicated jiggery pokey, is rarely useful, and you should know if you've gone to such lengths.
The function itself explains it "onLoad", so when your page is loaded ( including PHP ), your function then will be executed.
Related
I know PHP runs first but is there a way to get PHP to wait on an ajax request and then run its script? I have a php script here that I want to run but I NEED a variable from my JS file in order for it to run successfully. So was wondering if it's possible?
What I have is a normal request in my JS:
var myvar = data;
$.get('phpscript.php', {myvar: myvar} );
And in PHP:
$myphp = $_GET['myvar'];
But if i echo $myphp it returns "undefined", if I alert it however It displays the value; which means the php script is running before it even gets the request from ajax. Any way I could make the PHP wait?
Thanks.
Put the PHP that requires a variable in its own script and call it from the ajax call, once the ajax call gets a response update the DOM as needed.
PHP runs on server, then javascript runs on client to make the ajax call, then PHP runs on server returning data, then the javascript gets the data and does something with it.
$.get('phpscript.php', {myvar: myvar}, function(data) {
$('.result').html(data);
});
Inside the php file have something like:
$myphp = $_GET['myvar'];
echo $myphp;
The short answer is, no, you can't make PHP wait. PHP only runs on the server-side, by the time the AJAX request is sent, by definition, the page is already been sent to the client.
You'll probably have to do some refactoring. If the variable absolutely needs to be used for a PHP function, then you may need to move that logic into 'phpscript.php' or (less optimally) you may need to issue another AJAX request when you get the response from the first.
But my guess is that more commonly, you'll probably just have to figure out how to do what you want with javascript. If all you want is something equivalent to a PHP echo, you'll want to use Javascript (or JQuery) DOM manipulation for that.
EDIT: I forgot to mention, the other option is simply to do all the PHP stuff on the server-side before you send the page at all, instead of AJAX you'd want to do something in PHP like including your other php script and calling methods from it. But, everything you do on the server-side, the user is sitting there looking at a blank screen waiting for the page to load. So this isn't an option for anything that's not very quick.
I'm using the following code to get the screen resolution.
This works fine with WAMP server but when I put it on the remote Linux server, $height returns 0 .
HTML/PHP
<head>
....
<script language="javascript">
var y=window.screen.availHeight;
var x=window.screen.availWidth;
window.location.href = "index.php?height=" + y + "&width=" + x;
break;
</head>
<?php
$height=$_GET['height'];
$height=intval($height);
echo($height);
?>
You may want to explain what you are trying to do with this webpage, give an overall explanation of passing in the height to php, because this page is not working at all.
This is the execution flow that occurs with this web page.
User enters index.php in address bar.
PHP Server handles the request.
The $_GET array will not contain a height parameter.
$height will contain no value.
calling intval($height) when $height is empty will return 0.
The page will load, 0 will be displayed.
JavaScript is executed.
The URL will be constructed with the height and width parameters.
Browser will throw an error: Uncaught SyntaxError: Illegal break statement
Execution ends.
PHP is server side. It receives a request from your browser and renders the HTML/CSS/Javascript output before sending it back to your browser. Thus, any information about your browser size or screen size needs to be sent to the server as some kind of request, either when you first open the page, or after you've loaded the page. Sending a request to the server after you've loaded a page is called AJAX, and that's most probably how you'll have to do it.
http://w3schools.com/ajax/default.asp
You can do this via AJAX, but you will need to send the data to a different PHP file and do the processing there (which should work out alright, since PHP is processed on the server and not on your computer.
AJAX works like this:
In javascript/jQuery, put some value into a variable
Create an AJAX code block (I recommend using the $.ajax() structure at first)
In the AJAX code block, send the var-value pair to the PHP file
PHP file receives the variable-value, and does something with it
PHP can return some data (usually a string of HTML, like a table, etc)
Data from PHP is received in the SUCCESS function of the AJAX code block
Inside SUCCESS function, you can inject received code into the DOM
Here are a couple of examples (it's actually a LOT easier than it sounds/appears at first glance):
Sending value of dropdown to PHP page
Another basic AJAX example
I the following code:
<? $test = new com("Soundclass.Soundrec"); ?>
<? $test->startrec ?>
<script>
function stop(){
var stop_record = "<?=$test->stoprec;?>";
}
</script>
and I am running the stop function in a button click. But the php function seems to run without the click.
The purpose of this is to stop the recording on that button click.
You have a little logical error:
PHP is server-side and gets executed on the server when the document is called.
Javascript is client-side and gets called dynamically, it can't re-execute any php-code again.
See the source code in your browser and you'll understand!
But you could make an ajax call to the class and execute the stoprec() method with a (defined in your script) get/post variable. But as this still won't give you a handle on the same instance of the object, so unfortunately you probably have to rethink your whole script!
What you are trying to do is fundamentally impossible: You are mixing up PHP (which runs on server side) and JavaScript (which runs on client side).
You would have to build a second PHP script that stops the recording, and have that called from JavaScript using Ajax.
Calling $test->stoprec will probably stop recording (don't know Soundclass.Soundrec that specifically myself).
As you call it even before the page has been delivered, pressing the stop button later won't make a change here.
You need to execute the PHP later (after button click), however you have the general problem here that the com object in $test won't survive that (it would be a new one).
The only solution I see here is that you create a daemon that manages sound recording for you. This would work with AJAX, but is not trivial. So the short (and sad) answer is: Not easily possible.
For example:
$(document).ready(function(){
$('.selector').click(function(){
<?php
// php code goes here
?>
});
});
Will this cause issues or slow down the page? Is this bad practice? Is there anything important that I should know related to this?
Thanks!
If you are trying to bound some PHP code with the click event then this is impossible in the way you are trying and PHP code will be executed as soon as page load without waiting for a click event.
If you are trying to generate final javascript or jquery code using PHP then this is okay.
It won't slow down the page; the PHP runs on the server and emits text which is sent to the browser, as on any PHP page. Is it bad practice? I wouldn't say "bad" necessarily, but not great. It makes for messy code - in the event where I need to do something like this, I usually try to break it up, as in:
<script>
var stuff = <?php print $stuff; ?>;
var blah = "<?php print $blah; ?>";
// Do things in JS with stuff and blah here, no more PHP mixed in
</script>
PHP is executed on the server, and then the javascript will be executed on the client. So what you'd be doing here is using php to generate javascript that will become the function body. If that's what you were trying to do then there's nothing wrong with doing it.
If you thought you were going to invoke some PHP code from javascript, then you're on the wrong track. You'd need to put the PHP code in a separate page and use an ajax request to get the result.
Sure, as long as you keep in mind that PHP code will be executed by the server before the page is sent out. Other than that, have fun.
PHP is a "backend" language and javascript is a "frontend" language. In short, as long as the PHP code is loaded through a web server that understands PHP - the downside is that you have to inline the JS, losing caching ability (there are workarounds to parse php in .js files but you shouldn't really do this). To the user it will just look like javascript and HTML. Here's the server order:
User requests page.
Apache (or equivalent) notices this
is a php file. It then renders all
the php that are between php tags.
Apache sends the page to the user.
User's browser sees the JavaScript
and executes it.
Just be sure the PHP is outputting valid JavaScript.
you have a better choice to use ajax that runs the php script when you are handling a click event
$(document).ready(function(){
$('.selector').click(function(){
$.ajax({url:"phpfile.php",type:"POST",
data:"datastring="+value+"&datastring2="othervalue,
,success:function(data){
//get the result from the php file after it's executed on server
}
});
});
});
No it's not. Just as long as you know that the JS is executed after the PHP page is parsed.
is it possible to prevent further execution of javascript?
I include some javascript scripts with php in a header (with echo ''), but there are coming some other scripts later in the page which i can not always control, so it could be that my before included (with ) mootools javascript get later overwritten by another included mootools (which then possible is an older version, or is not complete etc.)
so is there a way that I can stop the js at one point so that later js code will not be executed?
kind of like the die(); function in php, but without that it stops the page from being loaded.
doesnt really exist.
but you can put everything into a function and "return;" any given time to exit the function which would stop the execution of the rest of the code within the function.
super simple example in standard JS:
function init() {
if(something happens) {
return;
}
}
init();
in terms of stopping the browser from executing other scripts within the page - not possible.