Jump to content

small w3schools ajax example help


roshanjonah

Recommended Posts

Hi

 

I am new to php and have built this simple ajax search example provided by w3schools at http://www.w3schools.com/php/php_ajax_livesearch.asp

 

I am just wondering what would be the code i need to edit in javascript and the php to connect to a database and retrieve the same results.

 

Could anyone provide me with some small modifications to convert the xml file into a mysql database retrieving system.

 

Thank You so much. Your replies would be appreciated.

 

Roshan

Link to comment
https://forums.phpfreaks.com/topic/142327-small-w3schools-ajax-example-help/
Share on other sites

This might help...

 

On the main page in the head...

<script type='text/javascript' src='fileajax.js'></script>

 

On the page in the field...

<input type='text' name='FieldName' id='FieldName' value='$FieldName' onkeyup='TestFieldName(this.value)' />

<span id='fieldname_testing'></span>

 

Remember these values..

"FieldName" -> Input ID

"TestFieldName -> The function

"fieldname_testing" -> The span where a message or anything else can be displayed. NOTE: This does NOT have to be after the input. It can be before it. Also, the onkeyup() function can be almost any other event like onChange, etc.

 

The JS (fileajax.js)...

function TestFieldName(str)
{ 
xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)
{
	alert ("Browser does not support HTTP Request")
	return
} 

var url="fileajax.php"
url=url+"?variable_name="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=ReturnFunction 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function ReturnFunction() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
	document.getElementById("fieldname_testing").innerHTML=xmlHttp.responseText 
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;

try
{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
}

catch (e)
{
	//Internet Explorer
	try
	{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}

	catch (e)
	{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}

return xmlHttp;
}

 

Remember these...

"fileajax.php" -> The filename of the PhP to process

"variable_name" -> The variable passed/used in the PhP file

"ReturnFunction" -> The name of the next function that processes the information

"fieldname_testing" -> In the ReturnFunction. This is the ID of the area where the return information will be displayed

 

Leave the GetXmlHttpObject function alone.

 

The PhP file (fileajax.php)...

<?php
$con = mysql_connect('localhost', 'database_user, 'password');
mysql_select_db('database_name', $con);

$variable_name=$_POST[variable_name]; //Passed from the function

//Code to be processed
//Code to be processed
//Code to be processed
?>

This help?

wow

 

thank you so much. I am going to try this now and will inform you by tomorrow of any error or else i just don't have any word in my mouth.

 

Just wanna say thank you soooo much! :)

 

Will post 2moro of the results because i am going to sleep soon...hahaha

Hi

 

I just read the php file and i am quite dum in php as mentioned earlier. So what variables are they. Are these the ones in the example.

 

I want it to retrieve a result from website title table, url which is going to be attached to the title and tags which are only going to be searched but not actually displayed in the span.

 

so basically three tables. Any way you can accomodate the example, please. So same searching function, same everything except one extra table to just search from which is tags table but the results show display the same as show in the example http://www.w3schools.com/php/php_ajax_livesearch.asp

 

Thank You sooo much for you help. Higly appreciated!  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.