heshan Posted April 7, 2012 Share Posted April 7, 2012 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. 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> Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/ Share on other sites More sharing options...
sunfighter Posted April 7, 2012 Share Posted April 7, 2012 This is not done in the php that reads the submitted form, but rather with AJAX when the Admission Number is entered and you leave the field 'onblur'; Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/#findComment-1335200 Share on other sites More sharing options...
heshan Posted April 7, 2012 Author Share Posted April 7, 2012 It's ok. Could you pls give me the AJAX code ? This is how my form looks like. <form name="form1" method="post" action="newStudentRegistrationFormvalidation.php"> <table> <tr> <td width="20"> </td> <td colspan="4"><h3> <label><strong>Add Student Information</strong></label> <strong> </strong></h3></td> <td width="1"> </td> <td width="1"> </td> <td width="6"> </td> </tr> <tr> <td> </td> <td width="261"><label>Admission Number </label> </td> <td colspan="2"> <input type="text" name="admission_no" id="textfield" /></td> <td width="127"> </td> <td> </td> <td> </td> </tr> -------- ------- </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/#findComment-1335208 Share on other sites More sharing options...
heshan Posted April 9, 2012 Author Share Posted April 9, 2012 Hi all, Pls help me to solve this issue... I have tried below coding. It seems to be fulfilling my above purposes. But the message comes soon after i enter an admission number. (irrespective of it exists or not) newStudentRegistrationForm.php <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","newStudentRegistrationFormValidation.php?q="+str,true); xmlhttp.send(); } </script> HTML part, <tr> <td> </td> <td width="261"><label>Admission Number </label> </td> <td colspan="2"> <input type="text" name="admission_no" id="textfield" onkeyup="showReg(this.value)"/></td> <div id="regresult"></div> <td width="127"> </td> <td> </td> <td> </td> </tr> 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{ ?> <?php $reg = $_GET['q']; echo $reg; }?> student registrationDatabase.php <?PHP $admission_no=$_GET['admission_no']; $admission_date=$_GET['admission_date']; $full_name=$_GET['full_name']; $name_with_initials=$_GET['name_with_initial']; $dob=$_GET['date_of_birth']; $religion=$_GET['religion']; //$gender=$_GET['gender']; $address=$_GET['address']; $telephone=$_GET['telephone']; $grade_on_admission=$_GET['grade_on_admission']; $present_grade=$_GET['grade_ID']; $stream=$_GET['stream_ID']; $present_class=$_GET['class_ID']; $student_house=$_GET['student_house']; $password=$_GET['password']; $description_about_st=$_GET['description_about_st']; $payment=$_GET["payment"]; $current=getdate(time()); $year=$current["year"]; $today = date("Y-m-d"); $con=mysql_connect("localhost","root",""); mysql_select_db("student_management",$con); //insert to database $query="SELECT admission_no FROM student_info WHERE student_info.admission_no='$admission_no'"; $result=mysql_query($query); $num_rows = mysql_num_rows($result); if($num_rows > 0) { echo "Admission number".$admission_no. " This student has been already entered to the system"."<BR>"."<BR>"."<BR>"; echo "<a href='newStudentRegistrationForm.php'>GO to registration page</a>"; } else { // admission no not exist $query="INSERT INTO student_info (st_id,admission_no,admission_date,full_name,name_with_initial,date_of_birth,religion,address,telephone_no,grade_on_admission, password,st_house,description_about_st) VALUES(null,'$admission_no','$admission_date','$full_name','$name_with_initials','$dob','$religion','$address','$telephone','$grade_on_admission','$password','$student_house','$description_about_st')"; $result=mysql_query($query) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/#findComment-1335808 Share on other sites More sharing options...
sunfighter Posted April 10, 2012 Share Posted April 10, 2012 A couple of things 1.) Do not use <input type="text" name="admission_no" id="textfield" onkeyup ...... This will execute as soon as the first number is entered. Use onblur. This occurs when you leave the text field and that should be when the complete number has been entered. 2.) Your code to establish XMLHttpRequest is wrong. I just re=wrote it. 3.) Don't send the Admission Number verification number to the same php file that you want executed upon submission. This is your code for the Admission Number HTML: <input type="text" name="admission_no" id="textfield" onblur="showReg('number')"/></td> I am sending a code word called 'number' to the function. When you write your HTML make sure the SUBMIT button sends a different code word!!! This is the javascript that sets up the XMLHttpRequest and the ajax. <script type="text/javascript"> function showReg(str) { var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ if (str == 'number') document.getElementById('regresult').innerHTML = ajaxRequest.responseText; } } Please notice that the js is not complete and that I choose where the responseText goes depending upon the code word 'number'. We end the script with a section that also depends on the code word. Add this after the above code: if (str == 'number'){ var words = document.getElementById('textfield').value; ajaxRequest.open("GET", "ajax.php?Admit="+words, true); ajaxRequest.send(''); } } // this ends the function </script> Write another section based on the last section to execute when the submit button is pressed. Don't forget to do the conditional statement for the responseText in the beginning js section. The php file I wrote is called ajax.php - use your own name. It looks like this: <?php // Connect to your database here $admission_no = $_GET["Admit"]; // sent with this ajaxRequest.open("GET", "ajax.php?Admit="+words, true); $query="SELECT admission_no FROM student_info Where student_info.admission_no='$admission_no'"; $result = mysql_query($query) ; if(mysql_num_rows($result) > '0') echo 'This Number has been taken'; ?> That should do it. Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/#findComment-1335900 Share on other sites More sharing options...
heshan Posted April 10, 2012 Author Share Posted April 10, 2012 Hi sunfighter, Thanks for your comments...But i have doubt. I want to display the message "Existing Admission Number" in the same page where the registration form exists. Then only the user knows this is existing admission number rather than having to type all the fields in the form and then display the message. In nut shell the message should appear as soon after the user fills the admission number field..... But the coding given by you also gives the message in a different page..... I have done the changes in my registration form as well as in the validation page....... Quote Link to comment https://forums.phpfreaks.com/topic/260506-issue-regarding-a-form-validation/#findComment-1336016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.