I have a table with id, header, etc. Would like to make a give a name from header column to dropdown list to each value. Now its only shows value, which is very uncomfortable :
<form method="POST" enctype="multipart/form-data" action ="uploadext.php">
<?php require_once('uploadext.php'); ?>
<div class="col-md-6"><input type="file" name="fileToUploadgp"></div>
<div class="col-md-6"><input type="file" name="fileToUploadpro"><br>
<?php
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$query = "SELECT id, header FROM done_add";
$query1="SELECT header FROM done_add";
$data = mysqli_query($dbc, $query);
$data1=mysqli_query($dbc, $query1);
$array=[];
while ($row = mysqli_fetch_array($data)) {
$arrayid[] = $row['id'];
$arrayhead[]=$row['header'];
}
?>
<select name="selectlink">
<?php foreach ($arrayid as $arr) {?>
<option value = "<?php print($arr)?>"
} ?><?php print($arr) ?></option>
<?php } ?>
</select>
<input type="submit" value="Отправить файлы" name="submita">
</div>
</form>
(sorry for bad english)
change the way you store your data in $array:
while ($row = mysqli_fetch_array($data)) {
$option = [];
$option['id'] = $row['id'];
$option['header'] = $row['header']
$array[] = $option;
}
every $option in $array will have and id and a header, then you can simply:
<select name="selectlink">
<?php foreach ($array as $option) {?>
<option value = "<?php echo $option['id'];?>"
} ?><?php echo $option['header'];?></option>
<?php } ?>
</select>
Related
How to select a supplement and display its associated data such as its description and cost.
Any help will be appreciated.
PHP:
require('database.php');
$suppID = filter_input(INPUT_POST, 'suppID');
if($suppID==null || $suppID==false){
$suppID = 'Supplement-1';
}
$query = 'select * from tblsupplements where Supplement_id= :SupplementID';
$statement = $db->prepare($query);
$statement = bindValue(':SupplementID', $suppID);
$statement->execute();
$supplements = $statement->fetchAll();
$statement->closeCursor();
HTML:
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement): ?>
<option value="<?php echo $supplement['Supplement_id']; ?>">
<?php echo $supplement['Supplement_id']; ?>
</option>
<?php endforeach; ?>
</select>
Try this, hopefully helps.
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement) {
echo "<option value='".$supplement['Supplement_id']."'>".$supplement['Supplement_id']."</option>";
}
?>
</select>
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>
When I submit the form I don't get the value of the select option I tried using POST and session but it always show nothing
main.php
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";
Name of your input type select is data and you are accessing it with data_id so you have to use $_POST['data'] instead of $_POST['data_id']
get value of select box using name "data" as you have set name="data" in <select> box in html:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
test.php
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
try this one..
Check below points its may be creating issue.
Check first your <select class="form-control" name="data"> name is data so you can access it using $_POST['data'] not data_id.
Check for <option value="'.$row['data_id'].'"> may be data_id not giving correct value try to check with static value.
I modified this code for you,please use this code.Its work for me,i hope this code will work also for you.
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>
Hi I need some help to create an form from database:Database
And I have this:
global $wpdb;
//$query ="SELECT modelo,ID FROM volumes";
$query ="SELECT Modelo,ID FROM volumes order by ID";
$wpdb->query($query)
And this:
<div class="form_fields">
<p>
<label for="modelo">Model:
echo "<select name=Modelo value=''>Modelo</option>"; // list box select command
foreach ($wpdb->query($query) as $row){//Array or records stored in $row
echo "<option value=$row[ID]>$row[Modelo]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
</label>
</p></div>
What is wrong?
Please check this :
<?php
global $wpdb;
$query ="SELECT Modelo,ID FROM volumes order by ID";
$wpdb->query($query);
?>
<div class="form_fields">
<label for="modelo">Model: </label>
<select name=Modelo value=''>
<option>Modelo</option>
<?php
foreach ($wpdb->query($query) as $row) {
?>
<option value="<?php echo $row->ID; ?>"><?php echo $row->Modelo; ?></option>
<?php
}
?>
</select>
</div>
Try this :
<?php
global $wpdb;
//$query ="SELECT modelo,ID FROM volumes";
$query = "SELECT Modelo,ID FROM volumes order by ID";
$results = $wpdb->get_results($query);
?>
<div class="form_fields">
<p>
<label for="modelo">Model:
<select name=Modelo>
<option value=''>Modelo</option>
<?php
foreach ($results as $row) {//Array or records stored in $row
echo "<option value='{$row->ID}'>{$row->Modelo}</option>";
/* Option values are added by looping through the array */
}
?>
</select>
</label>
</p>
</div>
Please try this
Model:
Modelo";
foreach ($wpdb->query($query) as $row) {
echo "" . $row['Modelo'] . "";
}
echo "";
?>
I have a droplist <select> list in html. How do I identify the selected value in the $_POST array after the user submits the form ?
<form action="subj_exec.php">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value=';
echo $subject_id;
echo '> ';
echo $name;
echo '</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
</form>
The subject_id is blank in another php file
echo $_POST['subject_id'] is blank.
Please help to identify the issue in the code.
Thanks,
The standard method for forms is GET, so you need to add method="POST" to your form.
get: Default. Appends the form-data to the URL in name/value pairs: URL?name=value&name=value
post: Sends the form-data as an HTTP post transaction
<form action="subj_exec.php" method="POST"> //<<<< added method
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
?>
<option value="<?= $subject_id; ?>"><?= $name; ?></option>
<?php
}
?>
</select>
<input type="submit" value="submit" name="submit" />
In your file subj_exec.php, you can output the selected value with
echo $_POST['subj_id'];
Here you go :
index.php
<form action="subj_exec.php" method="POST">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<select name="subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value="'.$subject_id.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
subj_exec.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit'])) {
if(strlen($_POST['subj_id']) >= 1) {
$option = htmlentities($_POST['subj_id'], ENT_QUOTES, "UTF-8");
// Do Something here with $option
echo $option;
}else {
echo 'nothing selected.';
}
}
?>