Reputation: 4102
I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Upvotes: 1
Views: 198
Reputation: 26386
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(@$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(@$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(@$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
Upvotes: 1
Reputation: 11134
If you want to automatically select a radio input you can add the attribute checked
to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked
attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value
attributes, you define the checked
attribute when you want that radio to be selected.
Upvotes: 1
Reputation: 1453
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(@$_POST['radio_name'] == 'on'?"checked":""); ?> />
Upvotes: 1