Jump to content

AJAX - How do I display links that I can click


timothyarden

Recommended Posts

Hi Guys,

 

In this script PHP and AJAX are used to fetch results from a array and display them according to which letters are put in. I was wondering how I could change the results from just being names to links which would then when clicked link to a user profile.

 

Here the script

<html>
<head>
	<title>AJAX Suggest</title>
	<script type="text/javascript">
		function showHint(str)
			{
				if (str.length==0)
					{
						document.getElementById("txtHint").innerHTML="";
						return;
					}

				if (window.XMLHttpRequest)
					{
						xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
					}

				else
					{
						xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
					}

				xmlhttp.onreadystatechange=function()
					{
						if (xmlhttp.readyState==4 && xmlhttp.status==200)
							{
								document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
							}
					}

				xmlhttp.open("GET","gethint.php?q="+str,true);
				xmlhttp.send();
			}
	</script>
</head>
<body bgcolor="black">
	<p style="color:blue"><b>Start typing a name in the input field below:</b></p>
	<table>
		<form name="suggestform">
			<tr>
				<td width="115px">
					<label style="color:blue" for="firstnameinput">First name:</label>
				</td>
				<td>
					<input type="text" onkeyup="showHint(this.value)" size="20" name="firstnameinput"/>
				</td>
			</tr>
			<tr>
				<td>
					<p style="color:blue">Suggestions:</p>
				</td>
				<td>
					<p style="color:blue"><span id="txtHint"></span></p>
				</td>
		</form>
	</table>
</body>
</html>

 

<?php
// Fill up array with names
$a[]="Anna";
$a[]="Abigail";
$a[]="Anthony";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

//get the q parameter from URL
$q = $_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
	{
		$hint="";
		for($i=0; $i<count($a); $i++)
			{
				if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
					{
						if ($hint=="")
							{
								$hint=$a[$i];
							}

						else
							{
								$hint=$hint." , ".$a[$i];
							}
					}
			}
	}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
	{
		$response="no suggestion";
	}

else
	{
		$response=$hint;
	}

//output the response
echo $response;
?>

 

Thanks for any help,

Timothy

Link to comment
Share on other sites

What I mean is if I put <a href="youtube.com">And in between here use the AJAX Loaded content</a> how would I change the hyperrefence to be according to what is brought up - example if I entered d and it then loaded david from the array - how would I make david a link to davids page instead of youtube. How would I make the link change according to the what is loaded?

 

Thanks for your help

Timothy

Link to comment
Share on other sites

you can change the value of the href with .attr('href') (in jQ).

 

How I exchange from PHP to JQ is something like this:

<?php
// prep response
$response = array();

// request from DB
$My_YT_Link = $database extraction method;

// results exist
if( $My_YT_Link ) {
    // create return array
    $response = array(
        'status' =>  TRUE,
        'link'  =>  $My_YT_Link->youtube_id,
        'title'  =>  $My_TY_Link->youtube_title,
        );
} else {
    // db request failed
    $response = array(
        'status'  => FALSE;
    );
}

// return in JSON
return json_encode($response);

 

The appropriate array will be returned to your JS ajax request and you can handle it accordingly.

Link to comment
Share on other sites

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.