Php/Html form with different option selection - php

my php and html coding is:
$damt2 = isset($_POST['damt2']) ? $_POST['damt2'] : 200;
$dateinfo2 = json_decode(file_get_contents($_SESSION['base_path'] . 'dl.dict'), true);
arsort($dateinfo2); //arsort — Sort an array in reverse order and maintain index association
$dateinfo2 = array_slice($dateinfo2, 0, $damt2); //array_slice — Extract a slice of the array
Table
<input type="hidden" name="userId" value="<?= $_SESSION['userId']; ?>">
<select name="damt2" onchange="this.form.submit()">
<option value="50" <?= (isset($damt2) & $damt2 == 50) ? 'selected' : ''; ?>>50</option>
<option value="100" <?= (isset($damt2) & $damt2 == 100) ? 'selected' : ''; ?>>100</option>
<option value="200" <?= (isset($damt2) & $damt2 == 200) ? 'selected' : ''; ? >>200</option>
<input name="radio" type="radio" value="cow">Cow
<input name="radio" type="radio" value="chicken">Chicken
</select> <input type="submit" name="submit" value="Show List & Save">
<?php
foreach ($dateinfo2 as $key => $time){
$uInfo = $_SESSION['data']->table('units')->byName($key)->data();
if ($uInfo["keyword"] != 'chicken') continue;
if ($uInfo["keyword"] != 'cow') continue;
if (isset($uInfo['type']) && !empty($uInfo['type'])) {
echo '<center><font color="olive">TypE: '.$uInfo['type'].'</font><br>';
}else{
}
}
?>
Why in results all blank ?
I guess where i'm doing mistake :
In foreach loop by both values are continued.
In category selection with radio button.
I want form with two options ( one with search number 50,100,200 ) and second with category like " cow " and " chicken " .

My question is solved now and i did it by reading several posts from all around the internet and at the end i tried :
IN HTML section:
<input name="cow" type="radio" >Cow
<input name="chicken" type="radio" >chicken
PHP coding :
<?php
foreach ($dateinfo2 as $key => $time)
{
$uInfo = $_SESSION['data']->table('units')->byName($key)->data();//item get data by name
if(isset($_POST['cow'])){
if ($uInfo["keyword"] != 'cow') continue;
}
if(isset($_POST['chicken'])){
if ($uInfo["keyword"] != 'chicken') continue;
}
if (isset($uInfo['type']) && !empty($uInfo['type'])) {
echo '<center><font color="olive">TypE: '.$uInfo['type'].'</font><br>';
}else{
}
}
In the results, i have two radio buttons and pulldown menu to select length(50,100,200..etc) of result with desire category (Chicken/Cow).
thank you all for trying to support me with your suggestions. Have good day to all ^_^

Related

Set default option with raw php variable (no javascript or other lib)

Hi I am trying to make dropdown with default set based on php variable but not getting desired result please help I am a new into this
<?php
if ($_POST['search'] != '') {
$searchType = $_POST['search_type'];
}
?>
<form action="" method="POST">
<select name="search_type" id="search_type">
<option value='A' <?=(isset($searchType) && ($searchType=="A"))?'selected':'';?> >A</option>
<option value='1' <?=(isset($searchType) && ($searchType=="1"))??'selected';?> >1</option>
<option value='x' <?=(isset($searchType) && ($searchType=="x"))??'selected';?> >x</option>
<option value='y' <?php (isset($searchType) && ($searchType=="y"))??'selected';?> >y</option>
<option value='z' <?=(isset($searchType) && ($searchType=="z"))??'selected';?> >z</option>
</select>
<input type="text" name="search" id="search" placeholder="Search.. " >
</form>
You can save a bunch of code by creating an array of your values and looping through them. When looping through them, you can do a check if your value matches your POST data.
arr = ['A','1','x','y','z'];
foreach ($arr as $val){
if($searchType === $val){
$sel = 'selected';
} else { $sel = '';};
echo "<option value=\"".$val."\" ".$sel.">".$val."</option>";
};

Option value selected for loop

