473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Database to lisbox...ok but onchange, I need some help please

18
The simple code below gets data from a Location field and puts them into a lisbox. What I want to do is to be able to select one from the list and run a new function. How to get $areasearch valid?
Thanks in advance for any help
F

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <h3>List Box Form Data</h3>
  4.   <p>Form data passed from the form</p>
  5.      <?php
  6.       require("./resources/globals.php") ;
  7.      $query="SELECT DISTINCT * FROM Garant GROUP BY Location";
  8.       $stmt= substr($query, 0, strlen($query)-4) ;
  9. // Connect to the Database
  10. $link=mysql_connect($location,$userName,$password) or die (mysql_error());
  11. mysql_select_db($dbname) or die (mysql_error());
  12. $result=mysql_query($query) or die (mysql_error());
  13. // Select the Database
  14. if (!mysql_select_db($dbname, $link)) {
  15.    DisplayErrMsg(sprintf("Error in selecting %s database", $dbname)) ;
  16.    DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
  17.    exit() ;
  18. }
  19.  
  20. // Execute the Statement
  21.  if (!($result =mysql_query($query, $link))) {
  22.     DisplayErrMsg(sprintf("Error in executing %s stmt", $query)) ;
  23.    DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
  24.    exit() ;
  25. }                                                                    
  26.  echo '<select name="areasearch" onchange="return dropdown(this)">';
  27.  
  28.  while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  29.  echo "<option value=$nt[Town]>$nt[Location]</option>";
  30.  }
  31.   echo"</select>";
  32.  echo" </form>";
  33.  echo $areasearch;
  34.  
  35.  mysql_free_result($result) ;
  36.  
  37.  Function  onSelection($areasearch){
  38.  echo "<here>";
  39. }
  40.    ?>
  41.  
  42.   </body>
  43.   </html>
Oct 27 '09 #1
13 1803
Dormilich
8,658 Expert Mod 8TB
@phobia1
hard to tell because you didn’t tell what the form action is. the main point is that the function should be run after the user selects something (is that a form submission?) on the client side. So it is either something you do while processing the form or processing the form with AJAX.

note:
$nt[Location] should be $nt["Location"], otherwise consider this example
Expand|Select|Wrap|Line Numbers
  1. define("Location", 12345);
  2. $nt[Location] // is the same as $nt[12345]
and you have at least notice reporting off…
Oct 27 '09 #2
phobia1
18
Thanks for the reply.. I have made a few changes and it is trying to work, so I hope that the correction is possibly minor:-)
Expand|Select|Wrap|Line Numbers
  1. // Generate the SQL command for doing a select from the Database
  2. 1. $query="SELECT DISTINCT * FROM Garant GROUP BY Location";
  3.  
  4. 2. $stmt= substr($query, 0, strlen($query)-4) ;
  5.  
  6. // Connect to the Database
  7. 3. $link=mysql_connect($location,$userName,$password) or die (mysql_error());
  8. 4. mysql_select_db($dbname) or die (mysql_error());
  9. 5. $result=mysql_query($query) or die (mysql_error());
  10.  
  11. // Select the Database
  12. 6. if (!mysql_select_db($dbname, $link)) {
  13. 7.   DisplayErrMsg(sprintf("Error in selecting %s database", $dbname)) ;
  14. 8.   DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
  15. 9.   exit() ;
  16. 10.}
  17.  
  18.  
  19. // Execute the Statement
  20. 11.if (!($result =mysql_query($query, $link))) {
  21. 12.   DisplayErrMsg(sprintf("Error in executing %s stmt", $query)) ;
  22. 13.   DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
  23.  14.  exit() ;
  24. 15.}     
  25.  
  26. 16, echo "<form>";
  27.  
  28. 17. echo '<select name="areasearch" onChange="submit();">'; 
  29. // printing the list box select command
  30.  
  31. 18. while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  32. 19. echo "<option value=$nt[Town]>$nt[Location]</option>";
  33. /* Option values are added by looping through the array */
  34. 20.}
  35. 21.echo "</select>";// Closing of list box
  36.  
  37.  
  38. 22.echo "</form>";
  39. 23.echo $areasearch;
  40.  
  41.  
  42. 24.output_array($areasearch); //$pricelow and $pricehigh correct BUT...
  43.  
  44. 25.Function  output_array($areasearch){
Oct 27 '09 #3
Dormilich
8,658 Expert Mod 8TB
some questions
  • what is the error/misbehaviour you encounter?
  • where does $areasearch come from?
  • where do you submit the form to?
  • what does output_array() do exactly?

and some remarks
  • $nt[Location] should be $nt["Location"], Location is not a string but a constant, and due to the lack of the appropriate constant definition, PHP tries the constant’s name as string.
  • the form’s required action attribute is missing

PS. please use [code] tags when posting code, makes it far easier to read.
Oct 27 '09 #4
phobia1
18
:-)
The error is that the onchange action gives me Veliko everytime..no matter what I select.
areasearch ooooo that doesn't get passed from the listbox...oops
The form I just submit it back to itself..maybe should be with PHP-SELF action.
Function output array displays a set of pictures and does work correctly. All I need is the value for areasearch or in other words the text in the selection..for example if I select Leeds...then I want the $areasearch to be Leeds.
I can send you the full page if you like.The problem is that I haven't worked with PHP and HTML for about 2 years and have forgotten so much.
Best
F
Oct 27 '09 #5
Dormilich
8,658 Expert Mod 8TB
@phobia1
technically that’s not an error, you just specified "veliko" to be the return value for every <option>.
Oct 27 '09 #6
phobia1
18
More than likely:-)
Obviously I am out of depth for the time being with this problem. Ill do some reading to figure out How I can make the idea to work. To my mind the hardest part works...populating the listbox. Now I have to figure out how to get the text value of the selected item.
Thanks very much for what you tried to tell me.
Best
F
Oct 27 '09 #7
Dormilich
8,658 Expert Mod 8TB
what about setting text value and element value alike?
Expand|Select|Wrap|Line Numbers
  1. echo "\n\t<option value='", $nt["Location"], "'>", $nt["Location"], "</option>";
