i have an optional checkbox called checkall
<input name="checkall" type="checkbox" value="ON" <?php echo set_checkbox('checkall', 'ON'); ?> />
i'm sure that the form is submitting that
if i give it a validation rule $this->form_validation->set_rules('checkall', 'Checkall', 'required');it works, but without a rule nothing worked out !
did i miss something? i think form helper doesn't require that for this function to work right ?
I think you're talking about the value being persisted without validation rules. This is STILL a problem in CI 2.x if I recall correctly, and jbreitwiser's patch from January 2010 is still necessary:
http://codeigniter.com/forums/viewthread/96617/P15/#689642
If this is still a problem in CI 2.x it is completely absurd, and I totally agree. But that patch will solve your problem.
If I understand you correctly your checkbox is not submitting with the form?
If that is the case, I had a fun time with this question on this thread HERE
PHP wants you to check to see if a checkbox is set or not by verifying whether or not there is a corresponding element in the POST array. If the checkbox was checked, there will be an element of the same name in the POST array (that element will have a NULL value), if the checkbox was NOT checked, then there will be NO matching element in the POST array.
The code would look something like this:
Your input element remains the same --
<input name="checkall" type="checkbox" value="ON" <?php echo set_checkbox('checkall', 'ON'); ?> />
Postback Handler Page gets a new way to validate a checkbox --
if(isset($_POST["checkall"])
{
$checkall = TRUE;
}
else
{
$checkall = FALSE;
}
Hopefully I helped, its late and your question is sparse on details.
Regards
Related
I am processing an html form using php. My question is specifically about input type checkbox:
<input type="checkbox" name="checkme" value="checked" <***php echo $data['checkme']; ***> >
This works for me because, on load, $data['checkme'] = ""; and on error, $data['checkme'] = "checked". I have searched quite a bit regarding this, and there are plenty of suggestions for setting the value of the checkbox input. But not in this way (that I found). This works, but I want to make sure I am not creating a problem that I don't foresee.
My question: Is this good practice?
Here is my problem, I know only html and php and I have no clue about how to use javascript... And all the solutions about my problems seems to be resolved in javascript and I wondered if there was a way to do it with php so that I could understand what I do.
I want to put a checkbox on the corner of my page (for instance "hide information") that would refresh the page automatically when checked and that would hide information on the page.
What I currently do is :
<?php
if(isset($_GET['condition']))
$_SESSION['condition'] = true;
else
$_SESSION['condition'] = false;
?>
...
...
<form>
<input type="checkbox" name="hide" value="1" onChange="this.form.submit()" <?php if($_SESSION['hide']) echo "checked";?> > hide information
</form>
I am facing two problems :
the first one is that I want the checkbox to stay checked/unchecked when the page is refreshed.. I solve that poorly with my php code, but there surely exists something better to do that.
When the box is checked, the page is refresh with only "hide=1" as an url argument, but I would love to keep all the other arguments that were there before the page was refreshed. Is there a way to refresh the page and keep all the arguments while knowing that the box is checked/unchecked ?
thanks for your help, and sorry for my poor knowledge.
Regarding the second point of your question you can move the POST (or GET) array to the SESSION one and back with the following code:
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
I use this to do exactly the same. When I reload the page I keep the posted values.
Regarding the first point you are already on the right path.
I don't see anything wrong with how you've tried to solve problem 1.
Regarding the URL problem 2, either put session_start(); at the top of the page to get the session to work correctly.
Alternatively have hidden inputs in this pages' form and echo out the previous pages' POST values.
<form action="" method="post">
<input type="hidden" name="condition" value="<?php echo $_POST['condition']; ?>" />
<!-- have hidden inputs from previous page here, plus your checkbox to retain post values from the previous page -->
</form>
Although I'd recommend POST for this, you can do GET although it gets a bit messy like so:
<form action="thispage.php?condition=<?php echo $_GET['condition'];?>" />
So I've been thinking about this for an hour and am interested in what the best way is to check which form has been submitted.
So lets say we've got formOne and formTwo that both submit to formsubmission.php and inside there I have deemed the most appropriate way to check for SUBMISSION is
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//do stuff
}
But what is the best way to actually determine WHICH form has been submitted, formOne or Two?
I have read here What is the best way to identify which form has been submitted? and am wondering whether these are the only two ways to get around this.
I figured that just checking isset($_POST[formname]) would be bad due to a few reasons I read about elsewhere, but now I am starting to think that the whole idea of posting to the same .php file is just bad practice and there is no GOOD way to check which form has been submitted doing it this way.
Is using GET and submitting each different form to a seperate ID bad practice? if so - why?
So my question is, what is the best way to check WHICH form has been submitted, that is GOOD practice?
Thanks
PS also looked at hidden fields -> doesn't seem html worthy in terms of quality
The best would be to use a different name for each from submit input :
For formOne :
<input type="submit" name="formOne" value="formOneValue" />
For formTwo :
<input type="submit" name="formTwo" value="formTwoValue" />
Then in you're php file :
if (!empty($_POST['formOne'])) {
//do something here;
}
if (!empty($_POST['formTwo'])) {
//do something here;
}
I would suggest you'd check the fields that you received if they don't have the exact same fields or have a hidden field which tells your php code which form it is, there is nothing wrong about that. However, the best option is to have separate urls for separate actions, regardless of which php file actually handles the submission in the end.
To do this you should look into urlrewrite and htaccess, which will allow you to turn a url like users/delete and users/add to myphpfile.php?action='delete' (meaning POST data is preserved) and in your php code look at the $_GET['action']value to decide which code to run.
In my CMS I have more then three from that submit onto the same page.You just need to see which form has been submitted using your submit button name and value.Your request can either be a POST or a GET. Here's one example for two form that submit onto the same page.
The form submit button name are different.
<input type="submit" name="video" id="submit" value="Save"> -- first form
<input type="submit" name="album" id="submit" value="Save"> -- second form
if(isset($_POST['video']) && $_POST['video']=='Save'){
#code for first form
}
if(isset($_POST['album']) && $_POST['album']=='Save'){
#code for second form
}
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}
I'm having an issue with a javascript interaction on a site, and I can't figure out what's going on.
The basic gist of this site is this: One single .php page that has multiple includes contained within hidden divs. When the next or back buttons are clicked one of the divs becomes active (visible), and it contains questions with image links for answers. When one of these links is clicked a javascript function is called which is supposed to set a value in a hidden form field equal to the value that's passed to the function from the link.
I've got no idea why it's not working. I've done stub testing using alerts, and everything is being passed correctly. Occasionally it'll work on one question, but never all of them. Any help would be greatly appreciated. You can find the source code here:
http://3-1.faile-test.appspot.com/kilt_page.php
It looks to me that you are setting the wrong value in the hidden fields.
For example if I enter a company name "foo inc", the field is set as:
<input type="hidden" value="company_name" id="company_name_answer">
Should it not be?
<input type="hidden" value="foo inc" id="company_name_answer">
I think that the change_selection function is wrong, you have:
function change_selection(selection, answer)
{
var selection = selection;
var answer = answer;
document.getElementById(answer).value = selection;
}
It should presumably be...
function change_selection(selection, answer)
{
document.getElementById(answer).value = document.getElementById(selection).value;
}
In case anyone else has this issue, here's how I solved it:
I added default values to the hidden form inputs. So, instead of:
<input type='hidden' name='answer_name' value='' />
I have:
<input type='hidden' name='answer_name' value='default' />