I am having troubling getting a simply if/else conditional for options in an html select form. If the value for bid is 'N', then I want one option for the select field to change to "Y" and vice versa. I know my db connection is good and other php works and the form was updating the db fine until I tried to work in the php conditional code within the so I just need to get the syntax correct.
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)){
?>
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
<?
}
if statement expect expression and you have syntax error in your statement
if (echo $row['bid']; == 'N')
the syntax error because of echo and the semicolon ;
you should change your condition to
if ($row['bid'] == 'N'){
if $row['bid'] string then you need to compare with strcmp , like this
if (strcmp($row['bid'] , 'N') == 0){
also you need to move <input> tag outside the <select>
Your <input type="submit"> was inside your <select>, so I moved it outside.
It is also incorrect to use the if statement in this manner:
if (echo $row['bid']; == 'N')
correct would be this:
if ($row['bid'] == 'N')
Here is the corrected code:
<form method="post" name="update" action="update.php" >
<select name="bid">
<?php
echo '<option value="'.$row['bid'].'" selected="selected">'.$row['bid'].'</option>';
if ($row['bid'] == 'N') echo '<option value="Y">Y</option>';
else echo '<option value="N">N</option>';
echo '</select>';
?>
<input type="submit" name="Submit" value="update" >
</form>
Your condition is bad, you must remove echo and ;. it should like this
<? if ($row['bid'] == 'N') { ?>
And your submit button should be presented out side the select field.
Your codes should look like this:
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?=$row['bid']?>" selected="selected">
<?=$row['bid']?>
</option>
<?php if ($row['bid'] == 'N') {
echo '<option value="Y">Y</option>';
} else {
echo '<option value="N">N</option>';
}
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
Your if condition has bad syntax. Remove the semicolon and echo statement.
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
<input type="submit" name="Submit" value="update" >
</select>
</form>
Related
I tried putting the loop inside but then when I pick a category it won't print in the echo and if the loop is outside like right now each category is printed alone I want them all in one tab and one go button
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<form action="" method="post">
<select name="work_place">
<option value="<?php echo $category;?>"><?php echo $category;?></option>
</select>
<input type="submit" value="go" />
</form>
<?php
}
<?php
$post = (isset($_POST['work_place'])) ? $_POST['work_place'] : '';
echo $post;
?>
Image of current issue:
<form action="" method="post">
<select name="work_place">
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<option value="<?php echo $category;?>"><?php echo $category;?>
</option>
<?php
} ?>
</select>
<input type="submit" value="go" />
</form>
You want to loop over 'option', only.
You do not want multiple forms and 'select' inputs.
I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</body>
</html>
I have a form with an input field. It works very well. When I write something into the input field and click submit, the value is stored into my database:
<form action="update.php?id=<?php echo $id?>" method="post">
<input name="option" type="text" value="<?php echo !empty($option)?$option:'';?>">
<button type="submit">Submit</button>
</form>
But I don't need an input field, I need a select box instead. I tried this, but it is not working:
<form action="update.php?id=<?php echo $id?>" method="post">
<select>
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
<button type="submit">Submit</button>
</form>
Do you have an idea what I did wrong?
missing name in select
<select name="option">
<?php
$default = "No";
$options = array("Yes","No");
foreach($options as $val) {
echo ($val == $default) ? "<option selected=\"selected\" value=\"$val\">$val</option>":"<option value=\"$val\">$val</option>";
}
?>
</select>
Missing name attribute of select input.
<select name="option">
You can access it by -
$_POST['option'] // for POST method
// sample code if you need call the value from database here for static value
<form action="update.php?id=<?php echo $id?>" method="post" >
<select name="option">
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
</select>
<button type="submit">Submit</button>
</form>
I finally found the answer:
<select name="option">
<option <?php if ($option == Yes ) echo 'selected'; ?> value="Yes">Yes</option>
<option <?php if ($option == No ) echo 'selected'; ?> value="No">No</option>
</select>
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
When I submit this form, I stay on the same page, but I want a the same time to keep the option selected previously option visible.. How can I do that?
I tried:
document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";
But it did not work..
here:
foreach($allemp as $row){
echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
Try this :
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
added a line above echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)
Added '.$selected.' in side options.
Your code becomes : Copy paste below code instead of yours
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
} ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
I have a very simle form, i wish the user to choose whether to sort ascending or decending. From the select form, I will use the answer to give the search results in required order. My problem is that the form is not giving a result to the page and both 'if' statements are satisfied. I am completely stumped. Can any one shed light? Thank you
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
?>
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
<?php if($_POST["thesort"]=="Lowest"){ echo 'selected="selected"';}
{
echo "<p> choice is ASC </p>";
?>
Why double curly braces? PHP will execute the second one in ever case.
if($_POST["thesort"]=="Highest")
{ echo 'selected="selected"';}
{echo "<p> choice is DESC </p>";}
Your code modified:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
if($_POST["thesort"]=="Highest"){
echo 'selected="selected"';
echo "<p> choice is DESC </p>";
}
if($_POST["thesort"]=="Lowest"){
echo 'selected="selected"';
echo "<p> choice is ASC </p>";
}
?>
This is a problem:
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
a) Those brackets aren't doing what I think you think they're doing. In particular, the second set are irrelevant; that code will always execute
b) Why are you echo'ing 'selected=..' here? It's not in the context of an open <option tag. For example, you probably want something like:
echo '<option value="Highest';
if ($_POST["thesort"]=="Highest")
{
echo ' selected="selected"';
}
echo '">Highest first</option>';
<?php
$sort = 'Lowest'; // define the default
if (isset($_POST['thesort']) && $_POST['thesort'] == 'Highest') {
$sort = 'Highest';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest"<?php if ($sort == 'Lowest') print(' selected="selected"'); ?>>Lowest first</option>
<option value="Highest"<?php if ($sort == 'Highest') print(' selected="selected"'); ?>>Highest first</option>
</select>
</form>