Text box show value from Ajax - php

Using PHP, Ajax and Jquery, I'm able to create a cascading dependent drop down menu. But I want to have the third drop down to be a text box instead.
How can I do this?
Ajax Code
PHP code...
<?php
include('conn.php');
$query = $con->query("SELECT * FROM job_category group by category_name ");
$rowCount = $query->num_rows;
?>
Category:
<select name="category" id="category">
<option value="">Select Category</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_num'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<br>
Sub-Category:
<select name="sub_type" id="sub_type">
</select>
<br>
Priority:
<select name="priority" id="priority_level" >
</select>
?>
Please help, I'm having a hard time on this.

Related

Populate a dropdown from mysql and have the chosen item display in the dropdown after page refreshes

I am using php with a dropdown to select data and display the selected item in the dropdown after the page refreshes so have used this code which works.
<select name="select" id="mysort" onchange="select(this.value);">
<option value="5" <?php if($selecting == '5'):?> selected="selected"<?php endif;?>>5 </option>
<option value="6" <?php if($selecting == '6'):?> selected="selected"<?php endif;?>>6 </option>
<option value="7" <?php if($selecting == '7'):?> selected="selected"<?php endif;?>>7 </option>
<option value="8" <?php if($selecting == '8'):?> selected="selected"<?php endif;?>>8 </option>
</select>
I then want to be able to populate the dropdown from a mysql database. This code works:
$sql = "SELECT DISTINCT number FROM files ORDER BY number";
$result = $mysqli->query($sql);
echo '<select name="choose" id="mychoice">';
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value='.$row["number"].'>'.$row,["number"].'</option>';
}
echo '</select>';
}
I am having trouble with the syntax when I put the two blocks of code together so that the values are populated from the mysql and the selected option displays in the dropdown after the page refreshes.
I would be grateful for any help with this.
I think you want this -
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["number"].'"';
if($selecting == $row["number"]) { echo " selected "; }
echo '>'.$row["number"].'</option>';
}

php html select value

I have a select box connect to database for countries, it works but trying to keep the selected data in the form clears itself out. I have tried adding a selected but gives error
<div class="form-group">
<label>Country</label>
<?php
$sql = "SELECT * FROM countries ";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<option value="">Select a country</option>
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
</select>
</div>
Assign the return value of a ternary operator to the variable $selected and add it to the option tag
<?php
$selected = ($_POST['country'] == $row["country_id"])?"selected":"";
?>
<option value="<?=$row["country_id"];?>" <?=$selected ?>>
<?=$row["country_name"];?>
</option>

Dropdown Filter and Display

I've created a dropdown that pulls data from my database:
//Selecting Product Name for Dropdown
$sql = "SELECT DISTINCT TypeOther FROM PedalDirectory";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["TypeOther"]."'>".$row["TypeOther"]."</option>";
}mysql_free_result($rs);
echo "</select>";
but im not sure how to get the dropdown menu to show the information from the database. tried a couple things but seems i can only get all the records to display.
What I would like to do is, when you select an item from the dropdown menu I want that record to appear on the page.
any helps appreciated.
Suppose your dropdown is like
<select id="drop">
<option value="">Select one...</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
</select>
Use jQuery to get selected value
$("#drop").change(function () {
var end = this.value;
alert(end);
});
Thanks all. I figured it out.
<form>
<?php
//Selecting Pedal Type for Dropdown
$sql = "SELECT * FROM PedalDirectory";
$rs = mysql_query($sql) or die(mysql_error());
echo '<select onchange="showUser(this.value)">';
echo '<option value="">Select a pedal type:</option>';
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["PedalID"]."'>".$row["Type"]."</option>";
}mysql_free_result($rs);
echo "</select>";
?>
</form>

php mysql search using boxlist

I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.
you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";

How to populate a combo-box in EDIT FORM

please help me. I include here (see bellow) the Combobox script for INSERTING FORM
I have a Form of Adding a new product. One of option is to select what Category. So, Combobox is for selecting Category from.
<TD>
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="0">Select Category
<?php echo $options;?></option>
</select>
</TD>
Now, I would like to have an EDIT FORM using the same script as for Inserting data in database using combobox.
<?php echo $CATEGORY; ?> this script is for retrieving the data from database.
please help me to find a way, when I want to Edit a PRODUCT information to get combobox with option I selected during the inserting the data...
I could succeed to fill the data for the name of products and other information, only Combobox is empty. I hope you could understand what I want to achieve! Thank you in advance for your time!!!
See bellow what I tried but did not succede:
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
Try the following in your edit page,
<?php
$CATEGORY = 3; //from DB table, consider 3 as category id for sample
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
If I understood you correctly then on Edit form you should mark the option that should be selected with 'selected ' tag:
<Option value="2" selected="selected">2</Option>
Tryt this..If I understood corectly than:-
<option value="your_id" <?php echo $CATEGORY == your_id ?'selected':'';?>>your_category_name</option>
here $CATEGORY will be the retriving data from table
for your edit page you should do like this:-
<option value="1" <?php echo $CATEGORY == 1 ?'selected':'';?> ><?php echo $options;?></option>

Categories