943,682 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 712
  • PHP RSS
Dec 22nd, 2008
0

Ajax Problem

Expand Post »
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..

php Syntax (Toggle Plain Text)
  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....

php Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
nil_gh_80 is offline Offline
64 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: A little problem with a script
Next Thread in PHP Forum Timeline: subcategory problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC