Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  58] [ 3]  / answers: 1 / hits: 30033  / 9 Years ago, sat, september 5, 2015, 12:00:00

I want to submit my form to a PHP file called reg.php after validating it using JavaScript. The HTML form is given below:



HTML Form:



<form method=post>
<input type=text id=username name=username></input>
<button type=button onclick=val()>Sign Up</button>
</form>


JavaScript Validation



The following is the JavaScript validation code:



function val(){

var uname = document.getElementById('username').value;
if(!uname){
document.getElementById(error).innerHTML = Enter a username;
return false;
}
else{
// I want to submit form if this else statement executes.
}


}

More From » php

 Answers
5

Just add action="reg.php" in <form> and return val() functions on onclick, also make type="submit" to form submission, like:


HTML CODE:


<form method="post" action="reg.php">
<input type="text" id="username" name="username"></input>
<button type="submit" onclick="return val()">Sign Up</button>
</form>

JAVASCRIPT CODE:


Just add return true; in else like:


function val(){

var uname = document.getElementById('username').value;
if(!uname){
document.getElementById("error").innerHTML = "Enter a username";
return false; // in failure case
}
return true; // in success case
}

[#65179] Thursday, September 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lailab

Total Points: 706
Total Questions: 102
Total Answers: 95

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;