Jump to content

onclick help


ev66

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>



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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.