although it is a good idea to re-read the manual from time to time…
Oct 27 '09 #8
phobia1
18
:-) Superstar
That fixes the problem. Syntax and understanding of anything takes much time. I just do this stuff for a hobby.
I'll keep reading as have many things that I would like to be able to.
Many thanks again
Fred
Oct 27 '09 #9
Dormilich
8,658 Expert Mod 8TB
@phobia1
So do I.
___________
Oct 27 '09 #10
phobia1
18
I really really do appreciate the help you gave me. Perhaps one day I can return the favour
Oct 27 '09 #11
Markus
6,050 Expert 4TB
Some nitpicking: use single quotes where you are not expecting to have variables parsed inside the string. Double quotes will parse the string for variables (extra overhead, although not much).

I think I'll run some benchmarks to see how much of a difference there is.
Oct 27 '09 #12
Dormilich
8,658 Expert Mod 8TB
@Markus
I’d be interested in the results.

$foo['bar'] vs. $foo[bar] is a difference of about 700%
Oct 27 '09 #13
Markus
6,050 Expert 4TB
@Dormilich
Apparently the results are of little difference (scroll to bottom).
Oct 27 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: | last post by:
I am hoping a mixed ASP- Javascript programmer could help me with this scenario: I have a list box populated with randomized records (NewID()) from a Broker (Salesperson) sql server table. ...
3
by: Lee Mundie | last post by:
Hi there, Simple problem here but can't seem to fix it! Okay, I have a select list from which people choose avatars... the list is option values ie. <option>Worm</option> ...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
7
by: Peter | last post by:
Does anyone know how to trap SelectedIndexChanged event on the client side for the following control? http://www.metabuilders.com/Tools/ComboBox.aspx I have tried...
3
by: lowslyn | last post by:
hi, can someone please help me with javascript... i have this code that checks a text field when the user overrides the value to blank/space. if it is blank/space, i need to change the value...
5
by: giandeo | last post by:
Hello Experts. Could you find a solution for this problem please! I have the following tables in Access Database Table Name: origin Fields Names: country, countrycode Table Name: make...
1
by: ALT75 | last post by:
Hi all, I'm after some guidance for a friend of a friend so to speak, JavaScript isn't my thing so i can't help but i'm hoping someone can. The email i recieved is as follows; I have a drop down...
0
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,...
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.