I want to retain the value that I selected after form submission.
<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
<?php
if (isset($_POST['submit'])){
$selected = $_POST['studname'];
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" '.(($stud_id ==
$selected)? 'selected="selected"':"" >'.$Name.'</option>';
}
}else{
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
}
}
?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create
Account' />
My problem is it is not retaining after submitting
<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
<?php
if (isset($_POST['submit'])){
$selected = $_POST['student']; // <---- multi-selects come in as Array
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
// if the current ID is inside the POST-ed Array - mark as SELECTED
echo '<option value="'.$stud_id.'" '.(in_array($stud_id, $selected) ? 'selected="selected"': "").'>'.$Name.'</option>';
}
}else{
$qry = "Select studtbl.stud_id,concat(studtbl.fname,' ',
substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
$result = mysqli_query($conn, $qry);
while($row = mysqli_fetch_array($result))
{
extract($row);
echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
}
}
?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create
Account' />
Related
In my database I have 2 tables:
To insert data, I have a form that populates dropdown options from the table formulation. This is what the insert form for formulation dropdown looks like:
<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
$formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>
<select>
<option value="">Select formulation</option>
<?php echo $formulation; ?>
</select>
Now I am working on the ‘Update’ form. But my question is how can I populate the ‘Formulation’ field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? Like this image below:
I am having problem with how I should build the form. How should I proceed with this form?
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$row['name'],
);
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<!--What should be the codes here? -->
</select>
</div>
<button type = "submit">Save changes</button>
</form>
Thanks in advance for your suggestion.
Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. This will not tackle the update part, just the populate part
Since you are editing a certain item, I will assume that you have something to get the item's itemID.
<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];
//now you have the name and the formulation of that certain item
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text" value="<?php echo $itemName; ?>"><br>
<label>Formulation</label>
<select >
<?php
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
<?php echo $row['formulation_name']; ?>
</option>
<?php
}
?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I changed the code to better suit the problem, there may be typos, just comment for clarification
If I have understand Your question... You have to put Your result into a string. For example:
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
$option = '';
while ($row = $query->fetch_assoc()) {
$name=$row['name'],
$option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<?=$option?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I hope to be of help
This should do the trick:
<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();
$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>
<form action="updateItem" method="POST">
<div>
<label>Item Name</label>
<input type="text" value="<?= $item[0]['name']; ?>"><br>
<label>Formulation</label>
<select>
<?php foreach($formulations as $formulation){
echo '<option value="'. $formulation['formulationId'].'">' .
$formulation['formulation_name'] . '</option>';
} ?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
i want to ask why my combobox not indexed?
here is a code
<form method="post" action="../php/proses-list-maintenance.php">
<p style="font-size:15px; font-family:Coda; color:black;">Pilih Aplikasi :</p>
<select name="comboapp" class="form-control">
<?php foreach ($list_app as $app) : ?>
<option value="<?php echo $app['application_id'] ?>"><?php echo $app['application_name'] ?></option>
<?php endforeach ?>
</select>
<button type="submit" class="btn btn-success">Pilih</button>
</form>
and here is action code
include '../koneksi.php';
$id = $_POST['comboapp'];
$query = "select * from application where application_id = '$id'";
$hasil = mysqli_query($db, $query);
$data_app = mysqli_fetch_assoc($hasil);
$query2 = "SELECT * FROM maintenance where maintenance_id = '$id'";
$hasil2 = mysqli_query($db, $query2);
if ($hasil == true && $hasil2 == true) {
echo "<script>location.href='../admin/list-maintenance.php?aaa='+$id</script>";
$data_app = array();
while ($row = mysqli_fetch_assoc($hasil)) {
$data_app[] = $row;
}
$data_man = array();
while ($row = mysqli_fetch_assoc($hasil2)) {
$data_man[] = $row;
}
}
and when I run, I got this notice
undefined index: comboapp
when the combo box is selected and the button is clicked. I want the data id's in the combo box enter the data into $id
My problem here is that my checkbox will only be disabled when all the checkboxs status is 0. But here, i only want the selected checkbox be disabled, but when the status of checkboxs is not all set to 0, it will not be disabled.
Here is my code:
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box WHERE status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows) {
$disable = 'disabled="disabled"';
}
?>
<form method = "post" action = "">
<input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/>
<label for="1.1" class="background1"></label> <br/>
<input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/>
<label for="1.2" class="background2"></label>
<br/>
<input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/>
<label for="1.3" class="background2"></label>
<input type="submit" name="Next" id="Next" value="next" />
</form>
<?php
if(isset($_POST['Next'])) {
foreach($_POST['boxs'] as $f) {
$sql = "UPDATE box SET status = '0' WHERE boxid = '$f'";
mysqli_query($con,$sql) or die(mysqli_error($con));
$result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')";
mysqli_query($con,$result) or die(mysqli_error($con));
}
}
?>
So, what is wrong with my code?
<?php $username = $_SESSION['username'];?>
<form method = "post" action = "">
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box WHERE boxid=1.1 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/>
<label for="1.1" class="background1"></label> <br/>
<?php
$query = "SELECT * FROM box WHERE boxid=1.2 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/>
<label for="1.2" class="background2"></label>
<br/>
<?php
$query = "SELECT * FROM box WHERE boxid=1.3 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/>
<label for="1.3" class="background2"></label>
<input type="submit" name="Next" id="Next" value="next" />
</form>
<?php
if(isset($_POST['Next']))
{
foreach($_POST['boxs'] as $f){
$sql = "UPDATE box SET status = '0' WHERE boxid = '$f'";
mysqli_query($con,$sql) or die(mysqli_error($con));
$result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')";
mysqli_query($con,$result) or die(mysqli_error($con));
}
}
?>
I don't know the your exact requirement but; Your solutions may be
<?php $username = $_SESSION['username']; ?>
<form method = "post" action = "">
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box";
$result = #mysqli_query($con, $query);
$i=1;
while ($raw = $result->fetch_assoc()) {
if ($raw['status'] == 1) {
$disable = 'disabled="disabled"';
} else {
$disable = '';
}
echo "<input type='checkbox' name='boxs[]' id='" . $raw['boxid'] . "' value ='" . $raw['boxid'] . "' $disable/>";
echo " <label for='" . $raw['boxid'] . "' class='background$i'></label> <br/>";
$i++;
}
?>
</form>
I have this code and I'm trying to put the selected state in a subcat table.
So far it returns an empty value. I'm not sure if this is clear or not, but all I want is: select a state from the select option and submit it. I want to get the selected state name into my table subcat.
enter <?php
include("connect.php");
$state = $row['states']; //Select name
if (isset($_POST[submit])){
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Thanks
Change $state = $row['states'] to $state = $_POST['states']
<?php
include("connect.php");
if (isset($_POST[submit]))
{
$state = $_POST['states']; //Select name
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>"; // if you want to
//get the name into table, then use like this
//echo "<option value='$row[name]'>$row[name]</option>"; or
//echo "<option>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form>
Try this:
enter <?php
include("connect.php");
if (isset($_POST[submit])){
$state = $_POST['states'];
$query = "INSERT INTO subcat (sub_name) VALUES ('".mysql_real_escape_string($state)."')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo '<select name="states" id="states">
<option value="">Select a state</option>';
while ($row = mysql_fetch_assoc($sql)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Dont forget to use mysql_real_escape_string to prevent SQL injections. I have replaced $state = $row['states']; with $state = $_POST['states'];
I dont know where u got $row from...
The above will insert the states name into the database.
How do I keep the selected item after I submit the page? I have the country dropdown list with the following code.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\"";
if($_POST['country'] == $row['country_id'])
echo 'selected';
echo ">".$row["country_name"]."</option>";
}
?>
<?php
while($row = mysql_fetch_array($Result))
{
if ($_POST['country'] == $row["country_id"]) {
echo "<option value=\"".$row["country_id"]."\" selected="selected">".$row["country_name"]."</option>";
} else {
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST['country'] == $row["country_id"]) echo 'selected="selected" ';
echo "value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
This code depends if you are trying to keep the value after you submit back to the original page.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px">
<option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST["yourSelectName"] ==$row["country_id"])
echo ' selected = "selected" ';
echo " value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
</select>
Or you can do it all inline... less code, doesn't require all the if/then/else stuff
Change
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
To
echo "<option ". (($_POST['country'] == $row["country_id"]) ? 'selected ' : '') ."value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
If it is get (passed in URL), the use GET
<?php
$selectedid = 5; //example of selected if before submitting
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]." ".(($selected==$row["country_id"])?"SELECTED":"")."\">".$row["country_name"]."</option>";
}
?>
//assume you have $result = array(your result list);
<select name='question'>
<?php
foreach ($result as $question) {
if ($_POST['question'] == $question) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='" . $question . "' $selected>$question</option>";
}
?>
</select>