Though I have researched, I couldn't find any solution for this. I need to get the database value ("Default") as the pre-selected value of the drop down list.
<select name="listCustomer" id="listCustomer">
<?php
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";}
?>
</select>
Can you please help me on this?
Just create a variable before the echo, something like:
$selected = ((strtolower($row['customer_name']) == 'default') ? 'selected' : '');
then change the echo to this:
echo '<option '.$selected.' value="'.$row['customer_name'].'">'.$row['customer_name'].'</option>';
This can be accomplished using an if statement on the customer_name
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
if($row["customer_name"] === "Default"){
echo "<option value=\"" . $row['customer_name'] . "\" selected>" . $row['customer_name'] . "</option>";
} else {
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";
}
}
?>
Note the selected tag on the first echo.
$query="SELECT customer_name FROM customers";
$result = #mysql_query ($query);
echo "<select name=customer_name value=' '>";
while($drop=#mysql_fetch_array($result)){
echo "<option value=$drop[customer_name]>$drop[customer_name]</option>";
}
echo "</select>";
Related
I have a problem with my page. I was trying to solve it by a lot of tutorials but i don´t know how to make it work. Simply put i have a database of objects. When i select object, page will redirect to another where are shown all informations about the object. But i need to keep the selected option in drop down menu. There is 110 objects so if i select object number 25, informations will show but the drop down menu wont stay on number 25. Can somebody help me with it?
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Vyber">
</form>
<?php
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
?>
First page php
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
Vyberte si zvolený objekt z menu a stlačte tlačidlo výber<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
Check whether the post value matches the object id in the loop.
$selcted = $_POST['Objekt'] == $row['Objekt'] ? ' selected' : '';
If it does $selected is set to " selected" and added to the option.
echo "<option value='" . $row['Objekt'] . "'" . $selected . ">" . $row['Objekt'] . "</option>";
You can send that value $_POST['Objket'] via a variable let us say $selected_value and in the option tag you can write
<option value='" .$row['Objekt']. "' '.if($row['Objket']==$selected_value) echo selected.'>" .$row['Objekt']. "</option>
When you select a option and click on button then it will work otherwise no.
if(isSet($_POST['Objekt']))
{
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
}
This is my code but I don't seem to get anything in the dropdown list. Is there something else I'm supposed to do besides this? Or is there something wrong with my code?
<div class="span10 offset1">
<div class="row">
<h3> Add catagory</h3>
</div>
<select class="selectpicker" data-style="btn-success" >';
<?php
include('database.php');
$query = "SELECT cat_name FROM catagory";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
echo "</select>";
?>
</div>
You're referencing $row but assigning the result to $r. Just change the variable:
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $r['cat_name'] . " '>" . $r['cat_name'] . " </option>";
}
Your variables names look wrong
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
You loop through the results using $r but use $row[] within the loop. It should probably read
while($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
I want to make multiple dropdowns from data out of my mysql database. I want 4 dropdowns to be exact. This is what I have at this moment:
<?php
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie, waar, metwie, voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
?>
For instance, this:
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
$sql = "SELECT waar FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
}
echo "</select>";
$sql = "SELECT metwie FROM data";
$result = mysql_query($sql);
echo "<select name='test2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
}
echo "</select>";
$sql = "SELECT voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
}
echo "</select>";
?>
As stated by HawasKaPujaari, avoid using mysql. Use mysqli. You could use a conditional switch statement like this:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT wie, waar, metwie, voeruig FROM data")) {
printf("Select returned %d rows.\n", $result->num_rows);
while ($row = mysql_fetch_array($result)) {
switch ($row) {
case "wie":
echo "<select name='wie'>";
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
echo "</select>";
break;
case "waar":
echo "<select name='waar'>";
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
echo "</select>";
break;
case "metwie":
echo "<select name='metwie'>";
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
echo "</select>";
break;
}
case "voeruig":
echo "<select name='voeruig'>";
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
echo "</select>";
break;
}
/* free result set */
$result->close();
}
?>
php and mysql are different softwares. what you are doing here is connecting php with mysql using mysql_*() functions
in your case what you get is an array in php. you can use this array for whatever you want. if you want to print array as is as for debugging use:
echo "<pre>";
print_r($row);
echo "</pre>";
From this you will get array structure
then you ca use different dropdowns for array elements
Note: mysql_*() is not safe. Use mysqli_* or PDO.
Try code below. hope it help you
<?php
$wie=array();
$waar =array();
$metwie =array();
$voeruig =array();
while ($row = mysql_fetch_array($result)){
$wie[]=$row["wie"];
$waar[]=$row["waar"];
$metwie[]=$row["metwie"];
$voeruig[]=$row["voeruig"];
}
?>
<select name="wie">
<?php
foreach($wie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?>
<php
}
?>
</option>
<select name="waar">
<?php
foreach($waar as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
<select name="metwie">
<?php
foreach($metwie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?></option>
<php
}
?>
</select>
<select name="voeruig">
<?php
foreach($voeruig as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>
I have the following code which provides a drop down list of all the rows in that specific table, this works fine. The code is below:
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo "<select>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I now want a second drop down list that is determined by what ever is selected in the one above based on the ID. So, I want something like:
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID=ID_FROM_QUERY_ABOVE");
echo "<select>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
I basically want to get the ID from the first drop down to provide the results in the second dropdown. Can this be done?
You can't "only" do this with Ajax, but you should do it with Ajax.
PHP way (not suggested, and untested). Basically use isset and if it is, more will be added to the form. The POST from the select, is the select name. So change the plain select tag which I did in the example below. This also requires them to submit it.
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo '<form id="project_form" method="post">';
echo "<select id='select_your_project' name = 'select_your_project'>";
echo "<option value=''>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2))
{
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
echo '<input type="submit" value="Submit">';
echo '</form>';
if(isset($_POST['select_your_album'])){
//do form submitted stuff here
}
Ajax way (two separate files, untested but gives you the idea)
//Main page (view) START
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//this will trigger automatically when they change the first select box
$('#select_your_project').on('change', function(event){
if($(this).val() == 'select_your_project'){
$("#ajax_reply_div").empty()
}else{
var values = $(this).serialize();
$.ajax({
url: "php_data_file.php",
type: "post",
data: values,
success: function(data){
$("#ajax_reply_div").empty().append(data);
},
error:function(){
$("#ajax_reply_div").empty().append('something went wrong');
}
});
}
});
</script>
<form id="id_of_form">
<?php
echo "<select id='select_your_project' name='select_your_project'>";
echo "<option value='select_your_project'>Select Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
?>
</select>
<div id="ajax_reply_div">
</div>
<input type="submit" value="Submit">
</form>
//Main page (view) END
//php_data_file.php START
if(isset($_POST['select_your_project'])){
$result2 = mysqli_query($con,"SELECT ID, ALBUM_NAME FROM a_different_table WHERE ID='".$_POST['select_your_project']."'");
//as a note it is better to only send an array back then build the HTML with jQuery, but this way is easier if you are new to jQuery/Ajax
echo "<select id='select_your_album' name = 'select_your_album'>";
echo "<option value=''>Select Your Album</option>";
while($row = mysqli_fetch_array($result2)){
echo "<option value='" . $row['ID'] . "'>" . $row['ALBUM_NAME'] . "</option>";
}
echo "</select>";
}
//php_data_file.php END