0

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'...

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Basically I will need to identify where this form will be directed (sales, engineering, or production). So between the two pages I will need to pass the variable. I think my problem is that i don't know what to put between the brackets [' **** '] because everything I'm reading php will only allow a specific name and not necessarily the name of a selected item.
@Sanya is the selection actually needed to be known on the first page? Or does only dpuform.php need to know which is selected? If the latter (which is what I think you need), use my example code
the latter. So would the solution be $rep = $_POST['submit'] ?
@Sanya No, because none of your inputs have a name of "submit", that's the type. See my code - that will detect which button was clicked.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.