How to store checkbox value as 1 and 0 - php

It doesn't stores value in my database. It always stores "0" value even if i checked the item.
Here is my code:
<input type="checkbox" name="parental" <?php $parental = (isset($_POST['parental'])) ? 0 : 1;?>/>
<input type="hidden" name="parental" />

You forgot the attribute value and also add echo in php code
<input type="checkbox" name="parental" value="<?php echo $parental = (isset($_POST['parental'])) ? 0 : 1;?>" />
also why you have this element?
<input type="hidden" name="parental" />
It will overwrite the checkbox since both have the same name, either remove the hidden element or change the name

To be clear, checkbox value should always be the same, in your case 1, since checkbox passes it's value when it is checked, and does not when it is not. Also, if you want to use hidden field, you should use it before checkbox, so in case checkbox is not checked, it still passes 0.
For example:
<input type="hidden" name="parental" value="0" />
<input type="checkbox" name="parental" value="1" />
If the checkbox is checked $_POST["parental"] will equal 1, else it will equal 0. There are better ways to achieve this in php after the submit, though.
Also, regarding your conditional, I suppose you want to check the checkbox if parental is 1, which you should do like this, for example:
<input type="checkbox" name="parental" value="1" <? echo $_POST["parental"]=="1"?"checked":""; ?> />
Changing input type checkbox value, does not really have much sense in your case, if I understood right.

Related

get hidden value of the currently selected radio button

i have a form with Variables coming from mysql query , and i passing from a form 2 value of radio and hidden input , no problem with radio .. but hidden not passing Correct value , passing only first value found it on the page .
I want to get 2 value of radio and hidden together when i Choose the currently selected radio button
<input type="radio" name="radio" value="<?php echo $awardid ; ?>" />
<input type="hidden" name="point" value="<? echo $point ; ?>" />
after print :
<input type="radio" name="radio" value="1">
<input type="hidden" name="point" value="3">
<input type="radio" name="radio" value="2">
<input type="hidden" name="point" value="5">
<input type="radio" name="radio" value="3">
<input type="hidden" name="point" value="8">
elc ...
As an example , when i Choose Second radio , passing Correct value of radio 2 and passing value of hidden 3 -> " that's not Correct value , must be 5 " .
every and any Choose for radio , passing with it first value of hidden on page -> 3
, when i Change hidden input to radio input and Choose it , passing the Correct value without any problem ...
so this problem happen when the input is hidden .. why ? and Solution ?
Radio inputs share a name in order to define the group.
Hidden inputs cannot share names, as they are discreet entities.
I'd suggest appending the $awardid to the hidden input's name.
<input type="hidden" name="point<?php echo $awardid; ?>" value="<?php echo $point ; ?>" />
Then, you can get the value of that particular input based on the selected radio button.
$radio = $_POST['radio'];
$point = $_POST['point'.$radio];
All of your form elements of the same input type share the same name. The name field of the form elements are the keys of the key/value pairs in form submissions. Thus, when you have 3 hidden input fields with the same value for the name field, it must choose one. Remember, the hidden input fields do not correlate at all with the radio input fields which are simply organized close to each other in the code. Your browser does not know that your point fields have anything to do with the radio fields.
You should have unique names for all your input fields, like so
<input type="radio" name="radio" value="1">
<input type="hidden" name="point1" value="3">
<input type="radio" name="radio" value="2">
<input type="hidden" name="point2" value="5">
<input type="radio" name="radio" value="3">
<input type="hidden" name="point3" value="8">
And then you can retreive their values with
$_POST['radio']
$_POST['point1']
and so on.
EDIT: However, that does mean that for every form submission, EVERY hidden field's data will be sent every time, regardless of which radio input is selected. To solve this, you can intercept the form submission on the front end with an event listener such as .submit(), and then disable the input fields you do not want to be submitted prior to allowing the form submission to go through.
you can rename point to point[radio buttons value]
so that when you request point as array, you can get the value from it something like this #$_REQUEST["point"][#$_REQUEST["radio"]];
So as discussed on the comments. I will show my approach.
So as you mentioned that you have a mysql query to create your inputs (point and radio) I think that is something like:
Please disconsider syntaxe as it been a while since I programmed on php
$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){
echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
echo "<input type=\"hidden\" name=\"point\" value=\"".$row[point]."\">";
}
My suggestion would be you just print the radios and after submit it you check the point associated to it, something like this:
if ( isset($_POST['radio']) ){
$qryToFindPoint = "select point from someTable where sameconditions_here AND radio = " . $_POST['radio'] ;
//do whatever you need here with the selected point
}else{
$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){
echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
}
}
This is just the idea. Of course you should use proper mysqli or PDO functions to do your queries and avoid sql injection.
With this approach you would avoid also a HTML injection. Say that in your final result as another answer suggests has this:
<input type="hidden" name="point1" value="4" />
Anyone can edit the html code and change this value to lets say:
<input type="hidden" name="point1" value="400000" />
And then submit it. As you would not check, the value of the point it would not be the right one.

onclick set php session to keep checkbox checked. On uncheck end session

