Using radio button to set cookie preferences? - php

I have these 4 radio buttons in which i am submitting to a validatepreferences.php which is the php code below however i am struggling to understand why when i click submit nothing is going through the if statement therefore not giving me my cookie in which to change images based on user input
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage"> No Picture
I think the php code must have an error or my if is not right altough i cannot see it.
<?php
if(isset($_POST['radioimage'])){
$radioimage = $_POST['radioimage'];
if ($radioimage == "0" || $radioimage == "1" || $radioimage == "2" || $radioimage =="3") {
setcookie("image", $radioimage, time()+300);
}
}
?>

You're not giving the radiobuttons values in the form. You have to give them values so you can retrieve those values with $_POST in validatepreferences.php. So the HTML should be:
<input type="radio" name="radioimage" value="1"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage" value="2"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage" value="3"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage" value="4"> No Picture

Related

to checked whether one of the radio button is checked or not from a particular radio group

All the inputs have the same name as option<?=$x?> for each loop but always the input with type=hidden of the last line overrides all of them although one of the radio buttons with same name option<?=$x?> is selected
Is there anyway so that I can check whether anyone out of four radios of above is chosen or not, if not then I have to pass the last input instead of them
<input type="radio" name="option<?=$x?>" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option<?=$x?>" value="null;<?=$data['id'].';'.$data['subject']?>">
You can try this
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option[<?=$x?>]['hidden']" value="null;<?=$data['id'].';'.$data['subject']?>">`
Now when you post you will get option array
$options = $_POST['option'];
$radioValue = $options[$x]['radio'];
$hiddenValue = $options[$x]['hidden'];
You can read value like this
$optionValue = isset($options[$x]['radio']) ? $options[$x]['radio'] : $options[$x]['hidden'];
Hope it may help you

Using the radio button 'checked' value in HTML and PHP

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
}
?>

Identifying if no radio buttons selected with PHP

I'm trying to write some code that will determine if no radio buttons are selected on a form
the form consists of many fields but have just included the radio buttons in form below
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
then in the myPHPPage.php page I have something like below to assign the POST value to a variable:
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}
But I want some code like: If no radio buttons selected $var = $someValue
You need to bind all-radio buttons in a group like
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
Then in PHP
<?php
if(isset($_POST['basic'])) { //remove ! from condition
$var = $_POST['basic'];};
echo $someValue = $var;
}
?>
TRY THIS....you should have all radio button same NAME
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
here your php code on server after the submit is press
<?php
if(isset($_POST['send'])) { //change index to submit button name
if($_POST['basic'] == ''){ // if no button is selected then
$var = $someValue;
}//if ends here
else
{ //if any of the radio is selected "if you don't want else so remove that"
$var = $_POST['basic'];
}//else ends here
}//isset ends here
?>
Try this:
<?php
// Handle Post
if (isset($_POST['send']))
{
// Get Post Values
$basic = isset($_POST['basic']) ? $_POST['basic'] : '';
$silver = isset($_POST['silver']) ? $_POST['silver'] : '';
$gold = isset($_POST['gold']) ? $_POST['gold'] : '';
// Atleast one is selected
if (!empty($basic) || !empty($silver) || !empty($gold))
{
echo 'You have made atleast one selection';
}
else
{
echo 'You have not made any selection.';
}
}
?>
<form action="" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
If you are using radio, all the names of input radio for that particular option should be same.
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
<?php
if(isset($_POST['basic']))
{
$var = $_POST['basic'];
}
?>
The first thing you have to know about radio button is that if they are reflecting the same attribute, they should have a same name.
Thus your form have to be changed like this.
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="radioName" value="basic">
Value 2 <input type="radio" name="radioName" value="silver">
Value 3 <input type="radio" name="radioName" value="gold">
<input type="submit" name="send" value="Submit">
Then you want to assign a value if no radio button is selected.
You can do the following:
$var="";
$someValue="abc";
if(isset($_POST['radionName'])) {
$var = $_POST['radionName'];// This means one radio button is selected, thus you have a value.
}
else{
$var=$someValue; // No radio button is selected, thus you can assign a default desired value.
}
<form action="myPHPPage.php" method="post">
if buttons are optional please make a group of radio button with name attribute value.
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
Then use a php condition here.
<?php
if(isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}
You can check all $_POST elements,than can compare name and their values.
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
PHP: Possible to automatically get all POSTed data?
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
<?php
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
Please Try this one

Checking which radio button is selected in PHP

I have an HTML page that contains two radio buttons ("yes" and "no") along with a submit button.
How can I specify that I want to execute a PostgreSQL query ONLY if the submit button is pressed AND the "yes" radio button is selected?
Your radio buttons need to have the same name, so check the value that is submitted for that field:
if ($_POST['radio'] == 'Yes')
{
// Execute query
}
Given HTML like this
<form action="foo.php" method="post">
<label for="yesno_yes">
<input type="radio" name="yesno" value="yes" id="yesno_yes"> Yes
</label>
<label for="yesno_no">
<input type="radio" name="yesno" value="no" id="yesno_no"> No
</label>
<input type="submit" name="submit_btn" value="Submit">
</form>
You can check using this
<?php
if (isset($_POST['submit_btn'], $_POST['yesno']) && $_POST['yesno'] == 'yes') {
// do stuff here
}

Using checkbox and Input field for inserting into DB

I have a form such as the one below:
<input type="checkbox" name="type" value="wash"><label>Wash</label><br>
<input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
<label>Other (Specify)</label><br>
<input name="type"><br>
If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".
Do you mean something like this?
HTML:
<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>
PHP:
if (!empty($_REQUEST['other_type']))
$_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
First of all you should better use radio buttons instead of checkboxes.
Then you could do the following
<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>
Your PHP would then look like this:
if ($_REQUEST["type"] == "wash"){
echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
echo "no wash";
}else if ($_REQUEST["type"] == "other"){
echo "you want to ".$_REQUEST["other_type"];
}
If you use JS you could even disable the textbox unless the user selects the third option.
Edit: If I got your comment right it would be the easiest like this:
<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>
PHP
if (isset($_REQUEST["wash_me"]){
echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
echo "and do the following: ".$_REQUEST["other"];
}

Categories