two problem i have:
first:
i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)
second:
why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.
and the codes:
<html>
<body>
<?php
if(isset($_POST['select sex']))
echo "it's done";
?>
<form name="input" action="" method="post">
<input type="radio" name="sex" value="male" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
"> Male<br />
<input type="radio" name="sex" value="female" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
"> Female<br />
<input type="submit" name="select sex" value="Submit" />
</form>
</body>
For checkbox, not mentioned checked attribute for any of the check box option.
For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.
if(isset($_POST['select sex']))
Related
Good day!
I'm doing a site. There is a checkbox which update the option I choose in phpmyadmin.
But if I press submit, then f5/reload page, the preference I choose disappears on client/html side, (look the image for better understanding)
Image, open me!
So basically I want the site remember my choice in the "user account"
I tried with
<form action="pr.php" method="POST">
<h3 style="color:red;">Numbers?</h3>
<input type="checkbox" name="a1" value="1" <?php if(isset($_POST['a1'])) echo "checked='checked'"; ?> /><label>One</label><br/>
<input type="checkbox" name="a2" value="2" <?php if(isset($_POST['a2'])) echo "checked='checked'"; ?> /><label>Two</label><br/>
<input type="checkbox" name="a3" value="3" <?php if(isset($_POST['a3'])) echo "checked='checked'"; ?> /><label>3</label><br/>
<input type="checkbox" name="a4" value="4" <?php if(!is_null($_POST['a4'])) echo "checked='checked'"; ?> /><label>Four</label><br/>
<input type="submit" value="submit" name="submit">
It doesnt work with isset, empty or is_null.
Thanks for all!
Unless you save the information on a database, even if you clicked on submit, after you refresh the webpage you are going to loose the the values of the variables.
You could use cookies to save the value of the choice that was checked:
setcookie("checkbox1","checked");
First parameter is name of the cookie, second one is the value of the cookie, and you can add a third one with the time you want the cookie to have before it expires.
And in your if condition, you could do this:
<?php if($_COOKIE['a1'] == "checked") echo "checked='checked'"; >
For more info, check this link.
so I'm creating a small form where you can update the values of some fields pulled from a database.
Two of the fields I have however are acting wrong, when the page loads they display the information correctly but when you press 'submit' the fields in the MySQL table go blank. All the other fields update correctly including a checkbox, but not these two radio buttons.
Here's the code for it, I'm sorry to be asking this but does anyone see the error? I'm rather new with PHP so I'm not sure what to look for.
Snippet of the PHP Code (I can post the full code of it, but it's long):
<?php
if(isset($_POST['submit'])) {
$mbr=$_POST['mbr'];
$rec=$_POST['rec'];
$update = $dbconnect->query("UPDATE testing SET mbr='$mbr', rec='$rec'");
}
?>
HTML Code for the 2 radio buttons:
<input type="radio" name="mbr" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" <?php echo $noChecked; ?>>No
Nevermind I'm dumb and tired and missed the obvious solution, I forgot to put the values of the buttons in their code.
How they should be
<input type="radio" name="mbr" value="up" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" value="down" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" value="yes" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" value="no" <?php echo $noChecked; ?>>
So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>
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.
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" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
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.
How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.