Redirect and set_select problem - php

I am using CodeIgniter 1.7.1.Ok here is the scenario.When ever the form is submitted,i need to do two things
1) persist the value selected in dropdown.
2) using session->set_flashdata(),i need to set the custom database message.
Now as we know,we need to redirect before this flash data can be set.
This is the code which i have written.
if ($this->form_validation->run() == TRUE){
$this->session->set_flashdata(‘msg’, ‘Taha Hasan’);
redirect(current_url());
$this->ShowReceiveInventoryView();
}
Also i m using set_select in the dropdown view to persist the value.
<select name=“myselect”>
<option value=“one” <?php echo set_select(‘myselect’, ‘one’, TRUE); ?> >One</option>
<option value=“two” <?php echo set_select(‘myselect’, ‘two’); ?> >Two</option>
<option value=“three” <?php echo set_select(‘myselect’, ‘three’); ?> >Three</option>
</select>
Now here is the problem…The flash message appears BUT because i am redirecting to the current page,the drop down set_select value is lost !!! Default value appears in the selection :(..If i remove the redirect line in the code,the dropdown value is presisted but Flash data is not set !!!
Hope you guys have a solution to this problem…

set_select() only works when the $_POST array has content (as you've discovered), but your redirect is obviously a GET request.
The proper way to handle this is to perform your query within the Controller, passing the object that is being edited to your view. Within your view you then repopulate your form, or set default values, based on $_POST if it exists or based on the passed object.
Let's assume we are editing a product, which has the myselect (a horribly named field) property. We will use PHP's ternary operator to test if the value of the product's myselect parameter is equal to the current option - if so, we'll use set_selects()'s third parameter to set the default.
<option value="one" <?php echo set_select('myslect', 'one', ((!$product) || !$this->input->post('myselect') && $product->myselect == 'one' ? TRUE : FALSE); ?>One</option>
<option value="two" <?php echo set_select('myselect', 'two', (!$this->input->post('myselect') && $product->myselect == 'two' ? TRUE : FALSE); ?>Two</option>

Related

Form not posting one field

I ran into a problem in my php/html code here, and I can't seem to locate the bug. The form seems to be posting everything except this one field, which seems to return "empty" every time.
(trying to set the variable "active" from the "select" option in the form, defaults to database value. The problem is, it's always returning to the default [database] value, regardless of the "select" option below...even after posting the form.) *Note that the database value of "active" is a 0/1 bit value. Does this affect the result?
php:
$active = (!empty($_REQUEST["active"]))?$_REQUEST["active"]:$row["active"];
html:
<select class="sel" name="active" id="active">
<option value="0" <?php echo ($active=="0"?"selected":"");?>>Not Active</option>
<option value="1" <?php echo ($active=="1"?"selected":"");?>>Active</option>
</select>
In your PHP code, empty will return true for the string "0", thus setting $active to the pre-existing value in such cases. What you maybe want instead is something like:
$active = (array_key_exists('active', $_REQUEST) ? (!empty($_REQUEST['active']) ? true : false) : $row['active']);
This will set the $active variable to true if the provided string is not considered empty (i.e. anything other than 0 or an empty string), false if it's present but empty, and preserve the existing value if the array key doesn't exist in the data.

Re-populate checkbox if it fails in validation in an edit form in Codeigniter

I works on the an data-edit form in codeigintier. And the problem is about re-populate checkbox
It works if it is an add form (that means I need not concern about the value in database):
<?= set_checkbox('is_default', '1'); ?> for checkbox
The problem is, in the edit form:
I can't repopulate the checkbox
<?php if ($customer_group[0]['is_default'] == "1") echo "checked"; set_checkbox('is_default', '1'); ?>
The checkbox will check even I have not check it in the edit => fail to validate in the form, thanks for helping
I have already set the validation rule in controller, the code in the add form is working , but how to handle the case for edit form?
In order to re-populate checkbox following code might be helpful:
set_checkbox('fieldName', 'fieldValue');
Where 'value' is the second parameter of the form_checkbox call. Like this:
form_checkbox('fieldName[]', 'value', set_checkbox('fieldName', 'value'));
Now if you are on edit form then below code might help you
$getVal=$valFromDb; //$valFromDb is actually value of the filed from db as you are on edit page
if($getVal!=0){
{
echo form_checkbox('fieldName[]', 'value', true);
}
else
{
echo form_checkbox('fieldName[]', 'value', false);
}
set_checkbox takes a third argument to set the default state, so basically you have to do something like this
echo set_checkbox('is_default', 1, $customer_group[0]['is_default'] == "1");
Can give one suggestion??
1. Hide all the checked value of checkbox in input box when you are directed towards edit page.
If checked box is checked in edit page, edit the value of hidden input field of textbox value.
Submit it, when validation failed, checked or repopulate the checkbox value according to hidden field value. send checkbox value of checked box field through array from controller to edit page view like this. e.g $data['repopulate_checks'] = $this->input->post('array name of checkboxs');
In view :
getit like this
$catch_checkbox = $repopulate_checks;
You can directly get through $repopulate_checks also.
Hope this help you.
You can use form_checkbox() function: Guide
$isChecked = False; // or True for default value
If have stored data then:
$isChecked = $customer_group[0]['is_default'];
echo form_checkbox('input_name', 'value', $isChecked);
or the hard way:
set_checkbox():
The first parameter must contain the name of the checkbox, the second
parameter must contain its value, and the third (optional) parameter
lets you set an item as the default (use boolean TRUE/FALSE)
<input type="checkbox" name="is_default" value="1" <?php echo ($customer_group[0]['is_default']) ? set_checkbox('is_default', '1') : '' ; ?>/>
set_checkbox takes a third argument to set the default state, so basically you have to do something like this
$checked = FALSE; if($customer_group[0]['is_default']){ $checked = TRUE; }
echo set_checkbox('is_default', 1, $checked);

why select option value if zero post empty

I am sorry bad English. I have a select menu option value problem:
<select>
<option value=0>element1</option>
<option value=1>element2</option>
</select>
I am if select value="0" option and this post, it retuns the value with no problem. But, when I use value = 0 save mysql table, this value not 0 this return empty. This value saving column type integer? What is problem?
I am not using mysql_real_escape_string or any filter. If select value="1" option no problem, it is saving succesfully.
Thanks
I ran into this issue myself and what I found was that when posting option/select box values and reading them with PHP, when the 'value' of it was 0 it was acting like I did not post at all because I was checking to see anything had been posted like this:
if ($_POST['status']) {
// do stuff here
}
When in fact I needed to check it using isset(), because the way shown above returns false even if the value is 0. The example shown below checks to see if it really is set at all, even if the value is 0.
if (isset($_POST['status'])) {
// do stuff here
}
You did not show us your PHP code so I am only guessing this was also your problem.
you missed quotes (")
<select name="my_select">
<option value="0">element1</option>
<option value="1">element2</option>
</select>
<?php
$my_select_value = $_POST["my_select"];
echo $my_select_value;
?>
This one is a pain...no value is sent in the POST if it's a select and the value is 0....so if it's a POST and NOT sent you can assume the value was 0...
if($_POST){
if(isset($_POST['my_select'])){
//has a value, can assume it's 1 given the example
}else {
//posted, but value wasn't sent, can assume it's 0
}
}

Dynamic default selection for a drop down menu using url variables

I have several links on one page which all direct to the same page, but each of them passes a different variable in the URL (http://sitename.com/thispage.php?id=3). I set new variables to true or false depending on which value is passed for "id", then put the true/false variables in to set the default option on an option tag like so:
if (isset($_GET['id']))
$id = ($_GET['id']);
if ($id = 4)
$id4 = true;
else
$id4 = false;
...
<option value="Value" selected="<?php echo "$id4"; ?>">
Value
</option>
Excuse me if it seems messy.
I've got 12 of those setup, one for each menu option. I can't seem to get it to display the correct one as the default selection though, as it always selects the 2nd to last menu item.
One thing you should correct is putting true and false in quotation marks, otherwise they will be converted from boolean to string and end up as a 1 or 0.
Or you could replace the whole thing with this
<option<?php if (isset($_GET['id']) && $_GET['id'] == 4){ echo ' selected'; } ?>>
value
</option>

'Remember' form drop list value when submitting to SELF?

I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared).
I have solved this in regular fields and checkboxes like this:
<input type="text" name="headline" id="headline" value="<?php echo #$_POST['headline'];?>">
But how can I do this with drop lists? or radio buttons? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. Didn't work!
So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ?
If you need more input let me know, thanks!
For selects, you need to compare each option to your posted value, and handle it individually. Simply print out your options in a loop, and test each value against the value was was previously posted. If it maches, add selected to the attributes of that particular option.
$color = $_POST["colors"];
$colors = array("red","green","blue");
<select name="colors">
<?php foreach ($colors as $option) { ?>
<option<?php print ($option == $color) ? " selected" : ""; ?>>
<?php print $option; ?>
</option>
<?php } ?>
</select>
Actually, found out that it is possible to set the selectedIndex with javascript...
So I could put the selectedIndex in a hidden input before submitting the form, and then get that selectedIndex and set it with a javascript function... tricky but suits me better in this case...
document.getElementById("select").selectedIndex=nr;
Thanks though Jonathan!

Categories