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.
Related
I am working on a project in which I have used JSON file to store data and used PHP to print it. But while comparing the value from $_POST, if it has spaces then comparison is not working otherwise there is no issue
This is what I am doing
$jsondata = file_get_contents("location-to-my-file-storage/".$_POST['mandi']."/".$_POST['crop'].".json");
$json = json_decode($jsondata, true);
foreach($json['crop'] as $data)
{
if ($data['name'] == $_POST['name'])
//This if statement is responsible for error
/* if $_POST['name'] = some value, then it's not working
but for $_POST['name'] = someValue, it is working properly
I have also tried if ($data['name'] == "$_POST['name'])"
but this is not working also
*/
{
/******
*do some printing here
*/
}
You can see a demo at http://divakarparashar.hol.es/innovation/en/user/farmer/price-calc.php
Select anything from two drop down list and click on available crops, it gives list for available crop. here comes the error, for a value with spaces like the first one 'Acorn Squash' nothing is happening, but for a value that has no space in it like 'Amarnath' every thing is working properly.
The same thing happened when I go for a mandi name or crop type with spaces in it, for the statement
$Jsonfile = file_get_contents("location-to-my-file-storage/".$_POST['nameMandi']."/".$_POST['cropType'].".json");
Where I am getting wrong ..??
please check your dropdown i.e <option> tags.
The value has cutted string in there thats why comparison is not working.
<select name="name" class="w3-btn w3-green w3-round w3-input">
<option name="crop" value="Acorn" squash="">Acorn Squash</option>
<option name="crop" value="Alfalfa" sprout="">Alfalfa Sprout</option>
<option name="crop" value="Amaranath">Amaranath</option>
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
}
}
I'm having a real problem with retrieving the value of a radio button using PHP . . . I have two radio buttons as such:
<input name="admin" type="radio" value="1" />Yes
<input name="admin" type="radio" value="0" checked />No
And a conditional PHP statement checking to see if PHP can retrieve any data from it:
if(!empty($_POST['admin'])) {
// do stuff
}
else {
echo "Value not set";
}
the problem is that PHP can seem to return a value for the radio buttons if "yes" is selected, but not if "no" is selected, I've tried removing the "checked" portion, to no avail. I just can't get it to retrieve the "0" value whatever I try.
I remember using PHP arrays to name checkboxes, but this shouldn't be needed for radio buttons surely, as only can be selected at any one time?
Or does PHP just have a problem returning radio buttons with a value of 0?
Or am I doing something horribly wrong without realising it?
PHP function empty will return false for 0, NULL, "", and others.
Quoting from php.net:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
All of these will return (false) therefore nullifying the execution of your if statement.
You will have to modify your condition to reflect a value depending on what you wish to accomplish.
Good luck!
Your problem is that empty() treats zeros as empty values, no matter if it's a number 0, or a string '0'. So, instead of using empty() use isset() and/or direct checks, e.g.
if (isset($_POST['admin'])) {
or
if (isset($_POST['admin']) && $_POST['admin'] == 0) {
I believe the zero is your problem. Try setting them to "Y" and "N" and you should see that the N value comes through. This is because 0 is an empty value in PHP.
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>
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>