I am trying to create a selected value of the previous output.
(the user fills in a form , submits, returns to the form) And I want the selected value to be the value the user previously used.
The vraagNummer and answer are given in the url parameter and the dropdown menu of items available in the list are created in a for loop.
Now I got stuck on that part.. How do I create an option selected value if it is created in a for loop?
Usually I would just put in option value = $vraagNummer selected> <?php echo $vraagNummer ?></option but in this case that wouldn't work. I suppose?
Anyone knows how to fix this?
Same with the A/B/C/D. How do I put in the selected value of answer into the value previously selected while still keeping the others as an option.
With kind regards,
if(!empty($_GET['vraagnummer'])){
$vraagNummer = $_GET['vraagnummer'];
if(!empty($_GET['answer'])){
$answer = $_GET['answer'];
}
}
echo $vraagNummer;
echo $answer;
?>
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
?><option value="<?php echo $i ?>">Question <?php echo $i?></option>
<?php
}
?>
</select>
<select name="result">
<option value="A">Answer A</option>
<option value="B">Answer B</option>
<option value="C">Answer C</option>
<option value="D">Answer D</option>
<input type="submit" id="submit" value="Submit" />
</form>
You simply test the $_GET['question'] against the current $i value in the loop using a ternary if
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
$sel = (isset($_GET['question']) && $_GET['question'] == $i) ? 'selected="selected"' : '';
echo '<option ' . $sel . ' value="' . $i .">Question ' . $i . '</option>';
}
?>
</select>
Warning:
Also be careful with the use of empty() when the variables can contain a zero as zero is considered as empty by the empty() function
Actually I would do your other dropdown like this
<select name="result">
foreach (range('A', 'D') as $char) {
$sel = (isset($_GET['result']) && $char == $_GET['result']) ? 'selected="selected"' : '';
echo "<option $sel value='$char'>Answer $char</option>";
}
</select>

Form showing no option is selected error message when options are selected

I have the following form:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
The way it works is that a user can select one of the two options from gender and age and search for a user. It is not required for both options to be completed to search.
However, if none of the options are selected, I want it to stay on the current page (`random_chat.php), and echo a message.
Here is my approach:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form'])){
if (empty ($refined_gender) || empty($age_from) || empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
Currently, when I have both search options empty, the message echos, which is good. But the message also echos when I am searching for a male user, or have age not empty, which shouldn't happen.
you need to change || to &&, (Some other changes are also stated as comment):-
<form method="POST" enctype="multipart/form-data"> <!-- remove action ='#' -->
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
if(isset($_POST['age_from']) && $_POST['age_from'] == $i){ // to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
if(isset($_POST['age_to']) && $_POST['age_to'] == $i){// to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info" name="submit_form" value="Click to start chat! " />
<form>
<?php
$refined_gender = isset($_POST['gender']) ? $_POST['gender'] : ''; // escape gives me undefined function error at my end so i removed it, but if it is working at your end thhen keep it up as it is.
$age_from = isset($_POST['age_from']) ? $_POST['age_from'] : '';
$age_to = isset($_POST['age_to']) ? $_POST['age_to'] : '';
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) { // check always with form posted value not submit button value and also cange || to &&
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
?>
The if condition does not reflect what you want fix it like this
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else { // search user }

how to add check bo0x in selection box with multiple selection box

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

PHP form values after POST in dropdown

I have a form with 'selected' values pulled from the database.
Now I want the user to edit the values.
When the data is send I want to show the new values.
When I submit my form I always get the 'green' value?
What am I doing wrong here?
<?php
// pulled from db
$color = "blue";
// update
if (isset($_POST['Submit'])) {
echo "write to db: " . $_POST['name'] . " + " . $_POST['color'];
}
?>
<html>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" size="30" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>">
<br />
<label for="color">Color:</label>
<select name="color">
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
<option <?php echo (isset($_POST['color']) || $color == "blue") ? 'selected="selected"' : ''; ?> value="blue">blue</option>
<option <?php echo (isset($_POST['color']) || $color == "green") ? 'selected="selected"' : ''; ?> value="green">green</option>
</select>
<br />
<input type="submit" name="Submit" value="Update">
</form>
</html>
Your conditionals all use ||. They all evaluate to TRUE when the post is set. If you look at the HTML output, every option will say selected='selected'.
Just compare $_POST['color'] to your specified string.
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
|| is the "or" operator. If $_POST['color'] is set (i.e., the form was submitted), that will always evaluate to true. You should probably just do
$_POST['color'] == 'red'
Instead. Forget the isset check.

Categories