This is my form :
<input type="checkbox" name="dept" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
When i click the checkbox, the page is refreshed with the ?dept=sales in the URL. Just as i want it to be. But the checkbox is unchecked. I want the checkbox to be checked. And when the checkbox is unchecked i want the ?dept=sales to be removed from teh URL.
Thanks.
Your checkbox's name is dept, not sales. The value of your checkbox is sales. This means that if you want to access the value of your checkbox, you will need to access it via $_POST['dept'], not $_POST['sales']. If your form method isn't declared as method="post", use $_GET['dept'] instead of $_POST['dept'].
At first check your check box name, It's dept but you fetch from sales $_POST, another hint is that if your request is shown on the url then it's get not post, If you want to remove parameter from your url add method="post" to your form, At last your code should be like this:
<form action="your action here" method="post">
<input type="checkbox" name="sales" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
</form>
You're checking for an input with the name "sales" change $_POST['sales'] to $_POST['dept'] that'll work :)
Submit the form only when the checked property true like,
<input type="checkbox" name="dept" value="sales"
<?php if($_REQUEST['dept']=='sales')) echo "checked='checked'"; ?>
onclick="if(this.checked==true) this.form.submit();" /><br />

How to mantain radiobutton, dropdown and checkbox values if submit fails PHP

I know how to it with text inputs. I can easily put a php script in its value, but doing it with input groups seems different. How can I mantain the values of group inputs if the submission of the form fails?
To re-mark a checkbox or radio button as checked, you use this code:
<input type="checkbox" name="foo" id="foo" checked="checked"/>
The key is checked="checked".
If you are using groups of checkboxes, make sure the name of the field ends with brackets [], like this:
<input type="checkbox" name="foo[]" id="foo_1" value="1" checked="checked"/>
<input type="checkbox" name="foo[]" id="foo_2" value="2" checked="checked"/>
Then your $_REQUEST['foo'] variable will automatically be an array of checked values. You can use in_array to see if a particular checkbox was checked.
Update based on comment
Here's how I would set it:
<input type="checkbox" name="foo[]" id="foo_1" value="1" <?= (isset($_POST['foo'] && in_array('1', $_POST['foo'])) ? 'check="checked"' : '' ?>/>
For single items (like radios), use this:
<input type="radio" name="foo" id="foo" value="1" <?= isset($_POST['radio]) ? 'check="checked"' : '' ?>/>
Hope that helps.
Update 2:
Also, make sure you escape user input! Your example should look like this:
<input type="text" name="username" value="<?php if(isset($_POST['username']) echo htmlspecialchars($_POST['username']);?>">
Always assume the user is trying to hack your system, always escape user input!
Print the " checked" attribute for radio buttons and checkbox input tags, or the " selected" attribute for dropdown option tags.

check box with accept terms

I would like to check whether user checked box or not with php code. how can I do this?? and if checked then what kind of value I will get??
<input name="accept" type="checkbox" class="tickbox" value="" />
In this specific case, you will get $_POST['accept'] == '' , which is immensely un-useful.
You'll want to add a value to that tag:
<input name="accept" type="checkbox" class="tickbox" value="1" />
With that value, you'll get $_POST['accept'] == '1' when the checkbox is checked, and no 'accept' key at all when the checkbox is not checked.
<input type="hidden" name="accept" value="0" />
<input type="checkbox" name="accept" value="1" />
if unchecked : return hidden field’s value => 0
if checked : return checkbox’s value => 1
You need to have a value assigned to the checkbox. If the check box is ticked, this value will be returned when the form is submitted. The other option is to use a Javascript to check this before submission.

determine whether checkbox is checked php $_GET

I just want to have php determines whether a checkbox is checked, but I am running into a problem of getting the right return. Help please.
My html code
<label>
<input name="Language" type="checkbox" id="aa" checked="checked" />
One</label>
<label>
<input name="Language" type="checkbox" id="bb" />Two</label>
<label>
<input type="checkbox" name="Language" id="cc" />Three</label>
I pass the values to php by the $_GET method
my php code:
$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];
echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";
the output is
true
false
false
I next want to just determine if each box is checked and if so, do something.
if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }
if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }
if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }
But the if statements always return true, even if the box is not checked. I tried variations of "===" and "!=", but it does not seem to work.
TIA
if (isset($_GET['checkbox_name'])) { ... }
Form controls (with the exception of file inputs, and with some special rules for image inputs) always submit strings. There is no concept of a boolean in a query string or a form encoded POST body.
The id is irrelevant — only the name and value matter (at least as far as PHP is concerned).
Since you haven't given them values they will, IIRC, default to on so if you are doing a comparison you should look for that. Looking with isset is simpler though. This is somewhat beside the point though, since your example gives them all the same name and value, so you can't differentiate between them.
Additionally, due to an oddity of the PHP form data parser, you have to end the with [] if you want multiple elements with the same name.
You probably want to do something like this:
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]" id="cc" value="Three" />Three</label>
Important: Note the addition of values and the change in name.
Then in PHP $_GET['Language'] will be an Array of the values of the checked checkboxes, which you can loop over.
Try isset()
I think your HTML code should be like
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One
</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
<input name="Language[]" type="checkbox" id="cc" value ="3" />Three
</label>
and then by using something like
$count = count($_GET["Language"]); you can count the number of checkboxes checked.
Or do
$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes
Then you can foreach over the array
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
In my PHP I got it from $_POST['checkbox_name'], and I found that the variable had the value on when the box was checked (i.e. it existed even if the checkbox was clear).

Categories