Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/219086-please-can-someone-help/
Share on other sites

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

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)?

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]

$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

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>";

}

 

 

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

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

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!

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

 

 

 

 

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>

 

 

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'];
?>

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]

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:

 

 

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'].'">';
?>

 

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.