The form doesn't send the information right.

Discussion in 'PHP' started by Rinox, Dec 28, 2008.

  1. #1
    Hello guys! I'm having a problem with a my simple form. The problem is that neither "echo $_POST['choice'];" nor the switch outputs anything. That probably means something wring with the form. This script is available for the users at index.php?d=test and I think maybe that has something to do with it in the <form> tag. In case it's worth mentioning the text "This works!" still appears everytime I submit the form.
    I'm not sure if I'm expressing myself well here, but please tell me if you don't understand what I mean.

    <?php
    if ( isset ( $_POST['options'] ) ) {
                echo "This works!";
    	echo $_POST['choice'];
                    switch ( $_POST['choice'] ) {
                        
                        case "3":
                            echo "Option 3 is chosen";
                        break;
                        
                        case "2":
                            echo "Option 2 is chosen";
                        break;
                        
                        case "1":
                            echo "Option 1 is chosen";
                        break;
                    
                    }
            
        }
       else  {
    ?>
    
    <table>
    	<tr>
    		<td>
    			<form action="" method="post">
    				<select>
    					<option name=\"choice\" value=\"1\">-- Options --</option>
    					<option name=\"choice\" value=\"2\">Option 2</option>
    					<option name=\"choice\" value=\"3\">Option 3</option>
    
    				</select>
    				<input type="submit" name="options" value="Submit" />
    			</form>
    		</td>
    	</tr>
    </table>
    <?php } ?>
    PHP:

     
    Rinox, Dec 28, 2008 IP
  2. MC_delta_T

    MC_delta_T Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    i suggest you to learn basic html tags

                    <select name="choice">
                        <option value="1">-- Options --</option>
                        <option value="2">Option 2</option>
                        <option value="3">Option 3</option>
    
    HTML:
     
    MC_delta_T, Dec 28, 2008 IP
  3. Rinox

    Rinox Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know basic html tags. I've tried your suggested code, but that didn't work. I've changed it now so that the name-attribute is only inside the select-tag but it still doesn't work... :( Do you have any other suggestions?
     
    Rinox, Dec 28, 2008 IP
  4. pepeRu

    pepeRu Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    uhm why are you escaping the values with a back-slash? tried to actually put the page name in the action attribute for the form? the code MC_delta_T gave you ought to work if there is no other problems we can't see
     
    pepeRu, Dec 29, 2008 IP
  5. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this solution should work =)

    if you want to see if all the data were passed or not, try this
    echo "<pre>"; print_r($_POST); echo "</pre>";
    PHP:
    with this you can see all the contents of the POST variable.
     
    fairuz.ismail, Dec 29, 2008 IP
  6. farad

    farad Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <select name="choose">
    <option value="0">Choose an option ..</option>
    <option value="1">First Option</option>
    <option value="2">Second Option</option>
    <option value="3">Third Option</option>
    </select>
    
    <?php
    $pselect = $_POST["choose"];
    echo "You selected option Nr.", $pselect;
    ?>
    PHP:
     
    farad, Dec 29, 2008 IP
  7. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I noticed that your HTML code has PHP like quotation marks \".
    You should just replace it with only quotes "choice".
    Now try your program as you like ....
     
    apsam29, Dec 30, 2008 IP