ev66 Posted September 2, 2006 Share Posted September 2, 2006 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]<?phpsession_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> </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> </p></body></html>[b]page2.php[/b][color=red]<?phpsession_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 https://forums.phpfreaks.com/topic/19496-onclick-help/ Share on other sites More sharing options...
wildteen88 Posted September 2, 2006 Share Posted September 2, 2006 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 https://forums.phpfreaks.com/topic/19496-onclick-help/#findComment-84749 Share on other sites More sharing options...
ev66 Posted September 2, 2006 Author Share Posted September 2, 2006 ok. thanks. Link to comment https://forums.phpfreaks.com/topic/19496-onclick-help/#findComment-84785 Share on other sites More sharing options...
Jezdo Posted September 2, 2006 Share Posted September 2, 2006 But I think you can do something like this via Ajax. Link to comment https://forums.phpfreaks.com/topic/19496-onclick-help/#findComment-84786 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.