which one is executed first - php

I have a web application in php, I don't know which one will be ever called an processed first,
In my php file, I also have a javascript code
<script>
$(document).ready(function(){});
</script>
In my php code, I will send an array object into that javascript. So I wonder why the php code is executed first instead of the javascript ? Does it mean that all server code will always be executed before the client script runs during the browser view is shown.

The server side code is executed first and the output generated by server side is sent back to client where client side code is executed.

Yes, normally, the whole server processing finished before the page is delivered to the browser. At this moment, JavaScript execution starts.
You can add late execution of PHP code using AJAX.

Of course, the server code (be it PHP,ASP,JSP,etc) runs first in the server, it generates an html page which contains your javascript code, your computer receives this page, renders it in the browser and runs the javascript in it.

if you have html,php,js in a single file you should know these two things only:
1)your file extension must be .php (because php execution required .php extension)
2)you will see the output exactly in the same order as you typed in your file.
<html>
<body>
<?php
echo "i m php upper"."</br>";
?>
<p id="pg">i am html upper</br></p>
<script type="text/javascript">
document.write(" i am javascript upper</br>");
</script>
<?php
echo "i m php bottem"."</br>";
?>
<script type="text/javascript">
document.write(" i am javascript bottom</br>");
</script>
<p id="pg">i am html bottem</br></p>
</body>
</html>
...output looks like this...
i m php upper
i am html upper
i am javascript upper
i m php bottem
i am javascript bottom
i am html bottem

Related

Run Qt generated executable (EXE) through php (LINUX)

Sorry, this question must be repeated, it already asked the following link, but the answer is not cleared.. So can anyone please solve this problem???
exe not giving output in php
I am trying to call a Qt generated executable file through PHP code which fails to run the exe.. The same exe runs on double-click and through command line..
Below is my code
<?php
$exec_cmd = exec('"./myEXE"');
?>
<html>
<body>
<form>
<input type="submit" value="RUN" onclick="$exec_cmd"/>
</form>
</body>
<html>
Thank You...
PHP is a server side language. It means that the php code is compiled and executed in the server and the output is sent to the client.
What you are trying to do is to use PHP as client-side language, like JavaScript.
The porgram is executed when the page is displayed and you want it to be displayed once the user clicks on the button, for that you must use JavaScript.
I would use AJAX to make the request to the server so it can execute the program but if you don't want to use javascript you can set the form to redirect the page with a GET or POST parameter and check in PHP if that parameter is set

php shortcode into jquery

I have a Wordpress based website, and some of the content is loaded through javascript.
For example:
jQuery(".portfolio-fs-slides").css({"display":"none"}).prepend('<div class="portfolio-fs- slide current-slide portfolio-ppreview"><div class="project-coverslide"></div><div id="content" class="content-projectc contenttextwhite"></div></div>');
What I want to do is append this shortcode: <?php echo do_shortcode('[daisy]'); ?>
But as far as I know is not really possible to append php code in javascript.
Is there any workaround to accomplish this ?
Thanks!
As #Bergi mentioned, the PHP will run serverside, and the JS will run client side. You can output JS (or parts of your JS) via PHP and have it run client side, e.g.
<script>
// extra code here
jQuery(".portfolio-fs-slides").css({"display":"none"}).prepend('<div class="portfolio-fs- slide current-slide portfolio-ppreview"><div class="project-coverslide"></div><div id="content" class="content-projectc contenttextwhite"></div></div>');
<?php echo 'we can output valid js here' ?>
// more code here
</script>
One way to think of this is that since PHP runs server side, it will always run before the JS is parsed.
Put another way, you could have a javascript line like this:
console.log(<?php echo $someVariable ?>);

How do I use jQuery to insert PHP tags through the DOM?

