csv file, ajax, php

Reply

Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

csv file, ajax, php

 
0
  #1
Jul 21st, 2009
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:
  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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 47
Reputation: Banderson is an unknown quantity at this point 
Solved Threads: 4
Banderson's Avatar
Banderson Banderson is offline Offline
Light Poster

Re: csv file, ajax, php

 
0
  #2
Jul 22nd, 2009
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

  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

  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)


  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
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC