I am facing issue to set the Auto drop down in my php form.
while selecting the QC Type, it should automatically select QC Code from MySQL Database. At the moment, i am selecting it manually. Can anyone help.
php Form :
<tr>
<td align="right"><b>QC Code</b></td>
<td>
<select name="qc_code" >
<option>Select a QC Code</option>
<?php
$get_qc = "select * from qctype";
$run_qc = mysqli_query($con, $get_qc);
while ($row_qctype=mysqli_fetch_array($run_qc)){
$qccode_title = $row_qctype['qccode_title'];
echo "<option value='$qccode_title'>$qccode_title</option>";
}
Database : -
You should put the title into the value instead. This way the title will be displayed and the value will be submitted.
$qccode_title = $row_qctype['qccode_title'];
$qctype_title = $row_qctype['qctype_title'];
echo "<option value='$qctype_title'>$qccode_title</option>";
Related
I am having a form, which contains select option for data entry, (the options are fetching from MySQL database) if I want to update the form at later stage, is it possible to get the form select option as pre-selected?
Here I am showing the code for form having select options
<?php
$queryjob = "SELECT * FROM `job`";
$result2 = mysqli_query($con, $queryjob);
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<tr>
<td ><div class='tabdata' align="right"> Name of Job/Survey: </div></td>
<td > <div class='tfieldz' align="right">
<select class='tfieldz' id="job_name" name="job_name" required='required'>
<?php echo $options;?>
</select></div>
</td>
</tr>
You can add a selected="" attribute to the option to have the option pre-selected.
Example would look like:
//You could use this in your loop
if($saved_value == $row[1]){
echo "<option selected>$row[1]</option>";
}else{
echo "<option>$row[1]</option>";
}
As I understand you are trying to set the default selected value of the selectbox. To set this value you can use following code:
/** The selectbox default value. This can come from database */
$selectbox_value = "selectbox default value";
while($row2 = mysqli_fetch_array($result2))
{
if ($selectbox_value == $row2[1]) $options = $options."<option SELECTED value='".$row2[1]."'>$row2[1]</option>";
else $options = $options."<option value='".$row2[1]."'>$row2[1]</option>";
}
In the above code the value attribute is added to the option tag. When the form is submitted, the value of the value attribute is sent to the server and stored in database. This stored value should be read from database and saved to $selectbox_value
how can i put the $CATEGORY dynamically so that whatever i click on the table it will retrieved in the combo box? (without settng its id to any number like 5 )
<?php
$CATEGORY = 5; //from DB table, consider 5 as category id for sample
$sql="SELECT tblcourse.id as id, tblcourse.course as course FROM tblcourse";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["course"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>";
}
?>
My Combobox form code below :
<select name="cbocourse" style="height:35px; width:280px; background-color:#923227; box- shadow:1px 1px #FFF;color:#C90;" onClick="submitCATEGORY();">
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option></select>
Your combobox code is wrong : you cannot parse a list of option into another super option, it means nothing. Just parse your $options between your select tags.
Just be sure to reload your page (or page fragment with AJAX for example) each time you call submitCATEGORY(), in order to regenerate your html combobox.
Your PHP code seems good.
I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.
I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").
I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.
index.html shown below:
<script>
$(document).ready(function() {
$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box
$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});
</script>
<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>
</div>
<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select>
</div>
</body>
</html>
sector_options.php shown below:
<?php
$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());
$query = "SELECT sector FROM Main_table ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>\n ";
}
echo $options;
?>
subsector_options.php shown below:
<?php
$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());
$Sectors = $_REQUEST['filter_sector'];
$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>\n ";
}
echo $options;
?>
For completeness, the solutions were:
Check how AJAX operations are doing using a browser network monitor
Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.
I want to insert a option from a dynamic populated MySQL select box, into another table with other form variables. Please excuses all the newbie errors.
This is the area of my form where the select box appears:
<select class="form-control3" name="Select_Name" id="Select_Name">
<?php include('../../controllers/controller1.php') ?>
</select>
This is where it is declared in PHP:
$Select_Item1 = $_POST['Select_Item1'];
$Select_Item2 = $_POST['Select_Item2'];
$Select_Item3 = $_POST['Select_Item3'];
$Select_Item4 = $_POST['Select_Item4'];
$Select_Name = $_POST['Select_Name'];
$insert_movie = mysqli_query($con, "INSERT INTO ".$tbl_name." (Select_Item1, Select_Item2, Select_Item3, Select_Item4, Select_Name) VALUES ('". $Select_Item1."','". $Select_Item2."','". $Select_Item3."','". $Select_Item4."','".$Select_Name."')");
But when I submit the form, everything submits except the $_POST['Select_Name'];
Any help will be greatly valued. Thank you.
can you echo $_POST['Select_Name'] and see what is its value? Maybe you are not doing it right in the <?php include('../../controllers/controller1.php') ?>
Can I see what are you doing on you controller?
Also why dont you pass data from controller like a variable and do a foreach between the select command?
//codeigniter syntax
<select class="form-control3" name="Select_Name" id="Select_Name">
<?php foreach($var as $key):?>
<option value="<?=$key?>"><?=$key?></option>//sth like this
<? endforeach ?>
</select>
So, im creating a form that will submit data into an SQL database. Ive got 2 select drop downs that hold the data of "Module Code" and "Module Title". In the database a module title will only have one module code e.g Team project(module title) has module code 11COB290.
How can i get it so that when a user selects a given module name OR Module title is will automatically select the correct partner i.e select the right module code thats related to the module name the user has selected without pressing any submit buttons?
The following code is the drop down select boxes and php code i have so far:
<td align="center">
<select name='ModuleTitle' id='ModuleTitle' style='width:100%;'>
<option>Select...</option>
<?php
//3. Perform database query
$result = mysql_query("SELECT * FROM Module
ORDER BY `ModTitle` ASC;", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row = mysql_fetch_array($result)) {
$module = $row[2];
echo "<option name='{$module}'>{$module}</option><br />";
}
?>
</select>
</td>
<td align="center">
<select name='ModuleCode' id='ModuleCode' style='width:100%;'>
<option>Select...</option>
<?php
//3. Perform database query
$result = mysql_query("SELECT * FROM Module
ORDER BY `ModCode` ASC;", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row = mysql_fetch_array($result)) {
$module = $row[3];
echo "<option name='{$module}'>{$module}</option><br />";
}
?>
</select>
</td>
Unless there is a special reason to do so, why not give a single SELECT box instead of two?
<td align="center">
<select name='ModuleTitle' id='ModuleTitle' style='width:100%;'>
<option>Select...</option>
<?php
//3. Perform database query
$result = mysql_query("SELECT * FROM Module ORDER BY `ModTitle` ASC;", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row = mysql_fetch_array($result)) {
$module = $row[2] . ' (' . $row[3] . ')';
$moduleCode = $row[3];
echo "<option value='{$moduleCode}'>{$module}</option>";
}
?>
</select>
</td>
Or otherwise if you would like to keep 2 SELECTs, use AJAX calls. The idea is to define onChange event on each SELECT and in that event, send an AJAX request to a PHP script. The PHP script will request the module code or title and send it back to the AJAX handler. The AJAX handler can then automatically mark as SELECTED the corresponding option in the other SELECT. You might need sometime to research on AJAX and JavaScript if you aren't already experienced with this stuff.
Another idea might be to use the module code as the value for the title SELECT and module title as the value for the code SELECT. For example, title SELECT will be:
<SELECT name="ModuleTitle" id="ModuleTitle" style="width:100%;">
<OPTION>Select...</option>
<OPTION value="123">Title 123</OPTION>
<OPTION value="345">Title 345</OPTION>
<OPTION value="567">Title 567</OPTION>
</SELECT>
Then in the onChange event handler you might do something like this:
var selObj = document.getElementById('ModuleTitle');
var selIndex = selObj.selectedIndex;
document.getElementById('ModuleCode').value = selObj.option[selIndex].text;
By the way, there is no "name" attribute for . It should be "value" instead. Please fix in your code.
Hope it helps!
CAUTION: none of the above code is tested but I hope it works fine