Here is what I'm trying to do...
$("div").prepend("<div id='comment-number'><?php the_comment_number(); ?></div>");
Is there some way to get this to work?
<div class="gallery-item">
<div class="comment-number"><!--?php echo htmlspecialchars(the_comment_number()); ?--></div>
</span>
<span class="gallery-icon">
<img src="http://mysite.com/test/wp-content/uploads/2011/06/fff-150x150.gif">
</span>
</div>
PHP is executed on the server, but JavaScript code (jQuery) is executed later, in the web browser. For that reason, PHP can produce JavaScript code, but JavaScript can't produce PHP code.
The <!--? in your posted output shows that something is filtering our your PHP code. So the problem isn't your PHP code, it's that you're not actually executing PHP code. If it's a .js file, PHP almost certainly can't be included.
If PHP were being evaluated (ex. if this were in a <script> tag in a .php file), this should produce valid JavaScript code that will do what you want:
$("div").prepend("<div id='comment-number'><?php echo htmlspecialchars(the_comment_number()); ?></div>");
1) php is SERVER side scripting
2) javascript is CLIENT side scripting (generally)
so this is what happens:
1) User opens up your page http://example/
2) Your CLIENT sends GET request to http://example/ server
3) Apache (given you run on it) captures the request, based on the server config approaches index.php (index.html, etc). If php is installed, your index.php will be parsed by mod_php module
<<<< this is where SERVER side scripting is activated
4) outcome of the parsing of index.php will be then transferred back to CLIENT
5) CLIENT will digest the outcome received by SERVER
6) If there are javascript calls, those are executed either immediately OR when document is loaded (based on approach)
That's it. Here normal request life ends.
NOW if you want your page to dynamically update some parts of it, here is how you do that:
1) Usually to update your page dynamically, you would use AJAX approach. When AJAX request is created, 2-7 happens again, but this time the caller is your ajax process and information which is received is sent back to that process to decided what to do with it.
Okay, some coding:
1) index.php
<!-- include jquery, etc //-->
<div id="comments"></div>
<script>
function fetch_comments(){
$.get("ajax.php", null, function(data)){
// this is called, when response from SERVER is received
$("#comments").prepend(data);
setTimeout("fetch_comments", 5000); // fetch again in 5 seconds
}
}
$(document).ready({
fetch_comments();
});
</script>
2) ajax.php
<?php
//fetch comments, return them for CLIENT
echo "<p>Comment on " . date("Y-m-d H:i:s") . "<br />Lorem Ipsum</p>";
This should help you understand the whole process. Did not test the code, but should be quite ok.
do a .ajax() query to PHP script that will provide you value of the_comment_number(); and put result to comment-number by $("#comment-number").prepend(result); on success event in ajax query.
Remebmer that PHP script have to have connection to database and pass to it all variables you need (like entity id, article id, page etc.). You can do it by GET or POST.
Request is sended by browser so session/cookies will be the same unless you changed it in current request.
PHP is executed on the server side so you cannot call it from javascript
You can try something like this which will render once the page loads
$("div").prepend("<div id='comment-number'>"+ <?php the_comment_number(); ?> +"</div>");
Couldn't you just add the value directly to the template instead of using javascriot? eg:
<div class="gallery-item">
<div class="comment-number"><?php echo (the_comment_number());?></div>
...
</div>
Also you have a </span> tag with out matching <span> tag in your example.
As already told, you can't produce or call php code from javascript directly(you need to make an ajax call or form submit). You need to make ajax call using jquery to fetch the comment number and then update it into div.
However, you may want to look at this library - http://www.phplivex.com/ .It may help you in doing things your way. It allows you to call user defined php functions in javascript using AJAX internally.
Reading through this disccussion and from what i understand you want to acheive.. You gotta figure how your page is served. If it is an .php file that is serving the content, then you wont need Javascript at all and could get your function call to work by adding the function between the div as so..
<div class="comment-number"><?php echo htmlspecialchars(the_comment_number()); ?></div>
Assuming you don't have access to the .php or if its a .html/htm page that serves the content then your only bet would be to use ajax. That is make an ajax call to a php file(on the same domain) that makes your function call and echos the comment no. The Ajax will retrieve the echo'd comment no. which you can append/prepend to the desired

PHP tags in javascript tags

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

Calling a JavaScript function from 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/

Categories