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
}
}
Related
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.
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>
How would i go about writing a if statement to check if the value of a option menu is empty and if so not to show it.
<option value="">GUYS AND GIRLS</option>
if value has nothing in it it wont show this option in the select menu
but if.
<option value="http://test.com">GUYS AND GIRLS</option>
It has something there it will show up in the drop down.
I see two possible solutions. The first is if have already have the value separated somewhere, then check it and then use php to replace the empty value. If you only have access to that statement in your if statement, then use a substring to extract the value and then compare it.
Put your options in an array and run a foreach loop looking for an empty value:
//put the dropdown option in an array
$options = array('<option value="">GUYS AND GIRLS</option>',
'<option value="http://test.com">GUYS AND GIRLS</option>');
//run a for each
foreach($options as $currentOption){
//look for empty value
if(!strstr('value=""', $currentOption)){
echo $currentOption;
}
}
EDIT, BASED ON COMMENTS
Try this, if it is possible for you to implement.
<?php
if(meta('guys') != ""){
echo '<option value="' . meta('guys') . '">GUYS</option>';
}
?>
My associative array.
$money = array("Nickels" => $_POST["nickels"], "Dimes" => $_POST["dimes"], "Quarters" =>$_POST["quarters"]);
My html form set up to handle Quarters,
Dimes and Nickels are not shown in this case for brevity.
<label><b>Quarters:</b></label>
<select name="quarters" >
<option value=".25">25c</option>
<option value=".50">50c</option>
<option value=".75">75c</option>
<option value="1">$1.00</option>
</select>
A user can only select either Quarters only, Dimes only, or Nickels only.
If a user selects Quarters with the option value of .25, this value will be sent to
the php script.
So I was just wondering for calculations based on the fact that the user can select
Quarters only with one value, Dimes only with one value, and Nickels only with one
value, and not a combination of denominations,
how would one go about setting up different test cases, for example if the user selects
$money["Quarters"]; // With different values coming from the html form as .25, .50,.75, or 1, and only one of the selected values will make it through to the php script depending on what the user selected.
Can I do this:
switch($selection)
{
case “Quarters”:
echo “ You chose $money[‘Quarters’]; .<br />”;
break;
case “Nickels”:
echo “You chose $money[‘Nickels’]; .<br />”;
break;
case “Dimes”:
echo “You chose $money[‘Dimes’]; . <br />”;
break;
default: print “Please select a Denomination”;
}
Thank you for not flaming the newb, I am still learning, and sorry for the mix and match in terms of " and “.
Selected values in a form are submitted as $_POST['quarters'].
I understand, that you want to check, if the user has selected more than one of your <select>s (correct?)
So, I'd create a check like this:
$selected = 0;
if ($_POST['quarters'] != "DEFAULT_VALUE_OF_YOU_SELECT_QUARTERS")
{
$selected++;
}
if ($_POST['nickels'] != "DEFAULT_VALUE_OF_YOU_SELECT_NICKELS")
{
$selected++;
}
if ($_POST['dimes'] != "DEFAULT_VALUE_OF_YOU_SELECT_DIMES")
{
$selected++;
}
if ($selected > 1)
{
// The user has selected more than one
}
There's a few things to pay attention to here.
So, first of all, your $money array captures every value the user submits.
Next, the way you have your HTML <select> statement set up, there's no default value. The first option in $_POST["quarters"] is going to be .25 even if the user never touches that pulldown. To avoid this, you would want to add to the Quarters <select>:
<option value="0">-- Select Quarters --</option>
But still this doesn't allow you to use a switch/case statement. You're still going to be submitting a value for EVERY HTML <select> tag. Florian Peschka's solution is better, since it checks every pulldown, making sure they only used one, and perhaps displays a message if they don't.
Finally... I don't know what $selection refers to in your example. You're populating the $money array, so the $selection variable doesn't exist.
I can further help if you clarify your question.
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!