SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to echo the value of a radio button and reuse in a form?

    Hi,
    I am trying to pick up php and run into something i can not solve.
    In a form i use a radio button with two options.
    When an option is selected and i send the form, it doesn't give back anything.

    This is the code i use:

    Code:
    <html>
    <head>
    <title>Test Calculation</title>
    </head>
    <body>
    <?php
    function valid_W ($str){ return (ereg ('^[0-9]{3,5}$' , $str));}
    function valid_H ($str){ return (ereg ('^[0-9]{3,5}$' , $str));}
    if ($submit != "Submit" || !valid_W($W) || !valid_H($H)) {
    ?>
    <h3>Test Calculation</h3>
    <form id="test_calculation" name="test_calculation" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
     <ol>
      <li>
       <label for="Type"> Type<br/>
        Type1<input type="radio" name="Type1" value="type" <?php if ($type == 'Type1') { echo "checked"; } ?>  /><br/>
        Type2<input type="radio" name="Typ2" value="type" <?php if ($type == 'Type2') { echo "checked"; } ?>  />
       </label>
      </li>
     </ol>
     <fieldset class="submit">
      <p>
       <input type="reset" name="reset" value="Reset" />
       <input type="submit" name="submit" value="Submit" />
      </p>
     </fieldset>
    </form>
    
    <?php 
    // in all other cases process the for,
    } else {		  
    
    // This function determens the type
    function ShowType($type){
    		 if ($type == 'Type1'){
    			 $version = 1;			 
    		 } else {
    		     $version = 2;		   
    		 }
    		 return $version;
    }
    
    $ShowVersion = ShowType($type);
    echo "The version is: " . ($ShowVersion) . "<br/><br />";
    
    }
    ?>
    i remember the process was different from getting the value out of a checkbox.
    it is just impossible for me to figure it out.

    would you be so kind to tell me what is not right and how i should get it working with what code?

    thanks so much in advance.

    kind regards,

    Hans

  2. #2
    SitePoint Member
    Join Date
    Sep 2011
    Location
    Moscow. RU
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, firstly - as i see you want to choose from 2 things with radio button - they must have same name "Type1" for example, not Type1 and Typ2 (these one different). Also you seem to be using variables with register_globals - avoid it, every variable is registered become global - your script can be hacked very easily. PHP have global array with posted variables - called $_POST - so you can get your posted variable by $_POST['Type1']. Just set different values like:

    <label for="Type"> Type<br/>
    Type1<input type="radio" name="Type1" value="type1" <?php if ($_POST['Type1'] == 'type1') { echo "checked"; } ?> /><br/>
    Type2<input type="radio" name="Type1" value="type2" <?php if ($_POST['Type1'] == 'type2') { echo "checked"; } ?> />
    </label>

    hope it help you.

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hanszuidinga View Post


    <?php if ($type == 'Type1') { echo "checked"; } ?> /><br/>
    <?php if ($type == 'Type2') { echo "checked"; } ?> />
    I don't see where you have declared $type before using it in the above lines.

  4. #4
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Some advice:

    1) Stop opening and closing PHP so much... Echo exists for a reason.

    2) if you're only doing a simple conditional check, don't waste the overhead of a function on it. (though I'd probably keep the preg one since that's more complex, the latter one is just silly)

    3) if you're only doing output as a condition, don't waste an IF on it, use a inline eval.

    4) you appear to be outputting XHTML, in which case it's checked="checked", not just checked.

    5) if you have two functions returning the same operation, you should only have one value.

    6) form elements are NOT a list and have perfectly good semantic meanings all their own -- so lose the list.

    7) FOR on LABEL points at ID, not NAME

    8) there is no reason after 2001 to put NAME on a FORM.

    9) you're using radio buttons, which means they should have the same NAME and different VALUE -- you've got that backwards.

    10) You've got some case mismatch's going on...

    11) I'd suggest storing 'type' as just a number, not the string as that makes your logic more complex than need be. (numeric compares are typically faster than string anyways)

    Code:
    <?php
    
    // assumes $type=$_POST['type'];
    
    function invalidWH($str) {
    	return !(ereg ('^[0-9]{3,5}$' , $str));
    }
    
    if (
    	($submit!='Submit') ||
    	invalidWH($W) ||
    	invalidWH($H))
    ) {
    
    	echo '
    	<h3>Test Calculation</h3>
    
    	<form
    		id="test_calculation"
    		method="post"
    		action="',$_SERVER['PHP_SELF'],'"
    	>
    		<fieldset>
    			<legend>Type</legend>
    			<label for="type1">Type 1</label>
    			<input
    				type="radio"
    				name="type"
    				id="type1"
    				value="1"',(
    					$type=='1' ? ' checked="checked"' : ''
    				),'
    			/><br />
    			<label for="type2">Type 2</label>
    			<input
    				type="radio"
    				name="type"
    				id="type2"
    				value="2"',(
    					$type=='2' ? ' checked="checked"' : ''
    				),'
    			/><br />
    		</fieldset>
    		<div>
    			<input type="reset" name="reset" value="Reset" />
    			<input type="submit" name="submit" value="Submit" />
    		</div>
    	</form>';
    
    } else {
    
    	echo '
    	<p>
    		The version is: ',$type,'
    	</p>';
    
    }
    Or something to that effect... See, same NAME, different ID, different VALUE.


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
  •