最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - JavaScript: If clause partially executed - Stack Overflow

programmeradmin0浏览0评论

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'";

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'";
Share Improve this question edited Jun 7, 2010 at 16:00 arwa asked Jun 7, 2010 at 15:44 arwaarwa 311 silver badge3 bronze badges 4
  • 4 What exactly were you trying to acplish by wrapping your code with <blink> tags? – Dolph Commented Jun 7, 2010 at 15:55
  • What are they teaching the kids these days.. :) – Sean Kinsey Commented Jun 7, 2010 at 16:01
  • 1 @Dolph I'm adding a request on Meta to allow blink tags in code blocks, how awesome would that be :) – Neil Aitken Commented Jun 7, 2010 at 16:02
  • @Dolph nothing actually.. just coz I'm new I guess :) appreciate the help everyone .. – arwa Commented Jun 7, 2010 at 16:55
Add a ment  | 

4 Answers 4

Reset to default 7

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 municate 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)

  1. 暂无评论