Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

php - How to store a radio button in a session for use on another page?

So I need to store the choice of selected radio button in session and then based on that value perform an action on a different page.

Page1.php:

 <input type="radio" name="person" value="p1"/>Person1
 <input type="radio" name="person" value="p2"/>Person2

Page2.php

if Person1 is selected on page one
  //do this
if Person2 is selected one page two
  //do this
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you will have to start a session in Page2.php

start by calling (at the top of your page)

 <?php
   session_start();

   //post your data
   $person = $_POST['person'];



   $_SESSION['person'] = $person;
 ?>

You will have to have the inputs be wrapped inside of a form:

 <form method="post" action="Page2.php">
   <input type="radio" name="person" value="p1"/>Person1
   <input type="radio" name="person" value="p2"/>Person2      
   <input type="submit" value="submit"/>

 </form>

Now you will be able to use that $_SESSION variable on mostly any page, if the session is not destroyed or overwritten.

To retrieve a session value on another page, simply use:

 <?php
   session_start();
   $person = $_SESSION['person'];

 ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...