Hey guys, am learning php/mysql and am trying to learn how to do the following. I have a database, and a form with 3 fields. I want fields 2 and 3 to be filled with data from mysql table immeadiately after field 1 receives a value. I think I am supposed to use the onfocus event on the 2nd field, and then add a query. I have tried different stuff but am not doing it right. Anyone have any examples of how to do this? I'd appreciate the help.

The form is below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>main page...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form name="form1" action="">
Student Id: <input type="text" name="adm_no" id="adm_no" value="" />
<br />
Student Name: <input name="surname" type="text" id="surname" />
<br />
Class: <input type="text" name="class" id="class" value="" />
<br />
</form>

</body>
</html>

Recommended Answers

All 3 Replies

yes... you should use in onfoucus in class id ... I prefer for jquery ... it's easier than javascript for me... now I am written with javascript ....

<html>
<head>
<title>main page...</title>

<script>
function chkval()
{
	if((document.getElementById("adm_no").value!="") && (document.getElementById("surname").value!=""))
	{
		//php process here
		document.getElementById("class").value="Working";
	}
	
}
</script>
</head>
<body>
<form name="form1" action="">
Student Id: <input type="text" name="adm_no" id="adm_no" value="" />
<br />
Student Name: <input name="surname" type="text" id="surname"  />
<br />
Class: <input type="text" name="class" id="class" value="" onfocus="chkval()" />
<br />
</form>

</body>
</html>

You will need to use ajax to do this. There are many examples on the internet, google it.

Thanks for the feedback. In your example saturn, the user enters an id, then when the field loses focus the fields for student name and class are filled with their corresponding values from the database. In which field is the onfocus event supposed to be placed? in the id field or name or class?

KKeith, I cant get any examples that can help me understand this better. Please provide me with some.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.