Two drop down lists in Ajax and MySQL - php

I'm trying to learn some Ajax and MySQL...
Basically, the code that I have right now is similar to: http://www.w3schools.com/PHP/php_ajax_database.asp
I was wondering how should I modify those codes (both html, javascript and php) to create two drop down lists. The first drop list would be used for choosing the Lastname (eg. Swanson) and the second drop list would be used for choosing the Hometown (eg. Quahog). Then the user would click "Submit" button and the query would return the matching results (eg. all the Swansons living in Quahog).
I'd be very thankful for all the ideas!

The only major difference to the html page will be the addition of another dropdown, addition of the submit button, and removal of the onchange event (assuming you want this removed since you're relying on the submit button). So instead of:
<form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
You now have:
<form>
Select a User:
<select name="lastname" id="lastname">
<option value="Griffin">Griffin</option>
<option value="Quagmire">Quagmire</option>
<option value="Swanson">Swanson</option>
</select>
<br>
Select a Hometown:
<select name="hometown" id="hometown">
<option value="Quahog">Quahog</option>
<option value="Newport">Newport</option>
</select>
<input type="submit" value="Submit" onclick="showUser(Document.getElementById('lastname').value, Document.getElementById('hometown'))">
</form>
The only change you need to the javascript is to change the showUser function to take and process the two inputs. So the declaration becomes:
function showUser(lastname, hometown)
and instead of the line:
url=url+"?q="+str;
you need:
url=url+"?lname="+lastname+"&town="+hometown;
Then in the PHP, you need only change the variable assignments and the query that is being executed. So:
$q=$_GET["q"];
Becomes:
$lname=$_GET["lname"]; $town=$_GET["town"];
And:
$sql="SELECT * FROM user WHERE id = '".$q."'";
Becomes:
$sql="SELECT * FROM user WHERE lastname = '".$lname."' AND hometown = '".$town."'";

Related

retrieve selected value in form_dropdown() and act on it in the same view

I am using codeigiter's form_dropdown() and would like to make the selection alter data farther down in the same view without submitting the form.
In the view php file:
echo form_dropdown('department_select',$options,'1');
<?php $test_list = $this->trainingmodel->get_dept_tests($deptselected); ?>
I would like $deptselected to reflect what the user has selected in the form_dropdown(). I would like this to update every time the user changes their dropdown selection but without submitting the form.
Things like the following would work if the form was submitted, but not before:
$deptselected = $_POST['department_select'];
or
$deptselected = $this->input->post('department_select');
There must be a way to do what I want using javascript, or onchange or the 4th input parameter to form_dropdown().
You can use the chained select jquery plugin http://www.appelsiini.net/projects/chained
A simple example taken from the website:
<select id="mark" name="mark">
<option value="">--</option>
<option value="bmw" selected>BMW</option>
<option value="audi">Audi</option>
</select>
<select id="series" name="series">
<option value="--">--</option>
</select>
<select id="engine" name="engine">
<option value="--">--</option>
</select>
<select id="transmission" name="transmission">
<option value="--">--</option>
</select>
And on
$("#transmission").remoteChained({
parents : "#engine",
url : "/api/transmissions.json",
depends : "#series"
});
You can set the ajax url to your Codeigniter controller that will serve the data to the view in json format.

Multiple selections into input box

I have a selection of countries that a user can choose from. They are displayed in a dropdown box. At the moment I can select one country and it will populate it in the input box, however i want to know if there is a way of selecting more than one and it puts them in the same box.
The data will be populated from a mysql database
Hope im making my self clear.
Here is my code so far:
JAVASCRIPT:
<script language="JavaScript" type="text/javascript">
<!--
function showSelected()
{
var selObj = document.getElementById('countries');
var txtTextObj = document.getElementById('txtText');
var selIndex = selObj.selectedIndex;
txtTextObj.value = selObj.options[selIndex].text;
}
//-->
</script>
HTML:
<form>
<p>
</form><select id="countries">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
<input type="button" value="Show Index" onclick="showSelected();" />
</p>
<p>
<input type="text" id="txtText" />
<br />
</p>
</form>
So what i would want is if you choose Australia, it shows Australia in the box, then go back to the dropdown and choose South Africa it puts it into the same box as: Australia, South Africa.
Thank you Guys
why no just append new selections to the text already in the inputbox.
txtTextObj.value += selObj.options[selIndex].text +', ';
Use Multiple select box and name as array:
<select id="countries" multiple="multiple" name="countries[]">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Now the select will treat as array. Use javascript code to fetch the values selected and convert it to comma separated list and put in the input box.
<select id="countries" multiple="multiple">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Use multiple="multiple" in your select box to make multiple selections.

How to disable drop down list

I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else?
<select name="Type" id="type">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
<?php
$value=$_POST['image'];
?>
</select>
<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select
You can do this with PHP, but that's going to require submitting form the data so you can then access $_POST, and then loading a new page, that's basically the same as the original, just with text-one disabled.
Remember, PHP runs server side, and once it's rendered in the browser, nothing else can be done via PHP. For example, you've got a line in your sample code that show's you tring to assign $_POST['image'] to $value - until you submit a form, $_POST will be empty.
Most likely, you want to do this client side and without a reload, and this can be done using javascript.
As a basic overview:
monitor the onChange event handler for your type select element
check the value of the type select element, and set the disabled attribute on the text-one as needed
Another option (maybe simpler?):
attach an onclick attribute to the video input, that will run a javascript function that disables text-one
jQuery will make some of this easier, but you could write all of the above in plain javascript, without any libraries.
try this code :
<script>
function disable(val)
{
if(val=="V")
document.getElementById("text-one").disabled=true;
else
document.getElementById("text-one").disabled=false;
}
</script>
<select name="Type" id="type" onchange="disable(this.value)">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
</select>
<select id="text-one" >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select>

how to get values from multiple select/listboxes in php?

i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.

Working with "multiple" select

In my profile advanced search i have an mutiple select type..
<form method="POST">
<select size="6" name="Cities" multiple="multiple" id="Cities">
<option value="0">All</option>
<option value="1">City1</option>
<option value="2">city2</option>
<option value="3">city3</option>
<option value="4">city4</option>
<option value="5">city5</option>
</select>
</form>
How do I use it, I mean how should i call what they have chosed...As you can choose one or more options (by holding in control, or just holding in mouseclick and point to other options).
Give it the name Cities[] instead of Cities, and you'll have an array in $_POST['Cities'] on submit.
google helps
http://www.onlinetools.org/tricks/using_multiple_select.php

Categories