I want to submit data of my student registration form to the database. If an admission number already exists, a message should be displayed like this.

"Admission number2012/1..This student has been already entered to the system."

I have created the below coding which fulfill the above task. But i want slight modification, rather than displaying the message in a seperate field after entering all the fields i want the message to display soon after entering the "Admission Number" in my form.

Can anyone give me the relevant AJAX code to solve this issue?

newStudentRegistrationFormValidation.php

<?php
$admission_no=$_POST['admission_no'];
$admission_date=$_POST['admission_date'];
$full_name=$_POST['full_name'];
$name_with_initial=$_POST['name_with_initial'];
$date_of_birth=$_POST['date_of_birth'];
$religion=$_POST['religion'];
//$gender=$_POST['gender'];
$address=$_POST['address'];
$telephone=$_POST['telephone'];
$grade_on_admission=$_POST['grade_on_admission'];
$grade_ID=$_POST['grade_ID'];
$stream_ID=isset($_POST['stream_ID']);
$class_ID=$_POST['class_ID'];
$student_house=$_POST['student_house'];
$password=$_POST['password'];
$description_about_st=$_POST['description_about_st'];
$payment=$_POST["payment"];

$currentdate=getdate(time());
$year=$currentdate["year"];

//admission number validation

$con=mysql_connect("localhost","root","");
mysql_select_db("student_management",$con);

$query="SELECT admission_no FROM student_info Where student_info.admission_no='$admission_no'";
$result=mysql_query($query) ;
$num_rows = mysql_num_rows($result);
echo $num_rows;

if($num_rows >= 0)
{

header("location:student registrationDatabase.php?admission_no=".urlencode($admission_no)."&year=".urlencode($year)."&admission_date=".urlencode($admission_date)."&full_name=".urlencode($full_name)."&name_with_initial=".urlencode($name_with_initial)."&date_of_birth=".urlencode($date_of_birth)."&religion=".urlencode($religion)."&address=".urlencode($address)."&telephone=".urlencode($telephone)."&grade_on_admission=".urlencode($grade_on_admission)."&grade_ID=".urlencode($grade_ID)."&stream_ID=".urlencode($stream_ID)."&class_ID=".urlencode($class_ID)."&student_house=".urlencode($student_house)."&password=".urlencode($password)."&description_about_st=".urlencode($description_about_st)."&payment=".urlencode($payment));
exit();

}else{

?>
<body>
<?php
echo "Admission number".$admission_no." .This student has been already entered to the system ."."<BR>"."<BR>"."<BR>";
echo "<a href='newStudentRegistrationForm.php'>GO to manage student details page</a> ";
exit();
}?>
</body>
</html>
Inline Code Example Here

This is how my form looks like.

<form name="form1" method="post" action="newStudentRegistrationFormvalidation.php">
<table>
      <tr>
        <td width="20">&nbsp;</td>
        <td colspan="4"><h3>
          <label><strong>Add Student Information</strong></label>
          <strong>&nbsp;</strong></h3></td>
        <td width="1">&nbsp;</td>
        <td width="1">&nbsp;</td>
        <td width="6">&nbsp;</td>
      </tr>

      <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        <td width="261"><label>Admission Number </label>&nbsp;</td>
        <td colspan="2">

        <input type="text" name="admission_no" id="textfield" /></td>
        <td width="127">&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
--------
-------
</table>
</form>

It would be easier to achieve this using AJAX.

<script type="text/javascript">
function showReg(str) {
    var xmlhttp;    
    if (str=="") {
        document.getElementById("regresult").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("regresult").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","getregistrationnum.php?q="+str,true);
    xmlhttp.send();
}
</script>

//HTML

<input type="text" name="admission_no" id="textfield" onkeyup="showReg(this.value)" />
<div id="regresult"></div>

//PHP getregistrationnum.php

<?php
$reg = $_GET['q'];
echo $reg;
?>

this is to get you started. just use the if($num_rows >= 1) { do something }

I have inserted your code to my newStudentRegistrationFormValidation.php and i have also created new page called** getregistrationnum.php**

But this is not working. What are the other changes i need to made?

i would suggest running my script as a test page seperately from your website, so you can understand the basics of AJAX, that way you can impliment your code into your php page. Here is a link where you can read on how AJAX works
W3SCHOOLS

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.