Getting Checkbox values when user edits form data. PHP - php

I have a form with checkboxes that users fill out and submit. I then have another form that allows them to edit the information they submitted. This second form pre-populates with the information they submitted on the first form. This is fine with text fields, but how do I pre-populate the checkboxes? I.e. if they checked a checkbox on the first form, how do I get the second form to recognise that and display a checked checkbox?
I'm new to Php so sorry I can't be more technical with this query!
Thanks
Luke

When you submit the form, you have the data on the next page. So if theres a checkbox looking like this:
<input type="checkbox" name="over18" value="1" />
You have to check if the person selected the "over 18"-field.
if ($_POST['over18'] == '1') {
$checked = 'checked="checked"';
}
else {
$checked = ''; //If it's not checked
}
Then your output for the checkbox looks like this:
echo '<input type="checkbox" name="over18" value="1" ' . $is_checked .'/>';
So everytime the Checkbox is checked, the next page checks the Checkbox too.

Related

How to Access $_POST Data Across Multiple Forms

For a shopping cart application I'm working on for a class, I have to display information across every step. My problem is that I can display information only from the previous step. For step 1, an item is selected from a radio box. It is then stored like so:
function processStep1() {
//Implemented sessions
$_SESSION["items"] = $_POST["items"];
displayStep2();
}
Step 2 asks for a quantity and then validates it, like so:
function processStep2() {
//Implemented sessions
$_SESSION["quantity"] = $_POST["quantity"];
if ( preg_match('/^\d+$/',$_POST["quantity"]) ) {
displayStep3();
} else {
echo "ERROR: Quantity entered was invalid. Please try again.";
displayStep1();
}
}
(As an aside, I can't seem to get this to just refresh the current step (which would be displayStep2) when the input is not an integer, as I get an error every time I try to do so. If anyone has an answer for why that is, that is additionally helpful.)
But then on Step 3 of the form, I try to run the following line:
<p>You have selected <?php echo $_POST["quantity"] ?> units of the <?php echo $_POST["items"] ?>.<p>
Which responds with an error every time. I have tried various configurations of this same output, and have determined that it will always parse $_POST["quantity"], but never $_POST["items"]. I need it to do both.
Your third form does not "see" $_POST['items'] because it was not submitted to that form. You stored it in a session instead. Start a new session on your third form and request $_SESSION['items'].
Step 1
<form name="step1" method="post" action="whateverPageContaintsProcessStep1.php">
<input type="radio" name="items" value="item1">Item 1
<input type="radio" name="items" value="item2">Item 2
</form>
When we submit our form the $_POST data that is going to be send will be the value of the radio button we selected we selected.
Step 2
<form name="step2" method="post" action="whateverPageContaintsProcessStep2.php">
<input type="number" name="quantity" value="0" />
</form>
When we submit form2 the $_POST data will contain the value of the textbox called quantity, however since this form does not contain the previous radio buttons this value will not be send to the server and therefor we will not be able to access it in 'whateverPageContaintsProcessStep2.php'.

PHP checkboxes that return display and return "Yes" from a database

I am building a site with a number of independant check boxes that collect information about industry topics. I user will check the ones of interest and store “Yes” in the database which is Filemaker.
When they return, they will see the ones previously checked and can uncheck if needed.
To this end, I am trying to get a check box to display as checked if the database value is equal to “Yes” and display as unchecked if the value is blank. Also, of the user checks the checkbox on the form, it will send the value of “Yes” back to the database and a value of blank if the check box is unchecked.
So far, I am only able to display the “Yes” or blank for fields. Here is my code so far:
<input type="text" name="Core_Compentencies__Marketing" value="<?php echo $port_row->getField('Core_Compentencies::Marketing'); ?>"></td>
Any help is appreciated.
Thanks.
usually i use helper function to decide whether field value is checked or not.
<?php
function isChecked($value = '', $defaultVal = 'Yes')
{
if($value == $defaultVal)
{
return 'checked';
}
}
?>
<input name="checkbox" type="checkbox" value="Yes" <?php echo isChecked(Core_Compentencies::Marketing); ?>>

If statement check if post is set

Hello I'm having some problem.
I want it to be checked by default, and if you unselect it and press submit I want it to be unchecked
<input type="checkbox" name="show_signature" value="1"<?php echo isset($_POST['show_signature'])) ? ' checked=""' : '' ?>>
This works good unchecking > submitting and checkbox is unchecked and same if you check it and send the form, it stays checked.
But, I want it to be checked by default. Should'nt this work?
if (isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Tried this to
if (isset($_POST['show_signature']) || !isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Okay; from the question, I got this process
When the page loads the first time, you want the checkbox to be checked.
When the page is submitted, if the checkbox is unchecked, let it remain unchecked; otherwise, let it be checked.
Try
/*making the assumption that the submission process starts when a submit button
with name **submit** is present, use this
*/
if (isset($_POST["submit"])){
value = '<input type="checkbox" name="show_signature"';
value .= (isset($_POST["show_signature"]))? 'checked="checked"': "";
value .= ' />';
print value;
}
else{ //when the page is initially loaded
print '<input type="checkbox" name="show_signature" checked="checked" />';
}
The reason the post depends on the submit button (or any other field)t is because if the user unchecks the box, the $_POST["show_signature"] variable will not be found, and the form will not be processed at all.
That should resolve the issue.
Hope the explanation is clear and this helps.

retaining values of radio buttons

I am a php programmer .I have an image rating page ,in which it displays the image gallary with radio buttons once selected and submitted ,the image button values go to the table .when I retrieve the images they are rendered properly , however they do not show the previous radio button rating selection.
My question is how to retain the most recent radio selections , when images are fetched into the browser.
I'm not sure what your options are but it would be something like this;
<input type="radio" name="image_option" value="1"<?php $_POST['image_option'] == 1 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="2"<?php $_POST['image_option'] == 2 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="3"<?php $_POST['image_option'] == 3 ? ' checked="checked"'; ?> />
Hope that helps.
You'll have to set the "checked" attribute for the appropriate radio buttons that you want to be selected.
if you want to default select then use "Checked".
and if you want to checked on resent selective button. then on submit time, store radio button value in session and when browser get request after submission radio value then retrieve session value and apply checked on previous select radio button.
Your problem is that you need:
The value in the database to match
The value of the radio button itself
Which will allow you to:
Compare the database value (DBV) with the radio button value (RBV) and set it as checked if the comparison comes back as true.
The variables below are:
$ar_rvbs = the array of radioset values (strings or booleans, usually) you are going to loop through and check against the stored DBV.
$value = the value of each item in the radioset value array
$attrval = the stored value for radio button. it doesn't necessarily have to be in a database. you could use the post method to pass it from one page to the next.
$checked = if the DBV matches the RBV when the loop goes through, this will be set to the string "checked" otherwise it is just an empty string.
function makeRadioSet($ar_rvbs,$attrval=[DBV] /*A*/
{
foreach ($ar_rvbs as $value)
{
$checked = ''; /*B*/
if ($attrval==$value) /*C*/
$checked = "checked"; /*D*/
echo '<input type="radio" name = "fieldname" value = "'.$value.'" '.$checked.'>';
}
}
/A/ Pass the list of RBVs as an array and the DBV as a variable
/B/ Set checked to an empty string because that will be the default for all the radio buttons in the set except the one that matches the DBV
/C/ Compare the DBV to the current RBV being processed by the loop from the RBV array
/D/ If the comparison from step C returns true, make the checked string available for insertion into the input element
The echo takes care of generating each radio set option and making sure the one that matches the DBV has "checked" in the input element tag
You can use in following method
#{
string MaleChecked = "";
string FemaleChecked = "";
if(#Model.Gender=="Male")
{
MaleChecked = "Checked";
}
else
{
FemaleChecked = "Checked";
}
}
if form use
Male<input type="radio" name="Gender" id="rdoGender" value="Male" #MaleChecked>
Female<input type="radio" name="Gender" id="rdoGender1" value="Female" #FemaleChecked>
after reload the page you have to add the attribute checked to the radio buttons you have to check.
The correct syntax is:
<input type="radio" name="foo" id="bar" value="1" checked="checked" />

Finding the values of all of the checked checkboxes in php

Hi so have form with a vote and what i want to do is what ever options they tick, when they click submit it the posts all the values of the check boxes ticked to the same page allowed me to echo them
i have tried just doing this
if(isset($_POST['submitted'])) {
$list = $_POST['vote'];
echo $list;
}
but that only echos the last value selected
Thanks,
Ben
ok so i have a fix by changing the name to an array but i got a problem because i used javascript functions like this
checkAll(document.form.vote)
so what do i change it to?
When you name the inputs you can give it a name like name="checkboxes[]" and that will throw it in an array when it posts to the next page. Hope that helps!
In your form set name attribut like this:
<input type="checkbox" name="vote[]" />
Then you can:
foreach($_POST['vote'] as $vote){
echo $vote;
}

Categories