943,749 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1108
  • PHP RSS
Jul 21st, 2009
0

csv file, ajax, php

Expand Post »
i'm parsing a csv file with ajax and php, although i think i did everything right, nothing happens when i click on the link.

here is the code:
PHP Syntax (Toggle Plain Text)
  1. here is the html:
  2. <div id="brandContainer">
  3. <ul id="brandNav">
  4. <li class="categorypage" id="face"><a href="javascript:showCD('a','brandPage')">A</a></li>
  5. </ul>
  6. <div id="brandPage"></div>
  7. </div>
  8.  
  9. here is the ajax/javascript:
  10.  
  11. var xmlhttp
  12. function showCD(str, div)
  13. {
  14. xmlhttp=GetXmlHttpObject();
  15. if (xmlhttp==null)
  16. {
  17. alert ("Your browser does not support AJAX!");
  18. return;
  19. }
  20. var url="getbrands.php";
  21. url=url+"?q="+str;
  22. url=url+"&sid="+Math.random();
  23. xmlhttp.onreadystatechange=stateChanged(div);
  24. xmlhttp.open("GET",url,true);
  25. xmlhttp.send(null);
  26. }
  27.  
  28. function stateChanged(div)
  29. {
  30. if (xmlhttp.readyState==4)
  31. {
  32. document.getElementById(div).innerHTML=xmlhttp.responseText;
  33. }
  34. }
  35.  
  36. function GetXmlHttpObject()
  37. {
  38. if (window.XMLHttpRequest)
  39. {
  40. // code for IE7+, Firefox, Chrome, Opera, Safari
  41. return new XMLHttpRequest();
  42. }
  43. if (window.ActiveXObject)
  44. {
  45. // code for IE6, IE5
  46. return new ActiveXObject("Microsoft.XMLHTTP");
  47. }
  48. return null;
  49. }
  50.  
  51.  
  52.  
  53. and here is the php(getbrands.php):
  54.  
  55. <?php
  56.  
  57. $q=$_GET["q"];
  58.  
  59. $handle = fopen("http://www.totalbeauty.com/resource/lists", "r");
  60. while (($data = fgetcsv($handle, ";;")) !== FALSE) {
  61. $brands= $data[0];
  62. }
  63. $brand = explode(",", $brands);
  64. for ($i=0; $i<=count($brand); $i++){
  65.  
  66. $a=strtolower($brand[$i]);
  67. $display = "<ul>";
  68. if ($q = $a[0]){
  69. $display .= "<li>'".$a."'</li>";
  70. }
  71. $display .="</ul>";
  72. echo $display;
  73. }
  74.  
  75.  
  76. ?>


can anyone please tell me what is wrong with this code?
thanks...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Jul 22nd, 2009
0

Re: csv file, ajax, php

If you get an error make sure it is not an access denied. An access denied is pretty common if your on a windows server. Because your requesting xmlhttp.open("GET",url,true); and windows is funny about those things.

Try this:

here is the ajax/javascript

PHP Syntax (Toggle Plain Text)
  1. var xmlhttp;
  2. function showCD(str, div)
  3. {
  4.  
  5. xmlhttp=GetXmlHttpObject();
  6. if (xmlhttp==null)
  7. {
  8. alert ("Your browser does not support AJAX!");
  9. return;
  10. }
  11. var url="getbrands.php";
  12. url=url+"?q="+str;
  13. url=url+"&sid="+Math.random();
  14. xmlhttp.onreadystatechange=stateChanged;
  15. xmlhttp.open("GET",url,true);
  16. xmlhttp.send(null);
  17. }
  18.  
  19. function stateChanged(div)
  20. {
  21. if (xmlhttp.readyState==4)
  22. {
  23. document.getElementById(div).innerHTML=xmlhttp.responseText;
  24. }
  25. }
  26.  
  27. function GetXmlHttpObject()
  28. {
  29. if (window.XMLHttpRequest)
  30. {
  31. // code for IE7+, Firefox, Chrome, Opera, Safari
  32. return new XMLHttpRequest();
  33. }
  34. if (window.ActiveXObject)
  35. {
  36. // code for IE6, IE5
  37. return new ActiveXObject("Microsoft.XMLHTTP");
  38. }
  39. return null;
  40. }


here is the html

PHP Syntax (Toggle Plain Text)
  1. <div id="brandContainer">
  2. <ul id="brandNav">
  3. <li class="categorypage" id="face"><a href="javascript:showCD
  4.  
  5. ('a','brandPage')">A</a></li>
  6. </ul>
  7. <div id="brandPage"></div>
  8. </div>


and here is the php(getbrands.php)


PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $q=$_GET["q"];
  4.  
  5. $handle = fopen("http://www.totalbeauty.com/resource/lists", "r");
  6. while (($data = fgetcsv($handle, ";;")) !== FALSE) {
  7. $brands= $data[0];
  8. }
  9. $brand = explode(",", $brands);
  10. for ($i=0; $i<=count($brand); $i++){
  11.  
  12. $a=strtolower($brand[$i]);
  13. $display = "<ul>";
  14. if ($q = $a[0]){
  15. $display .= "<li>'".$a."'</li>";
  16. }
  17. $display .="</ul>";
  18. echo $display;
  19. }
  20.  
  21.  
  22. ?>
Last edited by Banderson; Jul 22nd, 2009 at 5:44 am. Reason: corrected error
Reputation Points: 17
Solved Threads: 8
Junior Poster in Training
Banderson is offline Offline
66 posts
since Aug 2004

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: show the file icon
Next Thread in PHP Forum Timeline: Search for an exact match





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


Follow us on Twitter


© 2011 DaniWeb® LLC