I am trying to validate a html form using php. I have solved it in one like below:
Part of my html form:
<input class="text" type="text" name="fname" id="firstname1" required="required">
And at the signup.php I have
<?php
if (!preg_match("/^[a-zA-Z ]*$/",$_POST['fname']))
{
echo ('Name should include only characters');
die( '</body></html>' );
}
?>
But my problem is that in this form the error message does not appear at the same page with the form, but in another page. I want to have the error message appear at the page where the form is.
In javascript I make it this way, but I don't know how to make this in php: I build a function like below:
function name() {
var pass = document.getElementById('firstname1');
var sb = document.getElementById('submit1');
var letters=/^[a-zA-Z]+$/;
if (pass.value.match(letters) ){
sb.disabled = false;
document.getElementById('error-fname1').style.display = 'none';
}
else {
document.getElementById('error-fname1').style.display = 'block';
sb.disabled = true; }
}
and then I try to call it in this way:
<input class="text" type="text" name="fname" id="firstname1" required="required" onchange="name()">
<span class="errorformat " id="error-fname1" style="display: none" >
Please write only letters
</span>
Please help me...I am trying to learn php... Thanks in advance
actionof the form to another page? If so, the only way you could put an error message on the old page with the server-side PHP code is by redirecting back to the old page.AJAXrequest, but you will need javascript and PHP.