hope you fine and well,
i have a drop down list that contains categories list as follows:
<div class='form-group'>
<br/>
<label class='control-label col-md-2 'for='id_date'>Category</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
</div>
</div>
below this select, i have another select which is to choose element from the category as follows :
<div class='form-group'>
<br/>
label class='control-label col-md-2 ' for='id_date'>element</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control" id="sel12" ng-model="elemnt" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT element FROM elements where category = ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['element'] . "'>" . $row['element'] . "</option>";
} ?>
</select>
</div>
</div>
how i can make the content of the second drop list to be based on the first drop list ?! e.g how i can put the input of the first drop list in the second SQL statement ?!
regards.
You can't do that with only php.
You must use javascript/Jquery and ajax.
Make a php script who load data from a request.
After change your first select use ajax function who call your php script with the right value and update the second select.
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
Jquery
$("#sel1").change(function(){
$.ajax({
method: "POST",
url: "yourscript.php",
data: {myval : $(this).val()};
})
.done(function( msg ) {
//Here append your result in your second select
});
});
PHP
<?php
if(isset($_POST['myval']))
{
//SQL query where id=myval
echo $result;//result of query
}
Using pure PHP with intermediate submit:
<?php
if(isset($_POST['submitForm']) && $_POST['submitForm'] == 1){
//form is submitted by button, proceed with DB stuffs
echo 'Great, you have submitted the form, will check VALUES and do INSERT.';
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="aForm">
<input type="hidden" name="submitForm" id="submitForm">
<select name="category" onchange="this.form.submit();">
<option value="">Choose...</option>
<option value="1" <?=($_POST['category']==1 && !$_POST['submitForm'])?'selected':'';?>>Cat 1</option>
<option value="2" <?=($_POST['category']==2 && !$_POST['submitForm'])?'selected':'';?>>Cat 2</option>
<option value="3" <?=($_POST['category']==3 && !$_POST['submitForm'])?'selected':'';?>>Cat 3</option>
</select>
<select name="element">
<?php
if($_POST['category'] && !$_POST['submitForm']){
// SELECT from DB based on passed category ID
echo '<option value="">Now choose element...</option>';
echo '<option value="1">Elem 1</option>';
echo '<option value="2">Elem 2</option>';
echo '<option value="3">Elem 3</option>';
}else{
echo '<option value="">Choose category first...</option>';
}
?>
</select>
<input type="button" name="btnSubmit" value="Submit" onclick="document.getElementById('submitForm').value = 1; this.form.submit();">
</form>
</body>
However this is not either the best or most efficient approach, the script only demostrates how can be done without JS as is requested.
Related
I search for answers here but haven't found a solution.
I have also added picture of the error.
I want the data to go to the first drop-down list ( above the error)
I think the method I try to perform is also create drop-down list, am I correct?
<form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
<div class="form-group">
<label id="senderName">שם השולח:</label>
</div>
<div class="form-group">
<label for="to_user">מען:</label>
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = \mysqli_query("SELECT name From users");
$row = mysqli_num_rows($sql);
echo "<select name='to_user'>";
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
echo "</select>" ;
?>
</select>
</div>
picture of the error
In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.
$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");
Note: replace $con with your database connection variable!
As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
</div>
<label><b>Select Steam: </b></label>
<select id="study">
<option value="" selected="selected" disabled="">---Selected---</option>
<?php
$query = "SELECT study FROM details";
$query_run = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($query_run)) {
echo "<option value='".$row['study']."'>".$row['study']."</option>";
}
?>
</select>
I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.
I have table query which is displayed thru select option. The column 1 value is displayed on it. When I select the row 1 value from column 1, I also want the value on column 2 automatically displayed, how would I code that?
Example : medicinename(column1) = medicineprice(column2)
The medicinename is in select option. The medicineprice is not. Just want to display automatically the price every time I select the medicinename.
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($displaymedicine = mysql_fetch_array($medicine)) {
$medicineid = $displaymedicine['id'];
$medicinename = $displaymedicine['medicinename'];
$medicineprice = $displaymedicine['medicineprice'];
?>
<option id="<?php echo $displaymedicine['medicineid']; ?>"><?php if($displaymedicine ['medicineid'] == $displaymedicine ['medicinename']) echo 'selected="selected"'; ?><?php echo $displaymedicine ['medicinename'] ?></option>
<?php
}
?>
</select>
Do you mean you want to be returned the price of the medicine to your PHP script when you select the name of the medicine? In which case you need to add a value="" attribute to the <option tag. It is the contents of value="" that is returned to the script i.e. if this option was selected from your <select name="selectmedicine">
<option value="999"...>Penicillin</option>
Then PHP would receive $_POST['selectmedicine'] which would contain 999
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
echo ' value="' . $row['medicineprice'] . '"';
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>
</select>
Of course a better solution would be to put the unique ID of the medicine table into the value="" attribute. Then when the form is posted to your PHP script you read your database for that one row and you have all the data related to that medicine available to the script.
So that would be
<div class="col-md-3"><label><h5>Medicine Name : </h5>
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename" style="width:200px">
<option id="0" style="width:100px"> Select here...</option>
<?php
require_once 'config.php';
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicineid'] . '"';
> changed code line
echo ' value="' . $row['id'] . '"';
> changed code line
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>'
}
?>
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
When I submit this form, I stay on the same page, but I want a the same time to keep the option selected previously option visible.. How can I do that?
I tried:
document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";
But it did not work..
here:
foreach($allemp as $row){
echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
Try this :
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
added a line above echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)
Added '.$selected.' in side options.
Your code becomes : Copy paste below code instead of yours
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
} ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">