Reputation: 1280
I am very very new to php and have been running through some tutorials. I have a simple page with three buttons. When the user clicks a button, I want to store the name of the button in a session. This is what i have:
<?php
session_start();
$rep = $_SESSION[' *** selected button name ***'];
?>
<body>
<form method='post' action='dpuform.php' target='_blank'>
<input type='submit' name='sales' value='Sales'/><br/>
<input type='submit' name='engineering' value='Engineering'/><br/>
<input type='submit' name='production' value='Production'/><br/>
</form>
</body>
And then I'll need to retrieve this value on the next page 'dpuform.php'...
Upvotes: 0
Views: 861
Reputation: 1947
You can't actually store the button clicked in the session variable from the same page, because that is client-side code. There are alternative methods (1. ajax, 2. add to session variable on the page the form submits to using the $_POST
value, and...), the easiest being the following: If you only need to access the button clicked from "dpuform.php" than you can just use $_POST
variable in that page to get the selected button's value.
dpuform.php
if(isset($_POST['sales'])){
//sales button
} else if(isset($_POST['engineering'])){
//sales button
} else if(isset($_POST['production'])){
//sales button
} else{
//error handling
}
Upvotes: 2
Reputation: 1275
The base problem you'll face with this task is that HTML runs on the client side (in the browser) and the PHP code runs on the server side. This means, that in order to store anything in the session, you will actually need to transmit the data to the server side, for example with a form submission. So the HTML code gets send to the browser, there the user fills it out and submits it back to the server. Then you can store the data, not before. This means that there's no "direct" way to connect the PHP code to the HTML, you have to take the submitted data out of the request variables ($_GET, $_POST, etc) and handle it separately.
Upvotes: 1