Jump to content

Recommended Posts

Can anyone tell me why this Javascript isn't working? (The following is the PHP code:)

 

if ($quantity>$inventory) {
echo '<script language="javascript">
var i = confirm("There are ';
echo number_format($inventory);
echo ' copies in inventory in ';
echo $warehouse_name;
echo '.nnThis order is for ';
echo number_format($quantity);
echo ' copies.nnThat is ';
echo number_format($difference);
echo ' more than are available.nnClick OK to create a backorder for this itemnor Cancel to abort.");
if (!i) window.close();';
echo '</script>';
}
The idea is simple: A confirm box opens with a message. If the user clicks OK, the PHP code continues. If the user clicks Cancel, the window should close without executing any more PHP code. Right now, it doesn't matter which button you click: the PHP code just continues regardless.
How can I fix this?

Share this post


Link to post
Share on other sites

You need to use ajax requests to execute different parts of PHP at different times. All PHP code executes before any Javascript code starts. If you want to separate that then you need to use Javascript to send ajax requests to execute PHP code on the server.

Share this post


Link to post
Share on other sites

Great. One more thing to learn.

 

If all PHP code executes before javascript, though, why does the confirm box come up? That's executed before the rest of the PHP code. I don't understand why the javascript condition wouldn't also be executed before resuming with the PHP code.

 

Sorry for being such an amateur.

Share this post


Link to post
Share on other sites

When your browser sends a request to the web server for a PHP page, the server gets the request, sees that it is PHP, and will run PHP and send it the code to execute. PHP will execute the code and send the output to the web server, and the server will send the entire response back to the browser, then the connection between the browser and server closes. The browser will download the response, and then start doing things like rendering HTML or executing Javascript. So, by the time any of that Javascript code runs, PHP is out of the picture. The only job of PHP in this context is to produce output for the web server to send back to the browser. The browser doesn't know or care whether the response that it receives from the server was from a static file or something like PHP, in fact the browser doesn't understand PHP code at all. The browser just sees a bunch of HTML, Javascript, CSS, etc and does its thing. Javascript and PHP execute in different places (your computer and the web server) and at different times, they don't share any resources like CPU, memory, etc. They are completely separate. You can use them to exchange data with each other (ajax requests, form submissions, redirecting, cookies, etc), but otherwise they are separate.

why does the confirm box come up?

It's just Javascript. If you want to see what the browser sees, then after you pull up that page view the source code in the browser. You'll see the HTML and Javascript that PHP produced, you won't see any PHP code though. You could copy and paste that source code into a new static .html file, open that in the browser, and it will behave exactly the same way (for that page, anyway).

That's executed before the rest of the PHP code.

It's not, but things like confirm or alert boxes cause Javascript execution in the browser to pause until those dialogs are closed, so depending on what else is on the page, you're probably just seeing that pause in the browser. If that Javascript code is inline with a bunch of other HTML code, then the browser won't render the HTML that comes after the Javascript until the confirm box is closed. That doesn't have anything to do with PHP, that's just how browsers handle things like alert, confirm, and prompt.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...