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:
<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?
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:
Stop opening and closing PHP so much… Echo exists for a reason.
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)
if you’re only doing output as a condition, don’t waste an IF on it, use a inline eval.
you appear to be outputting XHTML, in which case it’s checked=“checked”, not just checked.
if you have two functions returning the same operation, you should only have one value.
form elements are NOT a list and have perfectly good semantic meanings all their own – so lose the list.
FOR on LABEL points at ID, not NAME
there is no reason after 2001 to put NAME on a FORM.
you’re using radio buttons, which means they should have the same NAME and different VALUE – you’ve got that backwards.
You’ve got some case mismatch’s going on…
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)