SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio buttons and sessions

    PHP page 1: I have two radio buttons...
    <?php session_start(); ?>
    <input type="radio" name="image_choice" value="lp13-web-wild165c"
    style="width: 35px;"/>LP-13<br />
    <input type="radio" name="image_choice" value="lp14-web-wild289c"
    style="width: 35px;"/>LP-14<br />

    PHP page 2: Problem...on PHP page 2, how would I show which button was
    pressed?

    Would it be....
    <?php session_start();
    echo $_SESSION['image_choice'];
    ?>

    What I am looking to get as a result on page 2 is lp13-web-wild165c or
    lp14-web-wild289c to be printed to the screen.

    Thanks,
    Thad

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a particular reason you are using sessions? Since you already have the form, all you have to do is use $_POST['image_choice']

    The session_start() would be unnecessary, unless you have additional uses for it. You will probably need to add <form> tags and a submit button to your form though, unless you're going to use AJAX.

  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My original page has a bunch of images on it with radio buttons below them. If a person selects an image and continues to the next page, I will then know what image to process on the second page. I have just put in this thread, the code which I thought was applicable since the pages are not on the web yet. Do you need any more information?

    Thanks,
    Thad

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Thad Sobota View Post
    My original page has a bunch of images on it with radio buttons below them. If a person selects an image and continues to the next page, I will then know what image to process on the second page. I have just put in this thread, the code which I thought was applicable since the pages are not on the web yet. Do you need any more information?

    Thanks,
    Thad

    Your radio buttons name / value will be passed through the POST or GET function of the form, this is how you determin which radio button has been selected, you cannot do this with session variables.

    Would it be....
    <?php session_start();
    echo $_SESSION['image_choice'];
    ?>
    No it would be

    PHP Code:
    echo $_POST['image_choice']; 

    or

    PHP Code:
    echo $_GET['image_choice']; 
    You can also remove the session_start from the top of your form code, unless you are actually using sessions elsewhere in your code

    For more tutorials on forms see this link

    http://www.w3schools.com/html/html_forms.asp
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want the name of your radio buttons to be "image_choice[]" not "image_choice".

    The way you have it now, only the last radio button will send a value to the server. If you click on the first radio, nothing will be sent.

    When you change the name of the value, then it will be available to you in
    $_POST[image_choice] as an array and you'll have to loop through it to get your value.

    Quote Originally Posted by Thad Sobota View Post
    PHP page 1: I have two radio buttons...
    <?php session_start(); ?>
    <input type="radio" name="image_choice" value="lp13-web-wild165c"
    style="width: 35px;"/>LP-13<br />
    <input type="radio" name="image_choice" value="lp14-web-wild289c"
    style="width: 35px;"/>LP-14<br />

    PHP page 2: Problem...on PHP page 2, how would I show which button was
    pressed?

    Would it be....
    <?php session_start();
    echo $_SESSION['image_choice'];
    ?>

    What I am looking to get as a result on page 2 is lp13-web-wild165c or
    lp14-web-wild289c to be printed to the screen.

    Thanks,
    Thad

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ikeo View Post
    You want the name of your radio buttons to be "image_choice[]" not "image_choice".

    The way you have it now, only the last radio button will send a value to the server. If you click on the first radio, nothing will be sent.

    When you change the name of the value, then it will be available to you in
    $_POST[image_choice] as an array and you'll have to loop through it to get your value.

    Not true, you can only select one radio button, so they should be named the same, without the array, the value of the selectd radio button will then be assigned to the name given to the radios. In contrast if you were using checkboxes, where you can select more than one option at any one time then you need to use the array method and search the array for returned values.

    See the section on Radio buttons here http://www.w3schools.com/html/html_forms.asp for more info
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes View Post
    Your radio buttons name / value will be passed through the POST or GET function of the form, this is how you determin which radio button has been selected, you cannot do this with session variables.



    No it would be

    PHP Code:
    echo $_POST['image_choice']; 

    or

    PHP Code:
    echo $_GET['image_choice']; 
    You can also remove the session_start from the top of your form code, unless you are actually using sessions elsewhere in your code

    For more tutorials on forms see this link

    http://www.w3schools.com/html/html_forms.asp

    Is there not something like Javascript in PHP to store a form element in a variable? For example... $answer=form.image_choice.name.this (I know, bad JS but this is just to get a point across since I don't know JS well.)

    I've tried using $_GET, $_POST and $_REQUEST but just cannot figure it out. If there is just no way I can pull a form variable and store it in a PHP variable, I will need more help using $_GET. Can someone explain a short complete example?

    Thanks,
    Thad

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK here goes, your form needs to look something like.

    HTML Code:
    <form action="yourscript.php" method="POST" 
    Choose One of the below options<br />
    <input type="radio" name="choice" value="1" />option 1<br />
    <input type="radio" name="choice" value="2" />option 2<br />
    <input type="radio" name="choice" value="3" />option 3<br />
    <input type="radio" name="choice" value="4" />option 4<br />
    <input type="radio" name="choice" value="5" />option 5<br />
    <input type="submit" value="Submit" /><br />
    </form>
    Then your receiving script would be

    PHP Code:
    $option $_POST['choice'];
    echo 
    'You chose option No ' $option 
    Last edited by Mandes; Apr 1, 2007 at 10:18.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The light shines bright. Thank you so much for the clear example. I've looked so hard at examples and was so confused. I'll give your example a whirl, using my info of course. Now, things are looking so much clearer.

    Thanks again,
    Thad

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Thad Sobota View Post
    The light shines bright. Thank you so much for the clear example. I've looked so hard at examples and was so confused. I'll give your example a whirl, using my info of course. Now, things are looking so much clearer.

    Thanks again,
    Thad
    No problem

    To answer you other question yes you can use JS to scan the radion buttons and return a value of the checked value, but why would you want to do this, using the form in its normal usage is much easier, and works even if your user has JS turned off or not available.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by Mandes View Post
    No problem

    To answer you other question yes you can use JS to scan the radion buttons and return a value of the checked value, but why would you want to do this, using the form in its normal usage is much easier, and works even if your user has JS turned off or not available.
    Well, I was not wanting to use JS if I did not have to. I have gotten my pages to work. Thanks to ALL who helped and specially to Mandes for the great example.

  12. #12
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry guys ... I screwed up on that one.
    Thanks for catching that Mandes.

  13. #13
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ikeo View Post
    sorry guys ... I screwed up on that one.
    Thanks for catching that Mandes.
    No problem, dont worry I dont think anyone was watching
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  14. #14
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes View Post
    No problem, dont worry I dont think anyone was watching
    Yes I am.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •