admin管理员组文章数量:1426795
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
4 Answers
Reset to default 7Javascript 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.
本文标签: phpJavaScript If clause partially executedStack Overflow
版权声明:本文标题:php - JavaScript: If clause partially executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477311a2660018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论