robbaust Posted November 18, 2010 Share Posted November 18, 2010 Hi new to PHPfreaks and I'm sorry if this as been asked before but I'm having a problem basically my question is how would i get ajax live search to return results from a mysql database I have an example here www.spinfish.co.uk/test.html if you type in say salmon it shows the title but if you click on any of the titles shown it just doesn't do anything, what I want is for a user to be able to click the title then get the description from the database so say a user clicks the first title it comes up with a description of that river this is my code that I am using and the html file <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $partialFishing = $_GET['partialfish']; $fishing = mysql_query("SELECT title FROM fishing WHERE title LIKE '%$partialFishing%'"); while ($fish = mysql_fetch_assoc($fishing)){ echo "<div>"."<a href='title'>".$fish['title']."</a>"."</div>"; } ?> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script type="text/javascript"> function getfishing(value) { $.get("getfishing.php",{partialfish:value},function(data){ $("#results").html(data); }); } </script> </head> <body> <input name="search" onKeyUp="getfishing(this.value)" id="search" size="35" /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> <br> <div id="results"></div> </body> </html> Thank you so much in advance for any help you can give Rob Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/ Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 Your ajax seems to be working fine. The problem lies here: echo "<div>"."<a href='title'>".$fish['title']."</a>"."</div>"; href="title" will redirect users to a non-existent file named "title". you will want to do one of the following: 1. intercept the click on the link and show the description by more AJAX. i.e. <a href="..." onclick="getDescription('titlehere') 2. create another page so to display description with a parameter passed i.e. <a href="description.php?title=titlehere" Note: it would be preferable to retrieve and ID column together with the title so that the correct description can be retrieved Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136461 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 Hi Seanlim Thank you for your reply it's really appreciated Sorry to ask this but where whould i put it as i forgot to say i'm new to php as well but slowly getting the hand of it Thank you again for the help Rob Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136546 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 you've got quite a long way to go... i can't really help you much further without more details What are the columns in your table? Are the Descriptions stored in the same table? Do you have an ID column? How do you want the results displayed? On the same page as the results shown (AJAX) or on a separate page (PHP)? Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136709 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 Hi seanlim the columns in the table are id, title, description, keywords, url and images but i don't need the keywords and yes the descriptions are in the same table i would like the results to display on the same page if possible but if not a separate page is fine [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136742 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 $fishing = mysql_query("SELECT id, title FROM fishing WHERE title LIKE '%$partialFishing%'"); while ($fish = mysql_fetch_assoc($fishing)){ echo "<div>"."<a href='description.php?id=".$fish['id']."'>".$fish['title']."</a>"."</div>"; } description.php <?php if(!isset($_POST['id'])) exit("No ID set"); // add mysql connection stuff here $fishing = mysql_query("SELECT description FROM fishing WHERE id='".mysql_real_escape_string(intval($_POST['id']))."'"); if(mysql_num_rows($fishing)==0) exit("Record doesn't exist"); $fish = mysql_fetch_assoc($fishing); echo $fish['description']; ?> that's the jist of it. if you want it on the same page, use ajax to display the same information output-ed by description.php Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136758 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 thank you seanlim where do i out this first part $fishing = mysql_query("SELECT id, title FROM fishing WHERE title LIKE '%$partialFishing%'"); while ($fish = mysql_fetch_assoc($fishing)){ echo "<div>"."<a href='description.php?id=".$fish['id']."'>".$fish['title']."</a>"."</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136767 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 replace the last 4 lines in your original PHP file with that 4 lines of code Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136773 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks done that but now just getting "no ID set" really sorry about all this Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136775 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 what is the URL shown when you get the message "No ID set"? work on the copy online if possible, easier to debug remotely Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136784 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 http://localhost/description.php?id= Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136787 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 upload whatever you have done. do all your records have a valid id? in the lines that you replaced: while ($fish = mysql_fetch_assoc($fishing)){ // add this next line var_dump($fish); echo "<div>"."<a href='description.php?id=".$fish['id']."'>".$fish['title']."</a>"."</div>"; } then try going to http://localhost/getfishing.php?partialfish=SALMON%20River%20Dee,%20Ballogie%20Estates,%20Aberdeenshire, view source and paste everything here Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136800 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 this is what the sorce code is saying array(1) { ["title"]=> string(50) "SALMON River Dee, Ballogie Estates, Aberdeenshire " } <div><a href='description.php?id='>SALMON River Dee, Ballogie Estates, Aberdeenshire </a></div> and yes every record as an id Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136804 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 geez... you didn't change the code right? in the four lines of code i posted, the MySQL query said "SELECT id, title FROM....". did you copy those over? if you're not sure, copy those four lines of code into your PHP file (getfishing.php) again! Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136808 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 sorry yes i copied the code as you pointed out this is all i'm getting array(2) { ["id"]=> string(1) "1" ["title"]=> string(50) "SALMON River Dee, Ballogie Estates, Aberdeenshire " } <div><a href='description.php?id=1'>SALMON River Dee, Ballogie Estates, Aberdeenshire </a></div> really sorry for putting on you like this Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136820 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 Seanlim these are the files i have getfishing.php <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $partialFishing = $_GET['partialfish']; $fishing = mysql_query("SELECT id, title FROM fishing WHERE title LIKE '%$partialFishing%'"); while ($fish = mysql_fetch_assoc($fishing)){ // add this next line var_dump($fish); echo "<div>"."<a href='description.php?id=".$fish['id']."'>".$fish['title']."</a>"."</div>"; } ?> the description.php you gave me and the index.html <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> <script type="text/javascript"> function getfishing(value) { $.get("getfishing.php",{partialfish:value},function(data){ $("#results").html(data); }); } </script> </head> <body> <input name="search" onKeyUp="getfishing(this.value)" id="search" size="35" /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> <br> <div id="results"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136823 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 okay, now remove // add this next line var_dump($fish); it should be working, no? any error messages? Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136835 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 i have uploaded the files to my website www.spinfish.co.uk/test.html Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136849 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 my bad, replace description.php with this: <?php if(!isset($_GET['id'])) exit("No ID set"); // add mysql connection stuff here $fishing = mysql_query("SELECT description FROM fishing WHERE id='".mysql_real_escape_string(intval($_GET['id']))."'"); if(mysql_num_rows($fishing)==0) exit("Record doesn't exist"); $fish = mysql_fetch_assoc($fishing); echo $fish['description']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136854 Share on other sites More sharing options...
robbaust Posted November 19, 2010 Author Share Posted November 19, 2010 WORKING Thank you so much Seanlim I really really appreciate what you have done Thank You Robert Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136859 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 no problem, glad i could help and remember to mark this topic as solved Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1136865 Share on other sites More sharing options...
robbaust Posted November 20, 2010 Author Share Posted November 20, 2010 hi seanlim sorry i have kind of lost the plot today as stated in my PM to you how would i get images to show the images are coming from a folder on my server but can't seem to get them to show if you look at my site www.spinfish.co.uk/test.html it just shows the file location or an broken image icon (firefox) description.php <?php if(!isset($_GET['id'])) exit("No ID set"); mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $fishing = mysql_query("SELECT title, description, images FROM fishing WHERE id='".mysql_real_escape_string(intval($_GET['id']))."'"); $images = mysql_query("SELECT images FROM fishing WHERE id='".mysql_real_escape_string(intval($_GET['id']))."'"); if(mysql_num_rows($fishing)==0) exit("Record doesn't exist"); $fish = mysql_fetch_assoc($fishing); echo $fish['title'] . "<BR/>\n"; echo $fish['description'] . "<BR/>\n" . "<BR/>\n"; echo $fish['images']; echo '<img src=/images/'; ?> getfishing.php <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $partialFishing = $_GET['partialfish']; $fishing = mysql_query("SELECT id, title, images FROM fishing WHERE title LIKE '%$partialFishing%'"); while ($fish = mysql_fetch_assoc($fishing)){ echo "<div>"."<a href='description.php?id=".$fish['id']."'>".$fish['title']."</a>"."</div>"; } ?> index.html <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> <script type="text/javascript"> function getfishing(value) { $.get("getfishing.php",{partialfish:value},function(data){ $("#results").html(data); }); } </script> </head> <body> <input name="search" onKeyUp="getfishing(this.value)" id="search" size="35" /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> <br> <div id="results"></div> </body> </html> and this is my database [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1137184 Share on other sites More sharing options...
seanlim Posted November 20, 2010 Share Posted November 20, 2010 The only problem is with description.php now. When you click the link, it takes you to description.php?id=[id-here], which is perfectly good. However, the code I gave only shows the raw, unformatted database data. The page should be a proper HTML page to display correctly i.e. proper <html> & <body> tags which you are missing. Furthermore, you can do a simple View Source to see that your image tag is not showing properly: http://www.spinfish.co.uk/images/066.jpg<img src=/images/ Your <img> tag must have a "src" attribute pointing to a proper file for it to be displayed. The last line of description.php: echo '<img src="/images/'.$fish['images'].'">'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1137190 Share on other sites More sharing options...
robbaust Posted November 20, 2010 Author Share Posted November 20, 2010 thanks seanlim I think i have it working www.spinfish.co.uk/test.html Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1137201 Share on other sites More sharing options...
robbaust Posted November 21, 2010 Author Share Posted November 21, 2010 Hi sorry my i ask two final questions please if a person as typed something in the search box then backspaces how would i get my script to stop showing all the results until they start typing in again and do you know when you type something in a box and the popup box thing shows that as something you typed in before how can you stop that I have uploaded a picture to show what i mean thanks for your time again [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/219086-please-can-someone-help/#findComment-1137422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.