As title says I need help with onchange. I have select tag,and I need to do different mysql query when I choose something from select list. Example:
<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
and then when i select cars it do this
$query="select * from table where type='cars'";
and if I choose trucks it do
$query="select * from table where type='trucks'";
and so on...
then I need to display the result in div under the list
example
<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
<div> this is where I need to display results from query</div>
Please help!!!
If you want to change the result by ajax :
HTML :
<select id="list" name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
in JS File:
$(document).ready(function() {
$('#list').change(function() {
var selected=$(this).val();
$.get("change_query.php?selected="+selected, function(data){
$('.result').html(data);
});
});
});
and you should create change_query.php file and your query and code in it and return the result in it
$type = $_POST["selected"];
$query="select * from table where type='".$type."'";
print result here .... ;
Tell me if you need any help ,I just guided you in Jquery not all code
You should add your SELECT code to a form with method attribute set to post,
and also a submit button of course.
Then , php will get the value of those INPUTS and do whatever you want.
$term = $_POST['list'];
/*
In order to prevent unwanted queries or injection add an array with those terms and
check if the posted value is in this array:
*/
$terms = array("cars" , "trucks");
if(!array_key_exists($term , $terms))
die(); //bad bad bad boy.
$query = "select * from table where type='$term'";
you could use
onchange="this.form.submit();"
as well.
Related
I have two tables in my database. I have a form to add information to table news. In my form I have dropdown list with information from another table courses. I want to save the result of the selection and save it in table news. How I can do that?
code:
<p>Course</p>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("university");
$sql = "SELECT name FROM courses";
$result = mysql_query($sql);
print '<select>';
while ($row = mysql_fetch_assoc($result)) {
print '<option value='.$row['name'].'>'.$row['name'].'</option>';
}
print '</select>';
?>
First you would need to give a nameattribute to your select tag, then as soon as you post your form, you will get the value of the selected option the exact same way as you get the value of other input fields.
I would also advise to get both the id and the name of your courses from your selct query. You should then use the id as the value attribute of your option , qhile you can keep the name to display it to the user. That way you would store the id of the selected course instead of the name, meaning that if a name slightly changes, you will still have the proper course id stored.
So your final HTML for this field should look something like that :
<select name="selectedcourse">
<option value="1">Course 1</option>
<option value="2">Course 2</option>
<option value="3">Course 3</option>
</select>
And you would get the seleted value server-side in PHP using something like :
$_POST["selectedcourse"]
If, of course, your form method was POST and not GET...
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 have two tables "oc_t_item_car_make_attr" , "oc_t_item_car_model_attr"
So how can i make a Dependable Dropdown From these Two Tables?
You can only do this by Ajax.
Following is city country reference site, you can get idea from that.
Referance Site
if you stuck any where feel free to ask me.
Actually didn't understand question very well, but you probably want something like this:
SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.fk_i_make_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id = ?
You need to insert id in the place of question mark.
Here is the entire code what your looking for:
Below code will display all car names from database dynamically:
<select name="cars" id="carmodels">
<option value="">select</option>
<?php
$qry = mysqli_query("select * from oc_t_item_car_make_attr");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>
</select>
Below result will display car models dependent on the names selected above:
html:
<select name="models" id="models">
</select>
you need to use ajax to get the car models from database:
$(document).on('change','#carmodels',function(){
var value = $(this).val();
$.ajax({
url: "demo.php",
type: "GET",
data: { id : value},
success: function(data){
$("#models").html(data);
}
});
demo.php:
<?php
include "connection.php"; //database connection code
$qry = mysql_query("SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.pk_i_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id =".$_GET['id'].");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>
Hey you can do this by ajax
Reference Link 1
Reference Link 2
also study inner join
I can't seem to get the value for a 'selected item' to pass to a JavaScript function with PHP that uses the value to query data from MySQL database -> populates a different drop down list.
Here is my 'HTML' code
<select name="numbers" id="numbers" onChange="populateOtherSelect()">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<?php echo $option ?>
Here is my 'JavaScript' function code
function populateOtherSelect() {
<?php
// code that opens db connection
$numbers = $_POST['numbers'];
$query = "select*from Database.Table where Something = 'Something' and Numbers like '%".$numbers."%'";
$result = mysql_query($query);
$option = "";
while($row = mysql_fetch_array($result))
{
$OtherOptions = $row["OtherOptions"];
$option.="<option value=\"$OtherOptions\">".$OtherOptions."</option>\r\n";
}
// code that closes db connection
?>
}
Any information will be helpful.
Thanks very much
It seems your javascript code is part of the html / php document and that´s not going to work; the php is run only at initial page-load and at that moment there probably is no $_POST variable.
What you need to do, is make an ajax call to a php script at the moment your first select is changed and use the response from that ajax call to populate your second select.
So basically your javascript function would have to look like:
function populateOtherSelect() {
/*
* 1. get the value of the numbers select
* 2. make an ajax call to a php script / the server that gets some information from a database
* 3. use the response from the ajax call to populate your second select
*/
}
I think you need to add a space in your select statement:
$query = "select * from Database.Table where Something = 'Something' and Numbers like '%".$Numbers."%'";
First of all, the $Numbers variable doesn't exist, it's $numbers.
Can any one please help me? I am new to php, currently I am trying to use $_POST['item'] to obtain a string (consists of many words) from a dropdown box and then process it. However, when I use $_POST['item'], only first word is returned, the rest of words are missing.
I have a dropdown box, something like:
echo "<form action='process_form.php' method='post'>";
echo "Select an item<br />";
echo "<select name='item' id='item'>";
...
...
...
each item in the dropdown box is a string that has product names like:
dressmaker mannequin size 12
torso mannequin in white color
...
User will then select an item from the dropdown box. When I used $_POST['item'] to obtain this string, I get the first word "dressmaker", all the rests were missing.
How do I get the whole string?
Many thanks in advance.
I am not sure exactly what you are doing but I would do something like this. For this example I assume that the values "dressmaker mannequin size 12" will correspond to the values of the columns in the database to which I will refer to as "colA, colB, colC, and colD", and in addition I assume you have an "ID" column in your database.
Here is the code I would use to generate the select drop-down list:
//$query is the variable storing the result of the mysql_query();
//assumption is that the result-set is non-empty
echo '<select name="item" id="item">\n'; //\n - new line character
//run through the loop to generate the items inside the list
while ($result = mysql_fetch_assoc($query)) {
//note the id - it will be used to find data in the
//database after the POST is complete
//couple of temp variables (not necessary but makes code cleaner)
$database_id = $result["ID"];
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//add option to the select drop-down
echo "<option value=\"$database_id\">$colA $colB $colC $colD</option>\n";
}
echo "</select>";
Now to retrieve the data from the POST. I am including the code Drewdin suggested.
//form was submitted already
//assumption is that the database connection is established
$item_id = mysql_real_escape_string(trim($_POST["item"]));
//Now get the info from the database for this id
//table is "table"
$string = "SELECT * FROM table WHERE ID = $item_id";
$query = mysql_query($string) or die("Could not complete the query: $string");
//assumption here is that the result set is non-empty
$result = mysql_fetch_assoc($query);
$colA = $result["colA"]; //ex:dressmaker
$colB = $result["colB"]; //ex:mannequin
$colC = $result["colC"]; //ex:size
$colD = $result["colD"]; //ex:12
//now you can use the values of colA-D to compute whatever you want
Hope this helps. Using database ids is nice for security plus it makes things more manageable.
Regarding using this blog. You can post comments to the answers people post. If you want however to add something to your question, you can edit your original question and just make sure its obvious what was added.
When the issue is resolved and if any answer helped and you liked it, you can pick it as the final answer by clicking the check mark next to it.
Best of luck
without seeing your select options i think this might help you
if (isset($_POST['submit'])) {
// Grab the output of the select list
$Select_Output = mysqli_real_escape_string( $dbc, trim($_POST['item']));
//What ever else you want to do here...
}
I also would use Miki725's post to make sure your select list is setup correctly
This is how the php gets the values from the HTML select item:
<select name="item" id="item">
<option value="this is option 1">option 1</option>
<option value="this is option 2">option 2</option>
<option value="this is option 4">option 3</option>
...
</select>
Basically you would do something like:
$var = $_POST['item'];
The $var will be the whatever was entered into the "value" of the option tag. So just make sure you have the proper values entered into the value fields.
Hope that helps.