2017-01-18 8 views
0

Ich habe ein Registrierungsformular, das die eingegebenen Daten nach jeder Funktion überprüfen sollte, die ich erstellt habe (in der Datei validation.js) und nachdem alles in Ordnung ist, sollte es alle Daten an die zurückgeben .php-Datei, die in der phpmyadmin-Datenbank gespeichert wird.Formularvalidierung funktioniert nicht zusammen mit Formularaktion

aber es ist nicht

Test1 Arbeits: Validierung arbeitet (allein die action = .php enthalten) Test2: ohne Validierung werden die Daten in der Datenbank (phpMyAdmin) Speichern Test3: mit Validierung und mit action = .php) es funktioniert nicht die Validierung Teil

Bitte helfen Sie mir.

function formValidation() 
 
{ 
 
\t var id = document.registration.nic; 
 
\t var registerno = document.registration.regno; 
 
\t var name = document.registration.pname; 
 
\t var paddress = document.registration.address; 
 
    var maritial = document.registration.mars; 
 
    var religion = document.registration.rel; 
 
\t var addname = document.registration.fhn; 
 
    var pcity = document.registration.city; 
 
\t var telephone = document.registration.tel; 
 
\t var age = document.registration.ag; 
 
    var gender = document.registration.gen; 
 
    
 
\t 
 
\t 
 
\t \t 
 
\t 
 
\t if(ValidateNIC(id)) //done 
 
\t \t { 
 
\t \t if(allLettername(name)) //done 
 
\t \t \t { 
 
\t \t \t 
 
\t \t \t \t 
 
\t \t \t \t if(allnumericreg(registerno)) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t if(alphanumericpaddress(paddress)) //done 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t if(maritialselect(maritial)) //done 
 
\t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t if(religionselect(religion)) //done 
 
\t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t \t if(allLetteraddname(addname)) //done 
 
\t \t \t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t \t \t if(alphanumericpcity(pcity)) //done 
 
\t \t \t \t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t \t \t \t if(allnumerictel(telephone)) //done 
 
\t \t \t \t \t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t \t \t \t \t if(allnumericage(age)) //done 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t if(genderselect(gender)) //done 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t \t \t \t \t } 
 
    \t \t \t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t } \t 
 
\t \t \t \t \t \t \t } \t 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t \t 
 
\t \t \t \t \t 
 
\t \t \t } 
 
\t \t } 
 
\t \t return false; 
 
} 
 

 

 

 

 
function allLettername(name) 
 
{ 
 
    var letters = /^[A-Za-z ]+$/; 
 
    if(name.value.match(letters)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert('Patient Name must have alphabet characters only'); 
 
     return false; 
 
    } 
 
} 
 

 
function allLetteraddname(addname) 
 
{ 
 
    var letters = /^[A-Za-z ]+$/; 
 
    if(addname.value.match(letters)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert('Father or Husband"s Name must have alphabet characters only'); 
 
     return false; 
 
    } 
 
} 
 

 
function maritialselect(maritial) 
 
{ 
 
    if(maritial.value == "Default") 
 
    { 
 
     alert("Select your Maritial Status from the list"); 
 
     return false; 
 
    } 
 
    else 
 
    { 
 
     return true; 
 
    } 
 
} 
 

 
function ValidateNIC(id) 
 
    { 
 
     var letters = /^[0-9a-zA-Z ]+$/; 
 

 
    if ((id.value.length == 10) && id.value.match(letters)) 
 
\t \t { 
 
\t \t return true; 
 
\t \t } 
 
\t else 
 
\t \t { 
 
\t \t alert("NIC must contain alphanumeric characters only and must be 10 charators long!"); 
 
\t \t id.focus(); 
 
\t \t return false; 
 
\t \t } 
 
    } 
 
    
 

 
function alphanumericpaddress(paddress) 
 
