PHP form with limit some value from html post - php

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
}

Related

HTML Multiple Select form not working

I am new in PHP.
Please check the following code; multiple select is not working. Not sure why. Thanks in advance.
<form action="test.php">
<select name="fin[]" multiple ="multiple" class = "multiple_select" id = "selected_options_fin" style="height: 200px">
<option value="week_period">Week Period</option>
<option value="comments_approved">Comments Approved</option>
<option value="comment_replies">Comment Replies</option>
<option value="avarage_response_time">Average Response Time</option>
<option value="avarage_response_time_in_minutes">Average Response Time in Minutes</option>
<option value="commenter_visits">Commenter Visits</option>
<option value="percentage_of_new_visits">% New Visits</option>
<option value="number_of_goal_completions"># of Goal Completions</option>
<option value="conversions_rate">Conversions Rate</option>
</select>
<input type="submit" name = "submit1" Value = "SUBMIT" id="submit_fin">
</form>
<?php
if (isset($_POST['fin'])) {
$fin_array = $_POST['fin'];
$loop_count = 0;
foreach ($fin_array as $key => $value) {
$loop_count++;
echo 'Column $loop_count || Array Key = $key || Value = $value<br />';
}
exit();
}
Sorry for bothering
First line need to have
form action="test.php" method="post"

Select element - Post method - return of value / Php

I'm having a hard time to fix and how can make my codes work well.
My textbox echo correctly while my dropdown box is not.
Can anyone help me and also clean my codes?
I wanna know how did you do it and can u please explain it to me.
Thank you so much.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Dropdown: <select name="drpdown1" value="<?php echo $drpdown1;?>">
<option></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
</select>
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$drpdown1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$drpdown1 = $_POST["drpdown1"];
}
?>
You're not echoing the value of $drpdown1 correctly:
// this is wrong for a select:
<select name="drpdown1" value="<?php echo $drpdown1;?>">
// etc.
If you want to select automatically the previously selected value, you need to add the selected attribute:
<select name="drpdown1">
<option value="1" <?php if ($drpdown1 === '1') { echo "selected='selected'"; } ?>>Mark</option>
<option value="2" <?php if ($drpdown1 === '2') { echo "selected='selected'"; } ?>>Extreme</option>
// etc.
you have to know more about the dropdown box because you can not put the value inside the
<select value="<?php echo $drpdown1;?>">
you have to compare the value inside the option directly. example
<select name="drpdown1">
<?php
if($drpdown1 == ""){
?>
<option selected></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
<?php
}else if($drpdown1 == "1"){
?>
<option></option>
<option value="1" selected>Mark</option>
<option value="2">Extreme</option>
<?php
}
?>
</select>

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.

html select value 0 is in php validation always empty

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
}

Using $_POST to get select option value from HTML

I use select as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select option and store it into a variable for future use, in PHP?
Use this way:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option> tags.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.
Make sure to check if it exists in the $_POST array before proceeding though.
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}
You can do it like this, too:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
for php8+ versions, you can use match expression:
$select = $_POST['select1'] ?? '';
$result = match ($select) {
'value1' => 'this is value1',
'value2' => 'this is value2',
default => 'unknown value',
};
echo $result;
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];
Depends on if the form that the select is contained in has the method set to "get" or "post".
If <form method="get"> then the value of the select will be located in the super global array $_GET['taskOption'].
If <form method="post"> then the value of the select will be located in the super global array $_POST['taskOption'].
To store it into a variable you would:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
Like this:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST array is always based on the value of the name attribute of any HTML input.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>
-- html file --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);

Categories