Jump to content

Archived

This topic is now archived and is closed to further replies.

ev66

onclick help

Recommended Posts

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 [color=red]onClick=<?php $_SESSION['sum']=4; ?>[/color]  statement regardless. any ideas ? the code is below.
Thanks

[b]page1.php[/b]

[color=red]<?php
session_start();
if (!isset($_SESSION['sum'])) {$_SESSION['sum']=10;}
$sum = $_SESSION['sum'];
?>[/color]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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>
[color=red]<form id="form1" name="form1" method="post" action="">
  <label>
  <textarea name="textarea"><?php echo $sum ?></textarea>
  </label>
</form>[/color]
<p>&nbsp;</p>
[color=red]<form id="form2" name="form2" method="post" action="page1.php">
  <label>
  <input name="button1" type="submit" id="button1" [color=green]onClick=<?php $_SESSION['sum']=4; ?>[/color] value="Minus"/>
  </label>
</form>[/color]
[color=purple][color=orange]<form id="form3" name="form3" method="post" action="page2.php">
  <label>
  <input name="button2" type="submit" id="button2" value="go" />
  </label>
</form>[/color][/color]
<p>&nbsp;</p>
</body>
</html>


[b]page2.php[/b]

[color=red]<?php
session_start();
$sum = $_SESSION['sum'];?>[/color]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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>[color=red]<?php echo $sum ?>[/color]
</body>
</html>



Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.