Jump to content


Photo

onclick help


  • Please log in to reply
3 replies to this topic

#1 ev66

ev66
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 September 2006 - 06:54 PM

why is i that when i click button2 and browser goes to page2 to show the value of $_SESSION['sum'] it show it as 4. It should only be 4 if clicked button1. it seems to be running the onClick=<?php $_SESSION['sum']=4; ?>  statement regardless. any ideas ? the code is below.
Thanks

page1.php

<?php
session_start();
if (!isset($_SESSION['sum'])) {$_SESSION['sum']=10;}
$sum = $_SESSION['sum'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>
  <textarea name="textarea"><?php echo $sum ?></textarea>
  </label>
</form>

<p>&nbsp;</p>
<form id="form2" name="form2" method="post" action="page1.php">
  <label>
  <input name="button1" type="submit" id="button1" onClick=<?php $_SESSION['sum']=4; ?> value="Minus"/>
  </label>
</form>

<form id="form3" name="form3" method="post" action="page2.php">
  <label>
  <input name="button2" type="submit" id="button2" value="go" />
  </label>
</form>

<p>&nbsp;</p>
</body>
</html>


page2.php

<?php
session_start();
$sum = $_SESSION['sum'];?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body><?php echo $sum ?>
</body>
</html>





#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 September 2006 - 06:58 PM

You cannot run PHP code in onClick or any other on* atrributes within a html tag. PHP process the php code on a per request basis. You cannot treat PHP like you can with javascript.

In your code your sum session will allways be set to 4 regardless the user clicks the button1 button or not,

#3 ev66

ev66
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 September 2006 - 07:46 PM

ok. thanks.

#4 Jezdo

Jezdo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 September 2006 - 07:48 PM

But I think you can do something like this via Ajax.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users