I trying make a combobox with php and mysql but i cant find a solution. How I can fix this code?
$conn = new mysqli($servername, $username, $password, $db);
$result = mysqli_query($conn, "SELECT * FROM exemple") or die (mysqli_error($conn));
?>
<form action="" method="post">
<h1>Combo</h1>
<select name="cbx" id="cbx">
<?php while($ri = mysql_fetch_array($result)) { ?>
<option value="<?php echo $ri['exemp'] ?>">
<?php echo $ri['exemp2'] ?>
</option>
</select>
<?php } ?>
</form>
Simply, close while loop before </select> tag:
<form action="" method="post">
<h1>Combo</h1>
<select name="cbx" id="cbx">
<?php while ($ri = mysql_fetch_array($result)) { ?>
<option value="<?php echo $ri['exemp'] ?>"><?php echo $ri['exemp2'] ?></option>
<?php } ?>
</select>
</form>
Related
I have create a select option form but when edit i can get current value from database.
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($nm_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?php echo $selected;?>>
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
You should not use value for select tag. Check the condition in inside the option tag and make it is selected like following
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){?>
<option value="<?php echo $value->kd_proyek; ?>" <?php if($nm_proyek==$value->kd_proyek) { ?> selected="selected" <?php }?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
<?php
$conn = new mysqli('SERVER NAME', 'DB USER NAEM', 'DB USER PASSWORD', 'DB NAME');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//set the value of which option you want to selected
$nm_proyek = '';
?>
<!-- for combobox you should use 'multiple' -->
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." multiple />
<option value=""></option>
<?php
$query = $conn->query("SELECT * FROM `t_proyek`");
while($value = $query->fetch_object()) {?>
<option value="<?php echo $value->kd_proyek; ?>" <?php $nm_proyek == $value->kd_proyek ? "selected='selected'" : "" ?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.
Thanks a lot.
Current Code:
<?php
include 'database/connectdatabase.php';
if(isset($_POST['select_db']))
{
$select_db = $_POST['select_db'];
$selectdb_query = 'SHOW TABLES FROM $select_db';
$query_select = mysql_query($selectdb_query,$connectDatabase);
if(!$query_select)
{
echo 'NO table selected!';
}
?>
<form method="POST" action="selecttable.php" autocomplete="off">
<select name="select_db">
<option selected="selected">Select Database</option>
<option>section_masterfile</option>
</select>
</form>
<form method="POST" action="#" autocomplete="off">
<?php while ($row = mysql_fetch_row($query_select)) {
$num_row = mysql_num_rows($row);?>
<select name="select_table">
<option selected="selected">Select Table</option>
<?php for($i=0;$i>=$num_row;i++){?>
<option><?php echo $row[0];?></option>
<?php}?>
</select>
<?php}?>
</form>
The problem is that you're already fetching rows yet you haven't even submitted the form yet.
I suggest restructure you logic this way:
$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
$select_db = $con->real_escape_string($_POST['select_db']); // escape the string
$query = $con->query("SHOW TABLES FROM $select_db");
while($row = $query->fetch_assoc()) {
$tables[] = $row['Tables_in_' . $select_db]; // use associative instead
}
}
?>
<form method="POST" autocomplete="off">
<select name="select_db" onchange="this.form.submit();">
<option disabled selected>Select Database</option>
<option>test</option>
</select>
<br/><br/>
<select name="select_table">
<?php foreach($tables as $table): ?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php endforeach; ?>
</select>
</form>
Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.
error_reporting(E_ALL);
ini_set('display_errors', '1');
<select name="select_table">
<option selected="selected">Select Table</option>
<?php while ($row = mysql_fetch_row($query_select)) { ?>
<option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php } ?>
</select>
Try This
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select))
{
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Consider this code:
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select)) {
?>
<option><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
I'm developing a webpage with a select list that contains images.
I already have this:
When I select an image name in the list the image will be displayed in the div below.
<?php
// Create connection
$con=mysqli_connect("******","***","***","charts");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form method="post" action="index.php" id="nano" name="nano">
<p>
<select name="SelectBox" id="SelectBox" onchange="this.form.submit()">
<?php if($_POST['submitted'] == true){ ?>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano WHERE IMAGE_NAME ='". $_POST['SelectBox']."'");
while($row = mysqli_fetch_array($result))
{ ?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php } ?>
<?php echo $_POST['SelectBox']; ?></option>
<?php } else{ ?>
<?php
$result = mysqli_query($con,"SELECT TOP * FROM Nano");
while($row = mysqli_fetch_array($result))
{
?>
<option selected="selected" value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php
$var1 = $row['IMAGE_NAME']; ?>
<?php
}
?>
<?php } ?>
<option value="" disabled="disabled"> -------- </option>
<?php
$result = mysqli_query($con,"SELECT * FROM Nano");
while($row = mysqli_fetch_array($result))
{ $values[] = $row['IMAGE_NAME'];
?>
<option value="<?php echo $row['IMAGE_NAME'] ?>">
<?php echo $row['IMAGE_PARAMETER'] ?>
</option>
<?php }?>
</select>
<input type="hidden" name="submitted" id="submitted" value="true" />
</p>
<?php if($_POST['submitted'] == true){ ?>
<p><img src="Images\Nano\<?php echo $_POST['SelectBox']?>" width="953" height="600" /></p>
<?php }else { ?>
<p><img src="Images\Nano\<?php print_r($values[0]) ?>" width="953" height="600" /></p>
<?php } mysqli_close($con);?>
</form>
</div>
I want when I move down in the select list the picture will change and not when I click on it in the select list.
In your case, you have to bind hover event to option, but there is no way to do what you want using native select control. The native one only answers when you click a different option from previous.
However, you can simulate a select control using html&css&js, that way when your cursor move down the simulated option(which might be a div or something), you can bind event handlers to it and display the name.
I have my textboxe showing the data that is in the correct field from the database, but not when it is a drop down (select)
I have tried this code
<?php
$rs_settings = mysql_query("SELECT * from thesis WHERE user_id = $user_id;");
?>
<form action="academic.php" method="post" name="regForm" id="regForm" >
<?php
$num_rows = mysql_num_rows($rs_settings);
if($num_rows > 0) { ?>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) {?>
1a.question <span
class="required">*</span></td>
<select name="Applied_Elsewhere" id="Applied_Elsewhere" >
<option <? if ($row_settings[Applied_Elsewhere]=='Y') { ?> selected <? } ?>
value="Y">Yes </option>
option <? if ($row_settings[Applied_Elsewhere]=='N') { ?> selected <? } ?>
value="N">No </option>
</select>
Try changing <? to <?php and $row_settings[Applied_Elsewhere] to $row_settings['Applied_Elsewhere']
<select name="Applied_Elsewhere" id="Applied_Elsewhere" >
<option <?php if($row_settings['Applied_Elsewhere']=='Y') { echo "selected='selected'"; } ?> value="Y">Yes </option>
<option <?php if($row_settings['Applied_Elsewhere']=='N') { echo "selected='selected'"; } ?> value="N">No </option>
</select>
Please try this code:
<?php
$rs_settings = mysql_query("SELECT * from thesis WHERE user_id = $user_id;");
?>
<form action="academic.php" method="post" name="regForm" id="regForm" >
<?php
$num_rows = mysql_num_rows($rs_settings);
if($num_rows > 0) {
while ($row_settings = mysql_fetch_array($rs_settings)) {
1a.question <span
class="required">*</span></td>
print'<select name="Applied_Elsewhere" id="Applied_Elsewhere" >'
if ($row_settings['Applied_Elsewhere']=='Y')
print '<option value="Y" selected>Yes </option>';
else
if ($row_settings['Applied_Elsewhere']=='N')
print '<option value="N" selected>No</option>';
?>
I have the following code:
<form name="votos" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $categorias=mysql_query("SELECT * FROM categoria") or die (mysql_error()); ?>
<?php while($cat=mysql_fetch_array($categorias)){
echo "<h5>".$cat[0]." - ".$cat[1]."</h5>";
$nomeados=mysql_query("SELECT * FROM nomeados WHERE cod_categoria='$cat[0]'") or die(mysql_error());
?>
<div class="styled-select">
<select name="voto">
<option value=""></option>
<?php
while($nom=mysql_fetch_array($nomeados)){
$nomes=mysql_fetch_array(mysql_query("SELECT nome FROM logins WHERE cod_login='$nom[0]'")) or die (mysql_error());
?>
<option value="<?php echo $nom[0]; ?>"><?php echo $nomes[0]; ?></option>
<?php
}
?></select></div>
<?php }
?>
<input type="submit" class="botao" value="" name="submit" />
</form>
The code shows all the categories with the nomenies in a select box.
The thing is that the select box goes to post with the same name. I don't know how to get the information from all of the select boxes.
If you have more elements with the same name you can post them as an array
<select name="voto[]">
and than you get it in php
$votos = $_POST['voto']
foreach($votos as $voto){
//do what you need to do
}
and it's an array with all the values