PHP - form problem

Discussion in 'PHP' started by zotrik, Jul 31, 2008.

  1. #1
    Hi everyone! I have a problem with forms.
    I've created a form with radio type choices with a submit button. What I want to do here, is to extend the form so that, if the user selects one option, one form will be shown under the first one and if the user chooses an other option, an other form will pop under the first one. I did everything correctly, but my form doesn't do what I want it to do: the radio button must stay checked if the user checked it previously. With my code, the radio button comes unchecked when the user hits the submit button. Here's an example of my first code:
    
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label for="choice1">choice 1</label><input type="radio" name="my_choice" value="choice1">
    <label for="choice2">choice 2</label><input type="radio" name="my_choice" value="choice2">
    <input type="submit" value="next step">
    <?php
    if(isset($_POST['my_choice']))
    {
    if($_POST['my_choice'] == 'choice1')
    {
    include "next_form_1.php";
    }
    else
    {
    include "next_form_2.php";
    }
    }
    ?>
    
    PHP:
    So to save the user's choice, I thought about doing something like this:

    
    <?php session_start(); ?>
    <html>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label for="choice1">choice 1</label><input type="radio" name="my_choice" value="choice1" <?php if($_SESSION['selection'] == 'choice1'){?>checked="checked"<?php }?> />
    <label for="choice2">choice 2</label><input type="radio" name="my_choice" value="choice2" <?php if($_SESSION['selection'] == 'choice2'){?>checked="checked"<?php }?> />/>
    <input type="submit" value="next step"/>
    <?php
    if(isset($_POST['my_choice']))
    {
    if($_POST['my_choice'] == 'choice1')
    {
    $_SESSION['selection'] = 'choice1';
    include "next_form_1.php";
    }
    elseif($_POST['my_choice'] == 'choice2')
    {
    $_SESSION['selection'] = 'choice2';
    include "next_form_2.php";
    }
    }
    ?>
    </html>
    
    PHP:
    So, by doing that, I'm able to save the users's choice BUT the problem is that it does it only if the person refreshes his page OR if he clicks on the submit button twice. What can I do to solve the problem?
    Thanks!
     
    zotrik, Jul 31, 2008 IP
  2. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    hmm try thgs mate:

    
    <?php 
    session_start(); 
    ?>
    <html>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <label for="choice1">choice 1</label><input type="radio" name="my_choice" value="choice1" <?php if($_SESSION['selection'] == 'choice1'){?>checked="checked"<?php }?> />
    <label for="choice2">choice 2</label><input type="radio" name="my_choice" value="choice2" <?php if($_SESSION['selection'] == 'choice2'){?>checked="checked"<?php }?> />
    <input type="submit" name="submit_form" value="next step"/>
    
    <?php 
    if(isset($_POST['submit_form']))
    {
    
    if($_POST['my_choice'] == 'choice1')
    {
    
    $_SESSION['selection'] = 'choice1';
    
    include "next_form_1.php";
    
    } elseif($_POST['my_choice'] == 'choice2') {
    
    $_SESSION['selection'] = 'choice2';
    
    include "next_form_2.php";
    
    }
    
    }
    ?>
    </form>
    </html>
    
    PHP:
     
    graham23s, Jul 31, 2008 IP
  3. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #3
    I think a more elegant solution would be to use Javascript. PHP is completely unnecessary in this case.
     
    Louis11, Jul 31, 2008 IP
  4. zotrik

    zotrik Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Graham23: I tried your code, but nothing changed
    PS: I know I have a "/>" that I forgot to remove, but just ignore it...

    Louis11: I'm sure that javascript would be a good way to do what I want to do, but I'm not familiar with that language so if you could drop a line of code to my script to make it work, it would be greatly appreciated.

    Thanks!
     
    zotrik, Jul 31, 2008 IP
  5. kb4

    kb4 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Zotrik,

    I have to agree with Louis11 here, PHP is inappropriate for what (I believe) you're trying to achieve. Remember, the user of your site is the client which asks your site server for a web page be it HTML, PHP etc. Once that page has been 'served' by the server there is no further interaction between the client and server, unless some form of data is submitted. What you're trying to achieve is form manipulation at the client side which cannot be done with PHP unless you intend to submit an initial form to the php server, it then returns the appropriate web page for that particular selection.... oh lordy it gets complicated ! Much better to use Javascript... and if you care to peruse the following :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
    function mustardselected(){
    	var form1 = document.getElementById("form1");
    	form1.style.visibility = 'visible';
    	var form2 = document.getElementById("form2");
    	form2.style.visibility = 'hidden';
    };
    
    function ketchupselected(){
    var form1 = document.getElementById("form1");
    	form1.style.visibility = 'hidden';
    	var form2 = document.getElementById("form2");
    	form2.style.visibility = 'visible';
    }
    </script>
    
    </head>
    
    <body>
    
    <form name="testform" id="testform">
    
    Mustard: <input type="radio" name="condiments" id="mustard" onclick="mustardselected()" checked="true"/>
    Ketchup: <input type="radio" name="condiments" id="ketchup" onclick="ketchupselected()" checked="false"/>
    </form>
    
    <form name="form1" id="form1" style="visibility:hidden">
    Form 1 : Field 1<input type="text" />
    <br />
    Form 1 : Field 2<input type="text" />
    <br />
    </form>
    
    <form name="form2" id="form2" style="visibility:hidden">
    Form 2 : Field 1<input type="text" />
    <br />
    Form 2 : Field 2<input type="text" />
    <br />
    </form>
    </body>
    </html>
    
    HTML:
    You can copy and paste this into a .HTML file and then just browse it with your favoutite browser. It's not perfect but it shows you how to make data fields appear when a particular radio button is selected. One 'bug' I cannot seem to resolve is the fact that one of the radio buttons is selected upon page load. It may have been preferable to have neither selected.

    Alas it seems you'll have to learn a little bit of Javascript, but it's not too difficult, plenty of stuff out there in the internet.

    Hope it helps !
     
    kb4, Jul 31, 2008 IP