By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

POST & GET - Passing a string value from javascipt to php

P: 33
hi,
i have two drop down list.
First one is populated from database and its working fine.
Second one will also be populated from database but as per the value selected from the first drop-down list.

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3. function xyz_list()
  4. {
  5.  
  6. // xyz_list is the id of my first drop-down list
  7.   var xyz_list=document.getElementById("mob_list");
  8.   brand=xyz_list.options[mob_list.selectedIndex].text;
  9.  if (window.XMLHttpRequest)
  10.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  11.   xmlhttp=new XMLHttpRequest();
  12.   }
  13. else
  14.   {// code for IE6, IE5
  15.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  16.   }
  17. xmlhttp.onreadystatechange=function()
  18.   {
  19.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  20.     {
  21.     document.getElementById("xyz").innerHTML=xmlhttp.responseText;
  22.     }
  23.   }
  24. xmlhttp.open("GET","XYZ.php?brand=" + brand,true);
  25. xmlhttp.send();
  26. }
  27. </script>
  28. </head>
  29.  
  30. <body>
  31. <form>
  32. <select id="xyz" onfocus="xyz_list()">
  33. <OPTION VALUE=All>All 
  34. <?php echo $options?> 
  35. </SELECT> 
  36. </form>
  37. </body>
  38.  
Below is the xyz.php script

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $brand=$_GET['brand']; 
  3. //alert($brand); 
  4.  
  5.  //below is just inserting a blank value in drop list 
  6.  $options_mobile.="<OPTION VALUE=abc>".$brand.'</option>'; 
  7. $con = mysql_connect('localhost', 'root', ''); 
  8. if (!$con) 
  9.   { 
  10.   die('Could not connect: ' . mysql_error()); 
  11.   } 
  12.  
  13. mysql_select_db("mobile1", $con); 
  14.  
  15.  
  16. $sql="SELECT xyz FROM abc where pqr= '$brand'"; 
  17.  
  18. $result = mysql_query($sql); 
  19. $options.="<OPTION VALUE=\"$result\">".$result.'</option>'; 
  20. while($row = mysql_fetch_array($result)) 
  21.   { 
  22.  // $xyz=$row["xyz"];  
  23.   $xyz=$row["xyz"]; 
  24.    $options.="<OPTION VALUE=\"$xyz\">".$xyz.'</option>'; 
  25.  } 
  26. mysql_close($con); 
  27. ?>
  28.  

I am new to both of the languages, but i know wat my code wants to do. i googled a lot to find the error, But i cant figure out where i am wrong.
Any help will be highly appreciated.
Dec 13 '11 #1
Share this Question
Share on Google+
7 Replies


100+
P: 1,059
Where is the error and what error?

Give us the error detail
Dec 13 '11 #2

P: 33
sorry i missed it.
Error is, instead of expected value, only a blank value is inserted in my second drop list.
I have checked the 'brand' value using alert box in jscript and its working fine.
also i have included some dummy output like $options_mobile.="<OPTION VALUE>Choose11".'</option>'; in php file. its too working fine.

thanks..
Dec 13 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
two things, line #6 (php) throws a warning because you're appending a string to an undefined variable and on line #19 you append a resource to a string (which should give some unexpected output)
Dec 14 '11 #4

AutumnsDecay
100+
P: 170
I see that you have "<OPTION VALUE=ALL>All", but you never close that option. Try:

<option value='All'>All</option>

Also, for standards'-sake, don't use uppercase letters in elements and element attributes.

<OPTION></OPTION> will still work, but it's best practice to do it properly, all lower-case: <option></option>.
Dec 14 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
actually, the standard for HTML 4 is uppercase element names and lowercase attribute names. but personally, I'd stick to all being lowercase (easier to read).

besides that the closing option tag is optional, but it's always a good idea to close elements properly (that can be closed).
Dec 14 '11 #6

P: 33
hi
thanks for the replies.

I tried above things still the same issue. A blank output.
Is there any way to check what is the 'brand' value in php. I mean something like alert box in jscript. So that i can figure out if the 'brand' value is actually passed from jscript to php.

Thanks
Dec 15 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
that's called var_dump()
Dec 15 '11 #8

Post your reply

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