Checkbox and search query in Codeigniter - php

My page is coded in PHP/codeigniter and I have a form with some checkboxes in it. When I select a few checkboxes and submit the form, I pass the checkbox values to the controller, which then decides if a checkbox is checked, implodes all the checked checkbox values into an array and passes to the url using uri->assoc_to_uri($array). This is how I coded my filters for a search engine (any better way?). Now when I redisplay the form with the checkboxes again, I want the checkboxes previously checked to be checked again. How can I do this?
My initial idea is to do a uri_>uri_to_assoc(#), find the value of the relevant key (ie. 1.2.3) then explode to an array $arr and something like the following code:
<input type="checkbox" name="price_range_4" value="<?php if($arr[4] != '') echo 'selected'; ?>">
Is there a faster/better way?

Why not use the Session Class and pass an array to set_flashdata(), then retrieve it on the search results page to check the checkboxes?

In addition to Darren's answer you have another HTML problem:
<input type="checkbox" name="price_range_4" value="<?php if($arr[4] != '') echo 'selected'; ?>">
It's not the value you want to change, it's the checked attribute. Something like this:
<input type="checkbox" name="price_range_4" value="my_value"<?php if ($should_be_checked) echo ' checked="checked"'; ?> />
If you're using the Form helper, you can use the form_checkbox() function to make this a bit cleaner:
Example:
echo form_checkbox('price_range_4', 'my_value', $should_be_checked);
Of course $should_be_checked will be whatever way you decide to define it, either with session data, $_POST data, or another way.

Related

Return Checkbox states: Form with errors: PHP

I created a from with multiple required fields that the users my complete. Then I also have 3 checkboxes of which the user must select at least one of them. When the user submit the form I start doing the checks and each error I find I store in an array. If there are any errors I display the errors for the user plus the form with the values that the user already entered. This bit work fine. I want to know how I would be able to return a checked checkbox if the user checked any of then and any amount, but none, fo them? With from fields it's easy:
<input type="text" name="First_Name" value="<?php echo $First_Name; ?>" />
My question is in what way I would be able to get the result of my checkbox the user select? My guess is that it might be something like:
if(isset())
But I am not sure. Any help on this please?
<input type="checkbox" name="check1" value="1"<?php if (isset($_POST['check1'])) echo " checked"; ?>>
Try if(isset($_POST['checkbox'])) { do this }
If the checkbox has been selected the if statement will run.
When you submit a checkbox, the "value" that has been given will be set. So if your value gets set into a database this probably will be a tinyint(1)
you should use the following:
<input type="text" name="aCheckBox" value="1" <?php if (#$_POST['aCheckBox']=="1") { echo 'checked="checked"';}?> />
The #-sign is supressing the notices, so you dont have to use isset. Isset is useless anyway, because some browsers send the field in the post array but without value. So the isset would ways return true.

Pass selected checkboxs between pages

I want to pass the values of selected checkboxs between pages. How can I do this ? I think that I have to use JavaScript to get select checkbox values but I don't know how pass to another page the values.
Best Regards
It depends on on which side you want to preserve them. On client side? If so, use session. On clien side? Then use any local storage, like jStorage, HTML5 Storage, DOM Storage etc...
The simplest way would be to use a PHP session:
<?php
//start session
session_start();
//get the posted values from your form
$_SESSION['checkbox_value_1'] = $_POST['checkbox_1_name'];
$_SESSION['checkbox_value_2'] = $_POST['checkbox_2_name'];
$_SESSION['checkbox_value_3'] = $_POST['checkbox_3_name'];
?>
So, when you need the values of the checkboxes you can can get the from the session:
<input type="checkbox" value="<?php echo $_SESSION['checkbox_value_1'] ?>" name="checkbox_1" />
That should do it.
You can also set the checkbox to 'checked' if you add this bit:
<?php
if (isset($_SESSION['checkbox_value_1']) && $_SESSION['checkbox_value_1'] != ''){
//is selected (has a value)
$checkbox_selected = 'checked="checked"';
} else $checkbox_selected = ''; //not checked
?>
Then you can write your checkbox like so:
<input type="checkbox" value="<?php echo $_SESSION['checkbox_value_1'] ?>" name="checkbox_1" <?php echo $checkbox_selected; ?> />
This will add the checked="checked" attribute to the checkbox.
You could also store these in an array if you want to limit the number of session values. Let me know if you'd like an example of that.
you could add get parameter to the form as you post it (so it sets get parameters to the url) representing the checkboxes and then using php to print checked and unchecked boxes according to the last states
for this you would have to catch the submit event of your form via javascript, parse the values of your form's boxes into get-values (many javascript frameworks have support for such operations) and then finally post it to the server, the php on the server then uses the global $_GET variable to test each state when printing the boxes.
(this is a rough idea of an implementation, allways consider unvalidated user parameters could be harmfull for your application)

Update MySQL DB with PHP from Checkbox

I am pulling data down from a MySQL table and loading it into a form for editing(updating) a record. Everything is working great until I come the the check boxes. The checkboxes in the form accurately reflect the values in the appropriate columns in the db. But when the person editing changes the checkbox in the edit form it does not pass the data to the database. I have read a ton of checkbox Q&A on stack overflow but don't seem to find what I am looking for. Sorry if this is a redundant Question. Here is the code.
<label for="amenities-beach">
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox"
value="<?php echo $row1["amenitiesB"]; ?>"
<?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach</label>
Where amenitiesB in:
value="<?php echo $row1["amenitiesB"]; ?>
is what has been returned from the DB with a SELECT statement with:
$row1 = mysql_fetch_array($result);
But when I change the value in the form and submit it nothing is passed to the variable in the UPDATE statement. Any idea what I am missing? I have 6 of these checkboxes,amenitiesB, amenitiesK, amenitiesS, amenitiesP, amenitiesF, and preferred all with the same code. Any help would be appreciated.
Thank You,
Dave
Ok here is the code: Everything else in the form updates fine. I attempt to pass it to:
$amenitiesB = $_POST['amenitiesB'];
then I put it into the update statement
Hotels.amenitiesB='".$amenitiesB."',
My UPDATE statement is,
$query="UPDATE Hotels
JOIN surfcup_Rates ON Hotels.id = surfcup_Rates.hotelid
SET Hotels.hotel='".$hotel."',
More columns, then
Hotels.amenitiesB='".$amenitiesB."',
Hotels.amenitiesB='".$amenitiesK."',
Hotels.amenitiesB='".$amenitiesS."',
Hotels.amenitiesB='".$amenitiesP."',
Hotels.amenitiesB='".$amenitiesF."',
Hotels.amenitiesB='".$preferred."',
More columns then:
WHERE Hotels.id='".$id."'";
The problem you have comes because when a checkbox is unchecked, by default its data is not transmitted to your PHP, and that's why you have problems by having the UPDATE query parameter empty.
So before your update statement you should have:
$fieldenabled=(bool)(isset($_POST['CHECKBOXNAME']) ? TRUE : FALSE;
And call your UPDATE query with that.
EDIT: Of course you can change $_POST with $_GET depending on the sending method of the <form>
Edit: I think I get the problem. When the box is initially unchecked, the input has an empty value, then when you check it, it passes an empty value in... it will never fill with what you intend the checked value to be. So, instead you need something like this:
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox" value="amenity B Selected" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
... don't make the "value" attribute dynamic, or else once it becomes empty it will always be empty.
Original Answer:
I assume when you say "change the value in the form" you mean that you uncheck the checkbox... unchecked checkboxes never send any data when you submit the form. You check for "unchecked" status by checking to see if the form variable has been passed at all.
For example:
if (isset($_GET['amenitiesB'])) {
// process with the knowledge that "amenitiesB" was checked
}
else {
// process with the knowledge that "amenitiesB" was unchecked
}
If you mean that you somehow dynamically change the "value" of the checkbox to something else, then I'll need to see the code that accomplishes that.
The main purpose of the "value" attribute in a checkbox input is when you're passing the variable as an array:
<label for="amenities-beach">
<input class="choose" name="amenities[]" id="amenities-beach" type="checkbox" value="<?php echo $row1["amenitiesB"]; ?>" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach
</label>
... note specifically that I've changed the "name" attribute from "amenitiesB" to "amenities[]", which, if carried through all of your amenities checkboxes, will give you access to them all in your processing script through the $_GET['amenities'] array (or $_POST, if applicable). Otherwise, there's not much reason to use the "value" attribute of the checkbox input, because you can get all you need just by knowing $_GET['amenitiesB'] is checked (and thus sent with the form) or unchecked (and not sent with the form).

How do I use php $_POST to get the input from a checkbox?

So, on my form I have two input methods, a text field and a checkbox.
How would I use PHP to test if the checkbox has been checked or not? Perhaps using $_POST?
I want to check if the checkbox is checked, and if it is, set a boolean to true. I have no problems setting variables, but I can't seem to figure out how to get the input from the checkbox....
So, how would I get the input from a checkbox?
In your HTML form you have something similar:
<form method="post">
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
</form>
That is a checkbox named vehicle Now, in your PHP, you would access that with:
$boolean_variable = isset( $_POST['vehicle'] );
Have a look here: http://www.homeandlearn.co.uk/php/php4p11.html
When checkboxes aren't selected, they effectively have no value, so you test it with isset.
Yes, you can using the name attribute of the input when getting the posted form values.
Here is an example:
Handling Checkbox in PHP
<input type="checkbox" name="foo" value="1" />
if (isset($_POST['foo'])) {
// checkbox was checked
}
Checked checkboxes and their value are submitted in a POST request. Unchecked checkboxes aren't submitted. So if $_POST['foo'] exists at all, it was checked.

Retain the value of while() generated list of checkbox fields. Value="checked" (but value has data already?)

I'm in a bit of an odd spot here. I am modifying a script, and my validation is all working fine with the exception of my checkbox input selections which are generated from an array with a while loop..
What I need to be able to do is retain the value="checked" if the box has been selected, but the value field currently stores the id value to be passed to a table, and I the value option is how the "checked" is called... So
...PHP_SELF...
while(...)
{
<input name="seminar[]" type="checkbox" id="seminar[]" value="<?= $data[id] ?>">
}
...SUBMIT...
I am thinking I might need to store the submitted values in an array, then replace the value="<?= $data[id] ?>" with value="checked" but seems kinda wonky to me..
Anyone run into this before have an suggestions?
I solved this. I did not realize this, but there is an input value titled: checked which works like: checked="checked" .. Geeze this put me in my place : )

Categories