2

Is it possible to set a session variable from a radio button? This is what I have so far:

<form action="process.php" method"post">
<input type="radio" name="number" id="number" value="1" /> 1 
<input type="radio" name="number" id="number" value="2" /> 2 
<input type="radio" name="number" id="number" value="3" /> 3 <br />
<input type="submit" name="Submit" value="Submit" />
</form>

And then for my process.php:

<?php 
session_start();
session_register ("number");
$_SESSION['number'] = $_POST['number'];
echo "Number = ". $_SESSION['number'];
?> 

I have the echo "Number = " in there just to test and make sure the variable is being set, but it still seems to be coming back blank.

I am really confused. Thank you for your help :-)

3 Answers 3

4

You are missing = in here:

method"post"

Should be:

method="post"

Note

You have a couple of more problems:

The id should always be unique per element per page

<input type="radio" name="number" id="number1" value="1" /> 1 
<input type="radio" name="number" id="number2" value="2" /> 2 
<input type="radio" name="number" id="number3" value="3" /> 3 <br />

Avoid using session_register function because it is deprecated, eg remove below line:

session_register ("number");
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to get a session variable from the unique id? E.g. $_SESSION['number1'];?
@Amyunimus: what do you mean by unique id here? As I see, number1, number2 ... are in html form
Does $_SESSION[variable] always grab the value by the name of the input, or would it be possible to get the value from the id of the input (when it differs from name, as in your example.)?
1

session_register is deprecated so no need to use it.

just

<?php 
session_start();
$_SESSION['number'] = $_POST['number'];
echo "Number = ". $_SESSION['number'];
?>

should do the trick.

also your form action is messed up:

<form action="process.php" method"post">

should be

<form action="process.php" method="post">

Comments

0

method"post" should be method="post". This is why it sends as a GET request, therefore $_POST['number'] will be undefined.

And yes, do not use session_register().

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.