{ 
 
    var letters = /^[0-9a-zA-Z ]+$/; 
 
    if(paddress.value.match(letters)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert('Patient address must have alphanumeric characters only'); 
 
     paddress.focus(); 
 
     return false; 
 
    } 
 
} 
 

 
function alphanumericpcity(pcity) 
 
{ 
 
    var letters = /^[0-9a-zA-Z ]+$/; 
 
    if(pcity.value.match(letters)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert('User City must have alphanumeric characters only'); 
 
     pcity.focus(); 
 
     return false; 
 
    } 
 
} 
 

 
function religionselect(religion) 
 
{ 
 
    if(religion.value == "Default") 
 
    { 
 
     alert("Select your Religion from the list"); 
 
     return false; 
 
    } 
 
    else 
 
    { 
 
     return true; 
 
    } 
 
} 
 

 
function allnumericreg(registerno) 
 
{ 
 
    var numbers = /^[0-9]+$/; 
 
    if(registerno.value.match(numbers)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert("Register No Must have numeric characters only"); 
 
     return false; 
 
    } 
 
} 
 

 
function allnumerictel(telephone) 
 
{ 
 
    var numbers = /^[0-9]+$/; 
 
    if((telephone.value.length == 10) && telephone.value.match(numbers)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert("Telephone Number must have numeric characters only and Length should 10 charators long! "); 
 
     return false; 
 
    } 
 
} 
 

 
function allnumericage(age) 
 
{ 
 
    var numbers = /^[0-9]+$/; 
 
    if(age.value.match(numbers)) 
 
    { 
 
     return true; 
 
    } 
 
    else 
 
    { 
 
     alert("Age must have numeric characters only"); 
 
     return false; 
 
    } 
 
} 
 

 
function genderselect(gender) 
 
{ 
 
    if(gender.value == "Default") 
 
    { 
 
     alert("Select your Gender from the list"); 
 
     return false; 
 
    } 
 
    
 
    else 
 
    { 
 
     alert('Registration Form Successfully Submitted'); 
 
     window.location.reload() 
 
     return true; 
 
\t } 
 
}
<?php 
 
$con= mysqli_connect ("localhost","root","","hmsjason"); 
 
if (mysqli_connect_error()){ 
 
echo"Failed to Connect"; 
 
} 
 
$sql="INSERT INTO patient(P_NIC,Reg_No,Full_Name,Address,Maritial_Sts,Religion,DOB,H_F_Name,City,Contact,Age,Gender,Room_No)VALUES('$_POST[nic]','$_POST[regno]','$_POST[pname]','$_POST[address]','$_POST[mars]','$_POST[rel]','$_POST[dob]','$_POST[fhn]','$_POST[city]','$_POST[tel]','$_POST[ag]','$_POST[gen]','$_POST[room]')"; 
 
if(!mysqli_query($con,$sql)){ 
 
die("Error".mysqli_error($con)); 
 
} 
 
mysqli_close($con); 
 
echo "<script type='text/javascript'>alert('submitted successfully!')</script>"; 
 
header("location:reception.html"); 
 

 

 
?>
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title>Registration</title> 
 
<link rel="stylesheet" type="text/css" href="style1.css"> 
 
<script src="regvalidation.js"></script> 
 
</head> 
 
<body> 
 
<br><br><br> 
 

 
<table background="Images/formbg1.jpg" border="2" align="center" width ="825" height="820" > 
 

 
<th align=center valign=center> 
 

 
<form action="patientdetails.php" method="POST"/> 
 
<!--form name='registration' onSubmit="return formValidation()"/>--> 
 
<div class="textcenter1"> 
 
\t <h2>Patient Registration Form</h2> 
 
</div> 
 
<br><br> 
 
<table border="0"; align="center" width ="655" height="550" > 
 
<tr> 
 
<td> 
 
NATIONAL ID :</td><td><input type="text" name="nic" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
<b> 
 
REGISTRATION NUMBER :</td><td><input type="text" name="regno" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
PATIENT NAME :</td><td><input type="text" name="pname" size="40" /> 
 
