html select value 0 is in php validation always empty - php

I try to post the selected value and check if the variable is empty.
html:
<select id="monitors-old" class="form-control" name="monitors-old">
<option value="">Auswählen...</option>
<option value="0" <?php if ($personData["cmo_mon"] == "0"){echo 'selected';}?>>0</option>
<option value="1" <?php if ($personData["cmo_mon"] == "1"){echo 'selected';}?>>1</option>
<option value="2" <?php if ($personData["cmo_mon"] == "2"){echo 'selected';}?>>2</option>
<option value="3" <?php if ($personData["cmo_mon"] == "3"){echo 'selected';}?>>3</option>
<option value="4" <?php if ($personData["cmo_mon"] == "4"){echo 'selected';}?>>4</option>
</select>
Result html:
<select id="monitors-old" class="form-control" name="monitors-old">
<option value="">Auswählen...</option>
<option value="0" selected="">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
POST Check:
if (empty($_POST["monitors-old"])) {
$errors[] = "Alt-Monitore is required.";
die;
} else {
$monitors_old = validateInput($_POST["monitors-old"]);
}
the value 0 is always empty and the script fired the die, all other values are working.
Is the value 0 like ""?
Also tried:
<select id="monitors-old" class="form-control" name="monitors-old">
<option>Auswählen...</option>
<option selected="">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
This is also working, but the same issue. And another question, why is the selected allocated to ="" ? I thought it is only a tag for html, that this value is the selected?
validateInput:
function validateInput($value) {
$value = trim($value);
$value = stripslashes($value);
$value = htmlspecialchars($value);
return $value;
}

The empty() function will return TRUE if the value is even 0.
So you should use the isset() function and != operation for checking
if(isset($_POST["monitors-old"]) and $_POST["monitors-old"]!=''){
// code here
}
else{
// code here
}

Related

Show or hide HTML code based on URL

I am working on a form and I've stumbled upon a problem. I have different users in which I want them to see different values for one of my SELECT tags; so far I am using PHP to get the URL and if $field == 'donor' then it takes off some of the options. Here's the code:
function defineUser($field){
if(isset($_GET['user']) && urldecode($_GET['user'])){
$field = urldecode($_GET['user']);
if($field == "donor"){
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
} else if ($field != "donor") {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
} else {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
}
This is my function, now on the page source itself I simply have,
<?php echo defineUser($_GET['user']); ?>
My question simply is how can I get each user to see a standard options set - inside my select tag - based on the URL the user has been sent from?
Your code could be simplified:
function defineUser() {
$user = isset($_GET['user']) ? urldecode($_GET['user']) : null;
switch ($user) {
case 'donor':
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
break;
default:
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
You don't need to pass it $_GET['user'] since that is globally accessible, unless you're worried about dependency. Second, you were essentially repeating the same block of HTML twice in your if else clause, using a switch here makes more sense.

php form using array

I want to create a form using array that user can select a number and echoes the number's corresponding object name after submit. I don't know why this code does not work, could someone please teach me how to do it the right way :( Thank you so much for your time.
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['obejct'];
echo "<p>I have $train!</p>";
}
?>
Thank you so much!
Looks like you're setting $train to the value of whatever the form passes for the "object" select field, and then echoing that. You would expect then to see a number between 0 and 8, or the word "all" print out, but your reference of the object key has the word "object" misspelled as "obejct", so my guess is you're getting nothing to print as the value of $train.
Either way, what you really want to do is print the value at the key in the $train array that corresponds with what was provided by the user. This means that once you've created your array, which functions as a map, you must select the item from the array that you want to print.
You also need to handle the "all" case or you will get an error.
Here's how it would look if you continue using the array option:
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
if ($_GET['object'] != 'all') {
//Handle the non-all case
$value = $train[$_GET['object']]; //This references a key in your array, like $train[0]
echo "<p>I have $value!</p>";
} else {
//Handle the all case here
}
}
?>

PHP form with limit some value from html post

my html code :
<form action="myfile.php" method="POST">
Data
<select id="data" name="data" class="form-control" required>
<option value="data1">Data 1</option>
<option value="data2">Data 2</option>
</select>
Value
<select id="value" name="value" class="form-control" required>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</form>
This my php code :
<?php
$data = $_POST['data'];
$value = $_POST['value'];
if code {
} else {
HERE MY PROCCESS
}
?>
How if Data 1 is selected and value is must more than 3,if not it will get notice "Value of Data 1 is must more than 3" and if not error it will return to my proccess
Check:
if($data == "data1" && $value == 1) {
// notice
} else {
// process
}
Try this
if($data == "data1" && $value > 3) {
//everything is fine
} else {
// through notice
}

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

Change selected option in select (form)

I have an intial group of options like these:
<select name="item[type]">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
I want to check if a variable isset($test) is defined. If it is, then I want to change the option selected where the value is equal of $test. Something like this <OPTION SELECTED>
For example. $test = 3; so, the option selected should be fourth. If $test is empty or not defined, then the first should be the option that is selected.
One way:
<select name="item[type]">
<option <?=$test==0?'selected="selected"':'';?> value="0" class="dr">First</option>
<option <?=$test==1?'selected="selected"':'';?> value="1" class="dr">Second</option>
<option <?=$test==2?'selected="selected"':'';?> value="2" class="dr">Third</option>
<option <?=$test==3?'selected="selected"':'';?> value="3" class="dr">Fourth</option>
</select>
Another:
<? $selected[$test] = 'selected="selected"'; ?>
<select name="item[type]">
<option <?=$selected[0];?> value="0" class="dr">First</option>
<option <?=$selected[1];?> value="1" class="dr">Second</option>
<option <?=$selected[2];?> value="2" class="dr">Third</option>
<option <?=$selected[3];?> value="3" class="dr">Fourth</option>
</select>
<select name="item[type]" id="selectBoxId">
<option value="0" class="dr">First</option>
<option value="1" class="dr">Second</option>
<option value="2" class="dr">Third</option>
<option value="3" class="dr">Fourth</option>
</select>
<script type="text/javascript">
var test = "<?= $test; ?>";
if (test != '' && parseInt(test)) {
document.getElementById('selectBoxId').selectedIndex = test;
}
</script>
Remove "[type]" from select name, make it simple to "item".
Then execute this code.
$test = isset($_POST['item']) ? $_POST['item'] : "0";
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>";

Categories