i have 2 tables 1 is status_type with field name status and other is users with field username and status
now i want that the first listbox lists all status from status type ( this i have achieved with php )
and based on selection of the value in listbox 1 the corresponding data in table users be loaded in listbox 2 .
i have achieved this by using a button in php but i want tht it operate without submit button and no page refreshing
something like whats shown in http://www.plus2net.com/php_tutorial/dd.php i donno javascript so cant figure out wat and where to make the changes
here is my code which works with a submit button in php...plz help me out 45 6788
There's at least two ways in which you could do this:
1. Loading all the data on page load (using PHP to generate JavaScript code) and then using JavaScript to populate the list box.
2. Using Ajax- call a PHP script which returns the options and set the select to that.
Which do you prefer?
the first one sounds like wat i mentioned
So, using PHP, generate something like this (assuming status type is a number): - var status = [];
-
status[0] = ['joe','ann','user','and so on'];
-
status[1] = ['some','more','users'];
then onchange (first select), empty the list box and then loop over the corresponding array to populate the contents, e.g. - selObj.options[i] = new Option(status[currStatus][i], status[currStatus][i]);
where currStatus is the currently selected status.
acoder i m a newbie in this and hence slightly lost when u said tht part abt onchange....btw the status is not nos. its variable like ban pending ....so what shud my code line be exactly ? and where shud i put it in?
u want me to put the onchange in select for users or select for status?
@angelicdevil
the onchange property in the select menu will let you call a javascript function which will be making an ajax call to a php script which will browse all the users from the data base regarding the status or whatever you are tying to view
did you get that or not ?
regards,
omer aslam
Based on what you've said earlier, it'd be status: - <select id="status" onchange="changeUsers()">
where changeUsers is the function that changes the second select (users).
thanx acoder but what is changeUser() function...what is tht code ?
That was just an example function name. You can name the function whatever you want.
no i m asking wat shud be there in tht function ... its code
As I mentioned earlier (quoted for reference)
@acoder
you need to empty the list box (select) element (the users one). You can set the length of options to 0 or null. Then use a loop (e.g. for) to loop over the array for the correct status. See what you can manage from that.
this is wat i did still not working and giving parse error at 122
That's because you haven't defined selObj and the status array, etc.
Before you do sort that out, you need to define a data structure to hold the data. An array would be easiest for you. Use PHP to generate it, e.g. - var status = [];
-
// generated by PHP
-
status["astatus"] = ["some","user","names"];
Just to add (to make things easier), the steps that you need to take to get a working solution:
1. Set up a data structure containing all the data (see previous post - an array should work well).
2. Define a function to be called onchange - done.
3. In that function, access the users list box and remove all options.
4. Then get the selected option value from the status list box.
5. Use that to get the corresponding array (values) in the status array.
6. Finally, use a loop to loop over the array and add options one by one (the line you have in changeUsers currently).
but i need the users list to populate other fields so wont i need the option value for users?
also the status has been defined in the status_list ...its being populated with data from database
...which is why you populate it again with the new options (point 6).
i m feeling lost ...guess i'll try again.
@angelicdevil
But that's a PHP variable - JavaScript can't see it. You need to generate JavaScript using PHP code, e.g. - <script type="text/javascript">
-
var status = [];
-
<?php
-
while (...) {
-
echo "status['".$status[$i]."'] = [...];";
-
}
-
?>
u mean the select options for users and status should be populated in javascript itself?
ok this is wat i did still not orking....can u make the changes and post it....i cant seem to figure it out. -
<?php
-
// Choosing a Status
-
$status_list = array();
-
-
$query_status = " SELECT status_type.status from status_type ";
-
-
$result_status = mysql_query($query_status);
-
confirm_query($result_status);
-
-
-
$status_list[]=" ";
-
while ($record = mysql_fetch_assoc($result_status)) {
-
$status_list[] = $record['status'] ;
-
-
}
-
$status = $_GET['status'];
-
-
?>
-
-
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
-
-
<select name="status" onchange="changeUsers()">
-
<script type="text/javascript">
-
<?php
-
-
// displaying status
-
foreach ($status_list as $value => $option)
-
{
-
$selected = ($status == $value) ? ' selected="selected"' : '';
-
echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
-
}
-
-
?>
-
</select>
-
-
</form>
-
-
<?php
-
// Part 2: Using the Chosen Status to Search the Database
-
$usernames = array();
-
$query = "SELECT username FROM users WHERE users.status = '{$status_list[$status]}'";
-
$result = mysql_query($query);
-
if ($result)
-
{
-
// Fetches a row until there are no more rows to fetch
-
while ($row = mysql_fetch_assoc($result))
-
{
-
// Stores the username from the fetched row in the $usernames array
-
$usernames[] = $row['username'];
-
}
-
}
-
if (count($usernames) > 0)
-
{
-
?>
-
-
<ul>
-
<td><select name="users" maxlength="23" >
-
<?php
-
// Loops through the list of usernames
-
foreach ($usernames as $name )
-
{
-
// Builds a list item for each username
-
echo '<option>'.$name.'</option>';
-
}
-
?>
-
-
</select> </td>
-
-
</ul>
-
<?php
-
}
-
else
-
{
-
?>
-
<td><select name="users" maxlength="23" type="multiple">
-
<?
-
echo '<option>'." ".'</option>'; ?>
-
-
-
}
-
-
<?
-
function changeUsers(){
-
selObj.options[i] = new Option(status_list[currStatus][i], status_list[currStatus][i]);
-
}
-
?>
-
@angelicdevil
Initially, the default options would be populated using PHP and subsequent options (for users) would be populated using JavaScript.
@angelicdevil
On line 23, remove the script tag. The function changeUser needs to be within script tags. You still haven't added PHP code to generate the JavaScript array to store the data. For that, you will need a query which lists the users by status.
damn i m just not able to get it to work!!!!! acoder please cud u make the neccessary changes and post it so i know where the hell i m messing it up big time.
thanx in advance
Well, I could have done that in the very first reply, but that wouldn't really help. I've been trying to show you the steps, so that I can help you figure it out.
However, this calls for a simple example: - <html>
-
<head>
-
<script type="text/javascript">
-
var statuses = [];
-
statuses[0] = ["some","user","names"];
-
statuses[1] = ["some2","user2","names2"];
-
statuses[2] = ["some3","user3","names3"];
-
function changeUsers() {
-
var users = document.getElementById("users");
-
var status = document.getElementById("status").selectedIndex;
-
users.options.length = 0;
-
userArr = statuses[status];
-
for (i = 0; i < userArr.length; i++) {
-
users.options[i] = new Option(userArr[i],userArr[i]);
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<form>
-
<select id="status" onchange="changeUsers()">
-
<option value="0">test</option>
-
<option value="1">test2</option>
-
<option value="2">test3</option>
-
</select>
-
<select id="users">
-
</select>
-
</form>
-
</body>
-
</html>
Now you just need to adapt this to your requirements.
ok this is wat i did but now i m getting parse error. on line 128
i checked again and again cant seem o get it to work...please see if i have coded it right and can correct it too?
if you don't have short tags enabled, replace <? by <?php (lines 116, 125)
The code that you have from lines 4 to 17 is PHP code, but it's not within PHP tags. I think you meant to create the JavaScript arrays, but you need to echo it out onto the page. The syntax needs to be correct too. Look at the example. Did you try it out? Now the PHP code (when you check the source on the client) should produce something similar.
hi i found a code on net which does wat i want ...i tried to modify it to fit my needs but its not working as i want it to ....could be please check....
dd.php -
<?php
-
-
$dbservertype='mysql';
-
$servername='localhost';
-
// username and password to log onto db server
-
$dbusername='lucky';
-
$dbpassword='lucky';
-
// name of database
-
$dbname='stockphotos';
-
-
-
////// DO NOT EDIT BELOW /////////
-
-
connecttodb($servername,$dbname,$dbusername,$dbpassword);
-
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
-
{
-
global $link;
-
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
-
if(!$link){die("Could not connect to MySQL");}
-
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
-
}
-
//////// End of connecting to database ////////
-
?>
-
-
-
-
<html>
-
-
<head>
-
<title>Multiple drop down list box </title>
-
<SCRIPT language=JavaScript>
-
function reload(form)
-
{
-
var val=form.cat.options[form.cat.options.selectedIndex].value;
-
self.location='dd.php?cat=' + val ;
-
}
-
-
</script>
-
</head>
-
-
<body>
-
<?
-
-
@$cat=$_GET['cat']; // Use this line or below line if register_global is off
-
if(!is_numeric($cat)){ // to check if $cat is numeric data or not.
-
echo "Data Error";
-
exit;
-
}
-
-
// Getting the data from Mysql table for first list box//////////
-
$query_usernamey_status=mysql_query("SELECT status,status_name FROM status_type ");
-
// End of query for first list box////////////
-
-
// for second drop down list we will check if category is selected else we will display all the subcategory/////
-
if(isset($cat) and strlen($cat) > 0){
-
$query_username=mysql_query("SELECT username FROM users WHERE status=$cat ");
-
}
-
// end of query for second subcategory drop down list box ///////////////////////////
-
-
echo "<form method=post name=f1 action='dd-check.php'>";
-
// Add your form processing page address to action in above line. Example action=dd-check.php////
-
-
// Starting of first drop downlist /////////
-
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
-
while($record1 = mysql_fetch_array($query_usernamey_status)) {
-
if($record1['status']==@$cat){echo "<option selected value='$record1[status]'>$record1[status_name]</option>"."<BR>";}
-
else{echo "<option value='$record1[status]'>$record1[status_name]</option>";}
-
}
-
echo "</select>";
-
// This will end the first drop down list ///////////
-
-
-
// Starting of second drop downlist /////////
-
echo "<select name='subcat'><option value=''>Select one</option>";
-
while($record2 = mysql_fetch_array($query_username)) {
-
echo "<option value='$record2[username]'>$record2[username]</option>";
-
}
-
echo "</select>";
-
// This will end the second drop down list ///////////
-
-
// Add your other form fields as needed here/////
-
echo "<input type=submit value=Submit>";
-
echo "</form>";
-
?>
-
<center></center>
-
</body>
-
-
</html>
-
dd-check.php -
-
<html>
-
-
<head>
-
<title> Multiple drop down list box </title>
-
<meta name="GENERATOR" content="Arachnophilia 4.0">
-
<meta name="FORMATTER" content="Arachnophilia 4.0">
-
</head>
-
-
<body>
-
<?
-
$cat=$_POST['cat'];
-
$subcat=$_POST['subcat'];
-
-
echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat ";
-
-
-
?>
-
<center>
-
-
</center>
-
</body>
-
-
</html>
-
plz help
The line: - self.location='dd.php?cat=' + val ;
reloads the page to change the options. I thought you wanted to avoid that. With the code you've posted, the whole approach changes which means you're effectively back to square one.
i m fine with it as long as it loads the values based on selection ...
can u figure out wats going wrong in this new one?
The problem from a cursory glance seems to be in your PHP code, e.g. $record1[status] should be $record['status']. Do you not see any errors?
yes i m getting the error below - 0){ $quer=mysql_query("SELECT username FROM users WHERE status=$cat "); } // end of query for second subcategory drop down list box /////////////////////////// echo "
-
"; // Add your form processing page address to action in above line. Example action=dd-check.php//// // Starting of first drop downlist ///////// echo ""; // This will end the first drop down list /////////// // Starting of second drop downlist ///////// echo ""; // This will end the second drop down list /////////// // Add your other form fields as needed here///// echo ""; echo "
-
"; ?>
with record or record1 the error is same
That's not an error message: it's code. Maybe the problem is that you're using short tags <? instead of the full PHP opening tag <?php on line 42.
changed it to full tags still same issue
I suggest you post a new thread in the PHP forum because the problem relates to PHP and is no longer relevant to this thread.
wat abt the previous script where i modified as u said.....any thing can be done to make tht work?
Yes, of course. Did you make the changes as suggested in posts #29/30?
yes still they r not working
Let me see the latest version, so we know what we're working off.
Put line 3 inside the PHP tags. Your PHP code at the beginning doesn't actually produce any output. It needs to echo out some data. For that, you will need a second query which gets the user names based on each status (see original PHP code). As an example: - while ($record = mysql_fetch_assoc($result_status)) {
-
$status_list[] = $record['status'] ;
-
$query = "SELECT username FROM users WHERE users.status = '{$record['status']}'";
-
$result = mysql_query($query);
-
echo "statuses[$i] = [";
-
while ($row = mysql_fetch_assoc($result))
-
{
-
echo $row['username'].",";
-
}
-
echo "];";
-
}
I haven't tested this code, but based it on code you already have just to give you an idea.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Darren Line |
last post by:
Could someone kindly point me the direction of some sample script or
tutorial that will enable me auto populate fields on a page.
By this I mean, I have a page with address fields on it, they...
|
by: judiphuongtu |
last post by:
I am trying to create an input form with a pull-down menu that allows a
user to add an item to the pull-down. I don't want the page to refresh
when I refresh the pull-down since there's entries on...
|
by: RUSSELL MCGINNIS |
last post by:
I have a web site that uses Form Authentication with the Session timeout set
to 20 minutes, however one of the pages refreshes itself every 30 seconds. Is
there a way to override the session...
|
by: Arielle |
last post by:
I've argued with this for quite some time and I've seen a number of other people on various other sites asking for this same thing with no answer so I figured that now that I have solved it I would...
|
by: PerumalSamy |
last post by:
Hi, I had created asp.net page with checkbox control. I set autopostback property as true and i wrote codings inside checked changed of that checkbox to do some process . In runtime, while i clicking...
|
by: lakshmishri |
last post by:
hi all
can anyone answer this ??
i have developed a module which displays cricket score card and this is one of the part in a HTML page along with other modules. now this module alone must be...
|
by: m1post |
last post by:
Hi,
I'm very much a novice, and wondered if someone could help. I'll try to be as specific as possible, but don't waant you to have to read too much.
I have 2 pages in the sequence. 1st allows...
|
by: onlymars |
last post by:
helloo guys, i have an image gallery page which have thumbnails at the bottom of the screen and the main image is in the middle of the page, i want that users click on the thumbnail pic's and the...
|
by: rahullko05 |
last post by:
one line code to replace an image without page refresh
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |