Ajax Problem

Reply

Join Date: Aug 2007
Posts: 64
Reputation: nil_gh_80 is an unknown quantity at this point 
Solved Threads: 0
nil_gh_80's Avatar
nil_gh_80 nil_gh_80 is offline Offline
Junior Poster in Training

Ajax Problem

 
0
  #1
Dec 22nd, 2008
Dear members,

I'm facing a problem using ajax..I ve two files language.php and ajax-language.php. In language.php I have a list of languages with checkbox where if i click a checkbox then a male female option will be shown. now the problem is when i'm going to click gender option then it shows nothing(in firefox error console it says "document.form1.sex is undefined")....can anybody plzzzzz take care to solve this isssue.

language.php..

  1. <?php
  2. echo $_REQUEST['langId'];
  3. ?>
  4.  
  5.  
  6. <html>
  7. <head>
  8. <script language="javascript" type="text/javascript">
  9. <!--
  10. //Browser Support Code
  11. function ajaxFunction(){
  12. var ajaxRequest; // The variable that makes Ajax possible!
  13.  
  14. try{
  15. // Opera 8.0+, Firefox, Safari
  16. ajaxRequest = new XMLHttpRequest();
  17. } catch (e){
  18. // Internet Explorer Browsers
  19. try{
  20. ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  21. } catch (e) {
  22. try{
  23. ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  24. } catch (e){
  25. // Something went wrong
  26. alert("Your browser broke!");
  27. return false;
  28. }
  29. }
  30. }
  31. // Create a function that will receive data sent from the server
  32. ajaxRequest.onreadystatechange = function(){
  33. if(ajaxRequest.readyState == 4){
  34.  
  35. // To display gender options
  36. if(document.form1.scripts.length > 0){
  37. var ajaxGenderDisplay = document.getElementById('gender');
  38. ajaxGenderDisplay.innerHTML = ajaxRequest.responseText;
  39. }
  40. // TO display voice options
  41. if(document.form1.scripts.length > 0 && document.form1.sex.length > 0){
  42. var ajaxVoiceDisplay = document.getElementById('voice');
  43. ajaxVoiceDisplay.innerHTML = ajaxRequest.responseText;
  44. }
  45.  
  46.  
  47. }
  48. }
  49.  
  50. var total=""
  51.  
  52. for(var i=0; i < document.form1.scripts.length; i++){
  53. if(document.form1.scripts[i].checked){
  54. //total +=document.form1.scripts[i].value + ",";
  55. if(document.form1.sex.length == 0){
  56. total +=document.form1.scripts[i].value + ",";
  57. }else{
  58. for(var i=0; i < document.form1.sex.length; i++){
  59. if(document.form1.sex[i].checked){
  60. total +=document.form1.scripts[i].value + "sex" + document.form1.sex[i].value + ",";
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. //alert(total);
  68. var queryString = "?language=" + total ;
  69. ajaxRequest.open("GET", "ajax-language.php" + queryString, true);
  70. ajaxRequest.send(null);
  71. }
  72.  
  73. //-->
  74. </script>
  75.  
  76.  
  77. </head>
  78.  
  79. <body>
  80. <table border='0' width='50%' cellspacing='0' cellpadding='0' >
  81. <tr>
  82. <th>Language</th>
  83. <th>Gender</th>
  84. <th>Voice</th>
  85. <th>Quotation</th>
  86. <form name="form1" method="post">
  87. <tr>
  88. <td align=left valign="top">
  89. <input type="checkbox" name="scripts" value='JavaScript' onChange="ajaxFunction();">JavaScript<br>
  90. <input type="checkbox" name="scripts" value='PHP' onChange="ajaxFunction();">PHP <br>
  91. <input type="checkbox" name="scripts" value='HTML' onChange="ajaxFunction();">HTML <br>
  92. </td>
  93. <td valign="top">
  94. <div id="gender"></div>
  95. </td>
  96. <td valign="top">
  97. <div id="voice"></div>
  98. </td>
  99. <td valign="top">
  100. <div id="quotation"></div>
  101. </td>
  102. </tr>
  103.  
  104. </form>
  105. </table>
  106. </body>
  107. </html>


ajax-example.php....

  1. <?php
  2. $language = $_REQUEST['language'];
  3. //echo $language;
  4.  
  5. $language = explode(',', $language);
  6. foreach($language as $val){
  7. if($val !=''){
  8. $disp_opt = "<b>".$val."</b><br>";
  9. $disp_opt .= "<input type=\"checkbox\" name=\"sex\" value=\"m\" onChange=\"ajaxFunction();\">Male &nbsp;";
  10. $disp_opt .= "<input type=\"checkbox\" name=\"sex\" value=\"f\" onChange=\"ajaxFunction();\">Female &nbsp;<br>";
  11. echo $disp_opt;
  12. }
  13. }
  14.  
  15.  
  16. ?>
Last edited by peter_budo; Dec 23rd, 2008 at 6:58 pm. Reason: Correcting code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC