开发者

JavaScript: If clause partially executed

My problem is that when the condition is true it will close the window but not execute the php part !

here goes my code..

<script type="text/javascript">  
function onClose()  
{  
var r=confirm("Is the meeting Over!");  
if (r==true)
 {  
<?php $result=mysql_query($sql);?>  
window.close();  
 }  
else  
 {  
<?php $result2=mysql_query($sql2);?>  开发者_运维技巧  
 }  
}  
</script>  

this is the php part..

$sql="UPDATE previousmeetings SET Live='0' WHERE MeetingID='34'"; //$meeting_id  

$sql2="UPDATE previousmeetings SET Live='1' WHERE MeetingID='34'";


Javascript is executed on the client side, PHP is executed on the server. The PHP has already been executed when your Javascript is run.

If you want the Javascript to execute functions on the server, then you need to use Ajax to make calls to a PHP script that will do your server side operations for you.

The way your code is constructed now, you're doing this:

Client Request -> Execute PHP Code (Both queries) -> Browser parses HTML and Javascript -> Javascript is executed.

What you want to do is set up another PHP script that your Javascript can make calls to. That way you can get the order of operations you want:

Client Request -> Browser parses HTML and Javascript -> Javascript is executed.
time passes if desired
Client Takes Action -> Javascript makes Ajax request to PHP script -> Appropriate query is executed.


You are trying to execute PHP code inside a javascript block. PHP is server side, JS is client side so this wont work.

If you need to call a PHP script via JS you can use an AJAX call, a library such as jQuery or Prototype will make this easier.


All PHP code is executed on the server, before being sent to the client (browser) to be displayed. You cannot conditionally execute PHP through Javascript like that. However, you can use AJAX communicate with the server from Javascript.

For example (this is pseudocode):

if (r==true)
{
  ajax_request("perform query 1");
  window.close();
}
else
{
  ajax_request("perform query 2");
}

All Javascript framework libraries (Prototype, jQuery, etc.) provide AJAX functionality.


The PHP code executes when the page is served, it isn't affected by the Javascript conditional which executes after your browser loads it.

You want an AJAX call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