Link two dynamic drop down menus php mysql - php

I have two drop down menus that are populated from the same table. What I would like to do is make them automatically match selected values with each other. In other words, if client 2a is chosen I want account 2a to be automatically selected in the other drop down menu and vice versa. It seems like it should be easy to do without jquery because they have matching ids, but I can't seem to make it happen.
Here is the code:
<p>Client's full name: <select name="client"><option value="<? echo $c_id ?>" ><? echo $client ?></option>
<?php //retrieve all the clients and add to the pull-down menu
$q = "SELECT c_id, CONCAT_WS(' ', c_firstName, c_middleName, c_lastName)FROM client ORDER BY c_lastName, c_firstName ASC";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
//Check for stickyness
if (isset($_POST['client'])&&($_POST['client']== $row[0]))
echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
}
?>
</select></p>
<p>Account nickname: <select name="nickname"><option value="<? echo $c_id ?>" ><? echo $nickname ?></option>
<?php //retrieve all the Acct Nicknames and add to the pull-down menu
$q = "SELECT c_id, c_nn FROM client ORDER BY c_nn ASC";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
//Check for stickyness
if (isset($_POST['nickname'])&&($_POST['nickname']== $row[0]))
echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
}
?>
</select></p>
</div>

Related

PHP dropdown search result selecting wrong input

I have made a dropdown search form that is auto populated by my database content. The voices in the table would be for example types of woods with varing dimensions.
So there are repeatable wood names with diverse data.
To avoid repetition the dropdown is populated with wood types combined to be selected then displayed with all their variants.
The problem is, upon selecting an input, the results are of the item listed above and not the one selected.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
echo "<option value=\"\">Selezione Materiale</option>";
while($row = $info->fetch_assoc()){
if($row['finit'] != $finit) {
echo "<option value=\"$finit\">" . $row['finit'] . "</option>";
$finit = $row['finit'];
}
}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Since there are many variants(dimensions) associated with a single wood type, you have to first take the wood type as input from user(via dropdown list), and probably then you may want to display all possible variants(dimensions) of that particular wood type.
So, change the SQL query in the following way,
$query = "SELECT DISTINCT finit FROM prime";
and the while loop in the following way,
while($row = $info->fetch_assoc()){
$output = "<option value='" . $row['finit'] . "'";
if($row['finit'] == $_POST['finit']){
$output .= " selected='selected'";
}
$output .= ">" . $row['finit'] . "</option>";
echo $output;
}
Try this but change if condition according your default value and sql value should be match.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
?>
<option value="">Selezione Materiale</option>;
<?php
while($row = $info->fetch_assoc()){
if($row['finit'] == $finit) {
$selected = 'selected';
}else{
$selected = '';
$finit = $row['finit'];
}
?>
<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>
<?php } ?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

edit select option values

I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";

How to get option list from database and select it?

I want a dynamic option list which reads from database table lets say table of : Students,
It must show to user the list of student_name and when it is selected by user it must send student_id of that student to the database. For example:
Students table :
student_id student_name
----------------------------
1 John
2 Edward
In users option list must be included only John, Edward.. but when user selects John, the option picker must send only student_id('1') to database.
My current code , but is not fetching list from db :S :
yes, this is my code but for some reasons it ain't work :
<select Name='student_id'>
<option value="">--- Select ---</option>
<?
mysql_connect ("localhost","root","");
mysql_select_db ("mydb");
$select="student_name";
if (isset ($select)&&$select!=""){
$select=$_POST ['student_name'];
}
?>
<?
$list=mysql_query("select * from students order by student_name asc");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['student_id']; ?>"<? if($row_list['student_name']==$select){ echo "selected!"; } ?>>
<?echo $row_list['student_name'];?>
</option>
<?
}
?>
</select>
may this one help you
$result = mysqli_query($con,"SELECT * FROM Students");
echo "<select>";
while($row = mysqli_fetch_array($result)) {
echo "<option id=".$row['student_id'].">" . $row['student_name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
You should fetch your database from your PHP, then build your list out of it
//YOU MUST ADD YOUR DATABASE CONNECTION
mysql_connect('localhost','username','password');
mysql_select_db("dbname");
//HERE IS YOUR SQL REQUEST
$SQL_request = "SELECT * FROM `student_table`";
$req = mysql_query($SQL_request) or die(mysql_error().'<br/>'.$SQL_request);
echo '<select name = "students">';
while($result = mysql_fetch_assoc($req)){
echo '<option value="'.$result['student_id'].'">'.$result['student_name'].'</option>';
}
echo '</select>';
?>
Try below code.
$con = mysqli_connect('localhost','root','','mydb');
$sql="SELECT student_id ,student_name FROM students";
$result = mysqli_query($con,$sql);
?>
<select name = "student">
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['student_id']; ?>"><?php echo $row['student_name']; ?></option>
<?php
}
?>
</select>

Displaying a list of checkboxes based on an array

I have a list of checkboxes displayed below. This shows all the contactors and allows them to be selected via check box.
<?php
$query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
<option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
</select></li>
I have an array saved in another place that I would like to generate the list above but with the items in the array below already checked/selected.
<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");
while($row = mysql_fetch_array($result))
{
$contractors = unserialize($row['contractors']);
foreach ($contractors as $contractor)
echo "" . htmlspecialchars ($contractor).' - ';
?>
Any help would be greatly appreciated.
Try this :
<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>

Can't set a default value at drop down menu

$sql = "select custName from customer where active = 1 order by custName";
sult=mysql_query($sql);
echo"<select name='custNameColo'>";
while($row = mysql_fetch_array($result)) {
if($row['custName']==$_GET['defaultCust']) {
echo "<option value=".$row['custName']."selected = 'selected'>".$row['custName']."</option>";
}
else {
echo "<option value=".$row['custName'].">".$row['custName']."</option>";
}
}
echo "</select>";
I want set a default value a drop down menu, but it doesn't work, please help me.
<?php
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<option value="<?php echo $row['custname'] ?>" <?php if($row['custname']==$_GET['defaultCust']) { echo "selected";}?>>
<?php echo $row['custname']; ?></option>
<?php
}
?>
<? echo "TRY this it will work";
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
?>
<select name="custNameColo">
<?
while($row = mysql_fetch_array($result)){?>
<option value="<?=$row['custname'];?>" <? if($row['custname']==$_GET['defaultCust']){?> selected="selected" <? }?>><?=$row['custname'];?></option>
<? } ?>
</select>
You didn't added single quotes in the value of option tag before and after. I done it for you. The default value was not selected because value attribute was not properly closed.
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
echo "<select name='custNameColo'>";
while($row = mysql_fetch_array($result)){
if($row['custName']==$_GET['defaultCust']){
echo "<option value='".$row['custName']."' selected = 'selected'>".$row['custName']."</option>";
}
else {
echo "<option value='".$row['custName']."'>".$row['custName']."</option>";
}
}
echo "</select>";

Categories