I'm having an edit form and within a dropdown box with movie-titles.
One of them should be selected though, so say DVD_id: 100.
How do I make this work in the code below?
<div class="form-group">
<label for="Site name" class="col-md-4 control-label">DVD Title</label>
<?php
$query = "Select DISTINCT dvd.dvdname, dvd.dvd_id, FROM DVD where ORDER BY dvd.dvdname";
$result = mysql_query($query) or die ("Couldn�t execute query.");?>
<div class="col-md-8">
<select id="box" name="dvdtitlefield" class="selectpicker form-control" multiple title="Choose one of the following..." >
<?php
while ($row = mysql_fetch_assoc($result))
{
extract($row);
echo '<option value="'. $dvd_id .'">'. $dvdname .'</option>';
}?>
</select>
</div>
</div>
use the selected attribute of the option tag to make any option by default selected
<select>
<option value="tiger">tiger</option>
<option value="lion">lion</option>
<option value="zebra">zebra</option>
<option value="cat" selected>cat</option>
</select>
In your code you can just check which option you want to be selected by default and apply an if condition for it
example
<div class="form-group">
<label for="Site name" class="col-md-4 control-label">DVD Title</label>
<?php
$query = "Select DISTINCT dvd.dvdname, dvd.dvd_id, FROM DVD where ORDER BY dvd.dvdname";
$result = mysql_query($query) or die ("Couldn�t execute query.");?>
<div class="col-md-8">
<select id="box" name="dvdtitlefield" class="selectpicker form-control" multiple title="Choose one of the following..." >
<?php
while ($row = mysql_fetch_assoc($result))
{
extract($row);
if($dvd_id==100)
{
echo '<option value="'. $dvd_id .'" selected>'. $dvdname .'</option>';
}
else
{
echo '<option value="'. $dvd_id .'">'. $dvdname .'</option>';
}
}?>
</select>
</div>
</div>
Related
Vs code
Output
<div class="form-group">
<label class="control-label col-lg-4" for="username">Menu</label>
<div class="col-lg-8">
<select class="form-control select2 " id="exampleSelect1" name="menu[]" multiple='multiple' style="width: 100%;height:200px" placeholder="Select multiple members">
<?php
$m = mysqli_query($con,"SELECT * FROM combo_details natural join menu where combo_id='$id'");
while ($rowm = mysqli_fetch_assoc($m)){
?>
<option selected value="<?php echo $rowm['menu_id'];?>"><?php echo $rowm['menu_name'];?></option>
<?php
}
?>
<?php
$result = mysqli_query($con,"SELECT * FROM menu");
while ($row = mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $row['menu_id'];?>"><?php echo $row['menu_name'];?></option>
<?php
}
?>
</select>
</div>
</div>
tried changing selected value and i even tried to delete it and merge it but it doesnt work. need help
What is the best way to return exactly the select option that the end user chose?
In my query for example when selecting the imoveis_tipo
Returns me all the data being HOUSES, APARTMENTS, TERRENOS;
How do I return only 1 purpose type?
If my question was not very clear, please comment here so I can adjust my question.
Here's an example showing what I'm trying to do, by selecting PURPOSE as SALE
PROPERTY TYPE as 2 BEDROOM APARTMENT
And the CITY as CAPÃO DA CANOA
Returns me all the records in the database and not just the ones I selected in the search.
This is my code showing the purpose, type and city:
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Finalidade</label>
<select id="finalidade" name="finalidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_finalidades WHERE status='S' ORDER BY nome");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Tipo de Imóvel</label>
<select id="tipo" name="tipo" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_tipos WHERE status='S' ORDER BY nome ");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Cidade</label>
<select id="cidade" name="cidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql6 = mysqli_query($link, "SELECT * FROM localidades_cidades WHERE status='S' ORDER BY nome");// or die (mysqli_error($link));
while ($dados6 = mysqli_fetch_array($sql6)) {
?>
<option value='<?php echo $dados6['url'] ?>'>
<?php echo ($dados6['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
Okay, so I have got this form with pairs of fields... One is select, and the other is number input. If the select value is not null, the number input appears beside it... After submitting, I need to retrieve the name of the select menu, It's value And the value of the number input besides it...
The whole thing is getting impossible for me as the names of all these fields are being fetched from SQL.
Here's the image of the page I'm making:
Preview Of My Form
https://drive.google.com/file/d/1Qv1Hr8tSIj95qBb98vCV1kftdftnaGGb/view?usp=sharing
And the HTML Given below (Note that these are all copied from the browser source.. In real these are all fetched from the SQL)
<form class="predictForm" name="predictForm" id="predictForm" autocomplete="off">
<div class="form-group clearfix" data-question="1">
<div class="inner-form-group predictType">
<label>Match Winner</label>
<select class="form-control match_winner" name="match_winner" id="match_winner">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal" style="display: block;">
<label>Points</label>
<input type="number" class="form-control match_winner_val" name="match_winner_val" id="match_winner_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="12">
<div class="inner-form-group predictType">
<label>Best Batsman</label>
<select class="form-control best_batsman" name="best_batsman" id="best_batsman">
<option value=""> - Select Player - </option>
<option value="7" title="team6">Name 1</option>
<option value="9" title="team5">Name 2</option>
<option value="10" title="team5">Name 3</option>
<option value="12" title="team8">Name 4</option>
</select>
</div>
<div class="inner-form-group predictVal" style="display: block;">
<label>Points</label>
<input type="number" class="form-control best_batsman_val" name="best_batsman_val" id="best_batsman_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="8">
<div class="inner-form-group predictType">
<label>Best catch done by</label>
<select class="form-control best_catch_by" name="best_catch_by" id="best_catch_by">
<option value=""> - Select Player - </option>
<option value="3" title="team5">Name 1</option>
<option value="7" title="team5">Name 2</option>
<option value="8" title="team8">Name 3</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control best_catch_by_val" name="best_catch_by_val" id="best_catch_by_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="4">
<div class="inner-form-group predictType">
<label>Runner Up Team</label>
<select class="form-control runner_up_team" name="runner_up_team" id="runner_up_team">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control runner_up_team_val" name="runner_up_team_val" id="runner_up_team_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="5">
<div class="inner-form-group predictType">
<label>Max Six</label>
<select class="form-control max_six" name="max_six" id="max_six">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control max_six_val" name="max_six_val" id="max_six_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix">
<button type="submit">
<span class="front">
<i class="fas fa-paper-plane"></i>
<span class="value">Send <span>Predictions</span></span>
</span>
<span class="back">
<i class="fas fa-paper-plane"></i>
<span class="value">Send <span>Predictions</span></span>
</span>
</button>
</div>
</form>
In short, I need to insert the
Name, and value of the select field;
The value of the number Input (Without the field name)
into the database..
What I've tried so far..
I've tried to post all the values and split it into different arrays of four. But it didn't work as expected as the third element in the array is not needed, and due to some other reasons..
I made a hidden element in the html, which gets updated with the field name, and values with an array; With an on change function for both select, and number input. I then POST it along with other data. But it creates more bugs than the one we solves, as different users interact with the form in different ways.. (Note that None of the fields are required.. All of them are optional..)
I Googled my issue🙄... Many many many times😫😫
<?php
foreach ($_POST as $key => $value) {
if($value != "") {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
If you are sending the Form content to php and need to get the Values of your inputs, they are all present in the $_POST array.
every input (also selects) are present with its html-name attribute.
for your Form-Element "runner_up_team" you could access it like this:
$val = $_POST["runner_up_team"]; // $val contains the seleceted value from the html-select
So if you want to get the number for a selected field i would suggest to use something like this:
$result = new array();
foreach ($_POST as $inputName => $value) {
// We only want to inspect the selects and retrieve the number value later.
if (strpos($inputName, '_val') === FALSE) {
if ($value != '') {
// With the current select name + the suffix we get the name for the number input field
$numberInputName = $inputName . '_val';
// Now we can access the value of the number input with the name.
$numberInput = $_POST[$numberInputName];
// now you can put your data into a result array.
$result[$value] = $numberInput;
}
}
}
The result array should now look like this:
["runner_up_team" => 43, "max_six" => 32]
If you now want to access it in a loop, you can use your previous example, but use the resultarray:
<?php
foreach ($result as $key => $value) {
if($value != "") {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
UPDATE
For your question in the comment:
replace following line
$result[$value] = $numberInput;
with
$element = new array();
$element['select_name'] = $inputName;
$element['number'] = $numberInput;
$element['select_value'] = $value;
$result[] = $element;
This will change the second foreach to:
<?php
foreach ($result as $value) {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $value['select_name'];
echo "</td>";
echo "<td>";
echo $value['select_value'];
echo "</td>";
echo "<td>";
echo $value['number'];
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>
I have HTML form which have select options like below
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<?php if ($row[occupation] == 0){
}?>
<option value="0">Parent</option>
<option value="1">Teacher</option>
</select>
</div>
I want
option selected called Parent if ($row[occupation] == 0) else want
show Teacher as selected
. I am new in PHP and does not know how I can I do it. Let me know if someone can help me for do it.
Thanks
You have to echo selected='selected' based on the value of $row['occupation'], like this:
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<option value="0"<?php if($row['occupation'] == 0){ echo " selected='selected'"; } ?>>Parent</option>
<option value="1"<?php if($row['occupation'] == 1){ echo " selected='selected'"; } ?>>Teacher</option>
</select>
</div>
You can use this one.
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name="occupation" required>
<option value="0" <?php if ($row['occupation'] == 0) { echo 'selected'; } ?>>Parent</option>
<option value="1" <?php if ($row['occupation'] == 1) { echo 'selected'; } ?>>Teacher</option>
</select>
</div>
Here's my drop down:
<form name="form" method="POST" style="display:inline;">
<select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Category</option>
<?php
$sth = $conn->prepare('Select name From category');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
}
?>
</select>
</form>
And I'm trying to access it using the following code:
if(!empty($_POST['category']))
$category=$_POST['category'];
When I echo the value of $category, it returns null. Why is that? How can I get the value of the selected value from the drop down?
Hi In PHP we can submit form using a submit button or using javascript.
if you want to use only php you shoud add a submit button and check if that is posted then check for category like this.
<form name="form" method="POST" style="display:inline;">
<select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Category</option>
<?php
$sth = $conn->prepare('Select name From category');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
}
?>
</select>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$category=$_POST['category'];}
or you can try javascript for submission it will work on change of value in select box
<form name="form" method="POST" style="display:inline;">
<select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;" onchange="document.form.submit();">
<option value="" disabled selected>Select Category</option>
<?php
$sth = $conn->prepare('Select name From category');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
}
?>
</select>
</form>
<?php
if(!empty($_POST['category']))
$category=$_POST['category'];
You need to use input type submit in form to get values in php
<form name="form" method="POST" style="display:inline;">
<select name="category" id="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
<option value="" disabled selected>Select Category</option>
<?php
$sth = $conn->prepare('Select name From category');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['name']!="")
echo ' <option id="CategoryName" value="' .$row['name']. '">'.$row['name'].'</option>';
}
?>
</select> <input type="submit" />
</form>
<?php
if(!empty($_POST['category']))
$category=$_POST['category'];
print_r($category);
?>