</td> 
 
<tr> 
 
<td> 
 
PATIENT ADDRESS :</td><td><input type="text" name="address" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
Maritial Status: :</td> 
 
\t \t \t <td> 
 
\t \t \t <select name="mars"> 
 
       <option selected="" value="Default">(Please select your Status)</option> 
 
       <option value="Maried">Married</option> 
 
       <option value="Unmarried">UnMarried</option> 
 
      </select> 
 
\t \t \t </td> 
 
</tr> 
 
<tr> 
 
<td> 
 
RELIGION :</td> 
 
\t \t \t <td> 
 
\t \t \t <select name="rel"> 
 
       <option selected="" value="Default">(Please select your Religion)</option> 
 
       <option value="Buddhist">Buddhist</option> 
 
       <option value="Catholic">Catholic</option> 
 
       <option value="Christian">Christian</option> 
 
       <option value="Hindu">Hindu</option> 
 
       <option value="Muslim">Muslim</option> 
 
\t \t \t \t <option value="Others">Others</option> 
 
      </select> 
 
\t \t \t </td> 
 
</tr> 
 
<tr> 
 
<td> 
 
Date of Birth: </td><td><input type="date" name="dob"> 
 
<td> 
 
</tr> 
 
<tr> 
 
<td> 
 
FATHER'S NAME/HUSBAND'S NAME :</td><td><input type="text" name="fhn" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
CITY :</td><td><input type="text" name="city" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
TELEPHONE NUMBER/MOBILE NUMBER :</td><td><input type="text" name="tel" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
AGE :</td><td><input type="text" name="ag" size="40" /> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td> 
 
Gender :</td> 
 
\t \t \t <td> 
 
\t \t \t <select name="gen"> 
 
       <option selected="" value="Default">(Please select your Gender)</option> 
 
       <option value="Male">Male</option> 
 
       <option value="Female">Female</option> 
 
\t \t \t \t <option value="Other">Other</option> 
 
      </select> 
 
\t \t \t </td> 
 
</tr> 
 
<tr> 
 
<td> 
 
Room: </td><td><input type="text" name="room"> 
 
<td> 
 
</tr> 
 
</b> 
 
<tr> 
 
<td></td> 
 
<td> 
 
<input align="left" type="image" src="Images/submit.png" alt="Submit" name="submit" value="Submit" width="115" height="57"/> 
 
</form> 
 
<!--if submit button and reset button in same <form> will perform the formvalidation which leds the reset buttton also work as a submit button(wont reset anyting)--> 
 
<form><input align="center" type="image" src="Images/reset.png" alt="Reset" name="reset" value="reset" width="115" height="57"/> 
 

 

 
</td> 
 
</tr> 
 
</form> 
 
</table> 
 

 

 
</th> 
 
</table> 
 
<br><br> 
 

 
</body> 
 
</html>

+0

ich denke, es ist etwas falsch in Form in der HTML-Datei –

+0

Es tut nichts in 'if (Geschlechtselect (Geschlecht))'. Plus die verschachtelten "wenn" nur schrecklich, gibt es Tonnen von Möglichkeiten, um Code wartungsfähiger zu machen. –

+0

gender select ist eine Dropdown-Liste in html, sobald ausgewählt der Wert wird überprüft (es kann nicht standardmäßig sein, nachdem alles validieren Daten in der Datenbank speichern –

Antwort

0

Es scheint Ihre Validierungsmethode immer false zurückgibt. Wenn der von onSubmit aufgerufene Code false zurückgibt, wird die Aktion nicht ausgeführt. Das gleiche gilt für onclick und andere Event-Funktionen.

Sobald Sie alle Bedingungen ausgewertet haben, um gültig zu sein, sollten Sie True zurückgeben, um dem Formular mitzuteilen, dass es übernehmen und seine Sache tun kann.

+0

Validierung Teil funktioniert, aber ohne Formular, sobald ich Form dosent Arbeit hinzufügen –

Verwandte Themen