0

i am using javascript confirm box with a submit button that calls a javascript function as shown.Inside the funcion,i am setting a PHP Session variable(i don't know whether its possible to do so).i.e.

<input type="submit" name="delete" value="Delete Album" onclick="**cdelete()**">

The javscript function is:

<script type="text/javascript">
function cdelete(){
var f=confirm("Are you sure you want to delete this Album?");
if(f==true)
{
    <?php $_SESSION['temp']=25;?>
}
else if(f == false)
{
    <?php $_SESSION['temp']=45;?>
}
}
</script>

But the problem is that the session variable always takes the value 45.i.e the value when f==false even if i press ok button i.e. even if the confirm box returns f as true.

Any help??

Ankur
  • 269
  • 2
  • 4
  • 15
  • 1
    You can't do it that way. You should use _Ajax_. – Ram Jan 25 '14 at 06:53
  • if its not possible,then why does it take the second value only...it should be null then?confused therefore. – Ankur Jan 25 '14 at 06:55
  • Imagine the code as if there's no Javascript around it (since that's what PHP is doing) and you're left with 2 assignments of `$_SESSION['temp']` and only the second will stay because it overrides the first. – Deryck Jan 25 '14 at 06:56
  • `$_SESSION['temp']=25; $_SESSION['temp']=45;` You are overriding the first set value before clicking on the element. Your JavaScript doesn't do anything. Seeing _page source_ clarifies it better. – Ram Jan 25 '14 at 07:02

5 Answers5

0

PHP is a server side language. You are going to have to use Asynchronous Javascript And Xml also known as AJAX to execute PHP code. jQuery has a really easy to use AJAX request function. You should check it out. http://api.jquery.com/jquery.ajax/

0

PHP is parsed before Javascript so the Javascript logic you're trying to use isn't even seen by PHP.

You can use cookies for this situation if you'd like to access from both Javascript and PHP but anything changed in real-time in the browser will have to be sent asynchronously using AJAX to be registered at the server-side before loading the next page.

Deryck
  • 7,608
  • 2
  • 24
  • 43
0

The issue is that PHP code is run server-side, while JavaScript is run client-side. In other words, the PHP is executed on the web server and the resulting web page (without any PHP left in it) is then passed to the browser.

What's happening is that all of the PHP in your document is parsed and executed, and then the resulting webpage is sent to the client's web browser, where the JavaScript runs. So, your two lines of PHP are executing on the server (first setting $_SESSION['temp'] to 25, and then to 45), and then the page (without any PHP left in it) is passed on to the client. If you view the page source in your web browser, you'll notice that the PHP code isn't there and the bodies of the conditionals are empty.

There are several ways to do what you're interested in doing; AJAX would be a good option.

mculhane
  • 923
  • 7
  • 7
0

PHP is a server side script. Where Javasript is client side.

Your PHP code is executed before anything else.

So, looking technically, this is how your code gets executed:

<?php
   $_SESSION['temp'] = 25;
   $_SESSION['temp'] = 45;
?>

<script type="text/javascript">
function cdelete(){
   var f=confirm("Are you sure you want to delete this Album?");
   if(f==true) { }
   else if(f == false) { }
}
</script>

You'll need to implement AJAX to set the session based on JS. Call a php script via AJAX which will set your session variables.

Tzar
  • 1,761
  • 2
  • 14
  • 21
0

All the php variables are set on the server side. So in order to set a php variable, you will have to make a request to server (may be using ajax). You can access the value of php variables (that has already been set) in the following way:

<?php
    $simple = 'simple string';
    $complex = array('more', 'complex', 'object', array('foo', 'bar'));
?>
<script type="text/javascript">
    var simple = '<?php echo $simple; ?>';
    var complex = <?php echo json_encode($complex); ?>;
</script>
Community
  • 1
  • 1
MjZac
  • 3,476
  • 1
  • 17
  • 28