HTML/PHP Form Select Option with link to MySQL - php

I followed a previous question on here to get a drop down with link to MySQL. It works without the form but with the form, all the room_id's just scatter across one line and don't go in a drop down box. Any ideas on how to fix it? Thank you
//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
echo "<select name='room_id'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";
//Submit button
echo "<input type='submit' value='Submit'>";
//Closes form
echo "</form>";

The reason is that you are echoing your select twice. Remove one.
//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";
//Submit button
echo "<input type='submit' value='Submit'>";
//Closes form
echo "</form>";

Related

PHP MySQL keep selected option from dropdown menu when menu is select from database

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'];
}

Selectbox for update value

I have a selectbox in PHP, where the values and one value is selected. This selected value is data from table in MYSQL. Now, I need update/change this value to another which is in selectbox and send new value in table. When I send this new value from selectbox nothing happens. Can someone write how to update values ​​with selectbox.
<?php echo "<form action=zap_azuriraj.php?y=".$_GET['y']."&b=".$_GET['b']." method=POST>";
echo $_GET['y'];
$upit = "SELECT * FROM zapis WHERE zapis_id='$_GET[y]'";
$rezultat = izvrsiUpit($upit);
$rows = mysql_fetch_array($rezultat);
$id_bilj=$rows['biljka_id'];
$upit2 = "SELECT * FROM biljka";
$rezultat1 = izvrsiUpit($upit2);
if(isset($_GET['b'])) {
echo"<select name='biljkaa_id'>"; }
while ($row = mysql_fetch_array($rezultat1)){
if ($row['biljka_id']==$id_bilj) {
$optional="selected=\"selected\"";
}
else {
$optional="";
}
echo "<option $optional value=".$row['biljka_id'].">".$row['naziv']." </option>";
}
echo"</select>";
others are all working
echo"<h3> Datum:</h3>";
echo"<input type=date name=date value=" . $rows[3] . ">";
echo "<h3> Vrijeme:</h3>";
echo"<input type=time name=vrijeme value=" . $rows[4] . ">";
echo"<h3>Opis:</h3>";
echo"<textarea name=opis >" . $rows[5] . "</textarea>";
echo"<h3> Broj parcele:</h3>";
echo"<input type=text name=br_parcele value=" . $rows[6] . ">";
echo"<h3> Broj biljke:</h3>";
echo"<input type=text name=br_biljke value=" . $rows[7] . ">";
?>
<input type="submit" value="update" name="update">
</form>
<?php
I think the problem is in the commented line
if(isset($_POST['update'])&&(isset($_POST['biljkaa_id']))){
/* $biljka_id = $row['biljka_id'];*/
$datum=$_POST['date'];
$vrijeme = $_POST['vrijeme'];
$opis = $_POST['opis'];
$br_parcele = $_POST['br_parcele'];
$br_biljke=$_POST['br_biljke'];
$id=$_GET['y'];
$upit="UPDATE zapis SET
biljka_id='$biljka_id',
datum='$datum',
vrijeme='$vrijeme',
opis='$opis',
broj_parcele = '$br_parcele',
broj_biljke = '$br_biljke'
WHERE zapis_id='$id'";
$rezultat = izvrsiUpit($upit);
echo "Ažuriran je zapis";
}
?>
I found a solution , thanks anyway
$biljka_id = $_POST['biljkaa_id'];

Modify a selection list in mysql using PHP to radio buttons

This is my code for the selection list, i need to modify the code so instead of a drop-down select list, its a bunch of radio buttons (whithout hard coding).
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT studentID FROM student ";
$result = mysql_query($sql);
echo "<select name='studentID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['studentID'] . "'>" . $row['studentName'] . "</option>";
}
echo "</select>";
?>
Any help would be appreciated.
Try following
while ($row = mysql_fetch_array($result)) {
echo '<input type="radio" name="studentID" value="'.$row['studentID'].'"> '.$row['studentName'].'<br>';
}

Post 'SELECT' Value To Next Page PHP

Here is the form code:
<?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 "<form class='form-vertical login-form' action='step-2.php' method='POST'>";
echo "<h4 class='form-title'>Step One: Choose Your Project</h4><div class='control-group'><div class='controls'>";
echo "<select>";
echo "<option value=''>Choose Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option name='ID' value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
echo "</div></div>";
echo "<div class='form-actions'><button type='submit' name='submit' class='btn green pull-right'>Proceed to Step Two <i class='m-icon-swapright m-icon-white'></i></button></div></form>";
mysqli_close($con);
?>
What do I need to put on page 2 that retrieves the value ID from the form on the previous page and how do I print it so I can check it is the correct ID?
Simple I know but my brain has packed up and gone home.
You need to change your select to
echo "<select name=\"project\">";
On your second page you can get the value with
echo $_POST['project'];
You need to move the name from option to your select. Then echo $_POST['id'];

How to use a select option value as a PHP variable in the next query?

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

Categories