views:

118

answers:

3
if ($page_title->exists()) {
//the rest of the code is in php.here i insert a javascript for the confirm box.
               echo'<script type= "text/javascript" >
var b=confirm("This page already exists.would you want to edit");
if(b == true)
//here i want to assign a php variable , say for eg. $a to 1 so that i can use that value of $a in an if clause outside this javascript code.
</script>';
}

if($a==1)
..some code..

How do I do it?

+1  A: 

you can send the result back to php using an ajax call, e.g.:

<script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">
x = confirm('Page exists, would you like to edit it?)
$.post('script.php', {x: x}, function(data) {
  if (data == '1') {
    alert('ok');
  } else {
    alert('not ok');
  }
});
</script>
jspcal
Out of curiosity, because I have never used that method. How portable is it across browsers and browser versions?
Fredrik
jquery will work in majority of browsers with js enabled.
jspcal
Oh, and by the way... Unless there is some magic done inside that get(). Normally using GET instead of POST introduces a risk of hitting the cache instead of the server.
Fredrik
@jspcal: ah, of course. I didn't realize it was jquery. Good thing you told not just me but the OP.
Fredrik
+1  A: 

The PHP code is executed on the server side and is all done at the point where your javascript is executed on the client side.

To communicate the value from the client to the server you typically either use AJAX (tutorial on how to do it here: http://www.w3schools.com/ajax/ajax_httprequest.asp) or store the value in a form and submit it together with data you are going to send down later anyway.

Good luck

Fredrik
@jspcal: the "submit a form later" was just an example of how it can be done if the data is not needed directly. Learning how to use the basics (in this case XHR) that JQuery and and others are built on top of is good because it helps you understand what is going on. People who doesn't understand the concepts but just how to use a library tend to comment things with "doesn't work" or "can't be done" just because the library is all they understand. It is also good to know how to use the underlying mechanism when bringing in jquery would just be overkill.
Fredrik
@jspcal: And by the way, I didn't intend to serve him a solution. Just to tell him about php being server side and js client side (in this case) and the problem with that. The rest was just friendly pointers in the right direction (even if you disagree).
Fredrik
@Fredrik, i agree this is a great solution!
jspcal
+1  A: 

Javascript runs on the client (browser) and PHP on your server. So you have to transport the variable to the server.

Most easy way to do this is to use the URL to send this information, for example: http://example.com/myscript.php?a=1. Then you can grab a using $_REQUEST['a'] and use it.

A more sophisticated (and complex) method is to use AJAX to send the variable to your PHP script, so it can return some data which you can use to modify your page.

What you need depends on your application, so you should provide some more details if you do not know what method is best to you in your situation.

Veger
@jspcal: Veger didn't say redirect. requesting the URL could be done in lots of other ways.
Fredrik
@jspcal: yes it can. One way is to use a http request a'la Ajax, another way (which I don't recommend but hey, it is possible) is to create an Image (or something else with a src) with the src pointing to the script. Point is, it can be done. Some ways are good, others are just ugly but that is not the point.
Fredrik
@vegar..can u tell me how exactly will i use the URL..i mean the syntax or whatever..and whether i will do tht inside the javascript code or in the php code.
shashank
I believe that http://stackoverflow.com/questions/2122529/assigning-the-result-of-a-javascript-confirm-box-to-a-php-variable/2122867#2122867 (answer to a very related question you asked) shows an excellent example how to implement this.
Veger