开发者

Setting Session Variable from Form

开发者 https://www.devze.com 2023-01-18 02:34 出处:网络
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\">

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 :-)


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");


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">


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消