So I am trying to run this code and at the if (isset($_POST["Submit1"])) statement for isset, the form will not give the error message whenever I leave my form empty. It is supposed to give the $nameErr whenever the form is left empty, but it is not giving that error message. Whenever I delete the if (isset($_POST["Submit1"])) statement, it will run the else statement fine.
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
Here is the submit button code.
<input type="submit" name="Submit1" value="Calculate">
As you can see, the names are the same so I don't think that is the problem.
Here is the rest of my code if you wish to take a look.
<!DOCTYPE html>
<html lang="en">
<head>
<title>GPA Improvement Calculator</title>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<h1>GPA Improvement Calculator</h1>
<p><span class="error">All form fields must be completed for the GPA calculator to function.</span></p>
<?php
function sanitizeString($var)
{
$var = stripslashes($var);
$var = strip_tags($var);
$var = htmlentities($var);
return $var;
}
$name = "";
$nameErr = "";
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
?>
<form method="post" action="improveGPA.php">
Name: <input type="text" size="35" name="name" value="<?php echo $name; ?>">
<span class="error"><?php echo $nameErr; ?></span>
<br><br>
E-mail: <input type="text" size="35" name="email" value="">
<span class="error"></span>
<br><br>
<input type="checkbox" name="agree" >
I agree to the terms and conditions of this website.
<span class="error"></span>
<br><br>
Current GPA: <input type="text" size="4" name="currentGPA" value="">
<span class="error"></span>
<br><br>
Current Total Credits: <input type="text" size="3" name="currentCredits" value="">
<span class="error"></span>
<br><br>
I am taking <input type="text" size="3" name="newCredits" value="">
<span class="error"></span> credits this semester.
If I want to raise my GPA
<input type="text" size="4" name="GPAincrease" value="">
<span class="error"></span> points,
I need a <span style="font-weight: bold;">????</span> GPA on my courses this semester.
<br><br>
<input type="submit" name="Submit1" value="Calculate">
</form>
</body>
</html>
It will only start messing up if I have that one if statement in and just won't show that error message at all, when the form is empty or filled in.
Related
I am fairly new at PHP. I have an assignment which requires me to make two separate files. On called input.html and the other handle.php.
The input.html file has a for that will receive user input and then send it to the handle.php file.
The input.html should look like
and if all the fields are filed in then I should display this
The complication that I'm having is with the required filed. If the fields do not have text or the radio is not checked then it should warn users but with me it keep sending them to the PHP file and showing an error.
My code for input.html is
<!doctype html>
<html lang="en">
<head>
<title> Feedback Form </title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $responseErr = $commentErr = "";
$name = $email = $response = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
//Check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)){
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
//Check if email address is well-formated
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailErr = "Invalid email format";
}
}
if (empty($_POST["response"])) {
$responseErr = "Response is required";
} else {
$response = test_input($_POST["response"]);
}
if (empty($_POST["comment"])) {
$commentErr = "Comment is required";
} else {
$comment = test_input($_POST["comment"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p>Please complete this form to submit your feedback:</p>
<p><span class="error">* required field</span></p>
<!-- Start of the form -->
<form method="post" action="handle.php">
Name: <select name="prefix">
<option>Mr. </option>
<option>Mrs. </option>
<option>Ms. </option>
</select> <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email Address: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Response: This is...
<input type="radio" name="response" value="excellent">excellent
<input type="radio" name="response" value="okay">okay
<input type="radio" name="response" value="boring">boring
<span class="error">* <?php echo $responseErr;?></span>
<br><br>
Comments: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br><br>
<input type="submit" value="Send My Feedback">
</form>
<!-- End of the form -->
</body>
</html>
and my code for handle.php is
<html>
<head></head>
<body>
Thank you, <?php echo $_POST["prefix"] ?> <?php echo $_POST["name"]; ?> for your comment.<br><br>
You stated that you found this example to be '<?php echo $_POST["response"]; ?>'<br>
and added: <br>
'<?php echo $_POST['comment'];?>'<br>
</body>
</html>
So when I leave all the fields blank instead of it warning me that they are blank i get this
I tried to use w3shool as a guide line but it did not work. I would really appreciate some help and thank you to everything that is taking their time to attempt to help me. :)
You can use required to solve your problem. required stops the form from submitting if the required field is empty or not selected.
<form method="post" action="handle.php">
Name:
<select name="prefix">
<option>Mr. </option>
<option>Mrs. </option>
<option>Ms. </option>
</select>
<input type="text" name="name" required>
<span class="error">* <?php echo $nameErr;?></span>
<br>
<br> Email Address:
<input type="email" name="email" required>
<span class="error">* <?php echo $emailErr;?></span>
<br>
<br> Response: This is...
<input type="radio" name="response" value="excellent" required>excellent
<input type="radio" name="response" value="okay" required>okay
<input type="radio" name="response" value="boring" required>boring
<span class="error">* <?php echo $responseErr;?></span>
<br>
<br> Comments:
<textarea name="comment" rows="5" cols="40" required></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br>
<br>
<input type="submit" value="Send My Feedback">
</form>
I also recommend to use 'email' as type for email input field. This will make sure the input data is a valid email format.
I need to create a form, which will check if the input is empty or not. If it is empty there should be a text like "Required field". There is a notice saying, that surName has an undefined index.
Here is my PHP code
<?php
$name_error="";
$sname_error="";
$f_name="";
$s_name="";
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}
else {
$name_error="Required Field *";
}
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else {
$sname_error="Please fill this out";
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?
php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
</form>
</body>
</html>
well for starters, you could use the built-in HTML5 input validator required
<input type="text" required>
This will provide that nice user message on submit asking them to please fill out that field.
Secondly, you can check on the server using empty instead of if ($_POST['surName']!=='')
if ( !empty($_POST['surName']) ) {
// your logic here
}
The functionality is basically there already...
I guess the } bracket for if (isset($_POST['submit_button'])) { should be just before ?>
There is missing something like <input type="submit" name="submit_button" value="Register">
And considering what Jeff Puckett II says
So, if I do the changes 1 and 2 to your code, it might look like this:
<?php
$name_error="";
$sname_error="";
$f_name="";
$s_name="";
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name = $_POST['firstName'];
} else {
$name_error="Required Field *";
}
if ($_POST['surName']!=='') {
$s_name = $_POST['surName'];
} else {
$sname_error="Please fill this out";
}
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
<input type="submit" name="submit_button" value="Send">
</form>
</body>
</html>
It is because you've put the surName validation outsite of submit conditional;
`
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}else{
$name_error="Required Field *";
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else{
$sname_error="Please fill this out";
}
}
?>`
Your can improve this a little bit by using an associative array like this errors = array(); and then $errors['surName'] = "this is field is required";
You don't have a submit button. Another thing is that you didn't use your if statement of $_POST['surName'] after submit. You can use it like this :
<?php
error_reporting(1);
$name_error = "";
$sname_error = "";
$f_name = "";
$s_name = "";
if (isset($_POST['submit_button']))
{
if ($_POST['firstName'] != '')
{
$f_name = $_POST['firstName'];
}
else
{
$name_error = "Required Field *";
}
if ($_POST['surName'] != '')
{
$s_name = $_POST['surName'];
}
else
{
$sname_error = "Please fill this out";
}
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
<input type="submit" name="submit_button" value="Register">
</form>
</body>
</html>
I'm having a big problem with my validation form. Basically I created a form that will, once submitted, redirect to another page. To do this I used header("location: aaaaa.php") and apparently it works. The problem is that by doing this the validation doesn't work anymore, in fact if I don't enter any data and press submit I'll be redirected to the second page without getting the errors. If I delete the header the validation works again.
Moreover I have a big problem with the session method. I tried different way of using it to transfer the data to the second page when pressed the button submit, but it doesn't work and no one until now was able to help me. In the code that I'm gonna put below I deleted the session method and I was hoping that you would help me with that.
London Flight Agency
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name =($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["surname"])) {
$surnameErr = "Surname is required";
} else {
$surname =($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email =($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["telephone"])) {
$telephoneErr = "Number is required";
} else {
$telephone =($_POST["telephone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9\_]{7,20}/",$telephone)) {
$telephoneErr = "Enter correct telephone number";
}
}
if (empty($_POST["date"])) {
$dateErr = "Date is required";
} else {
$date =($_POST["date"]);
// check if name only contains letters and whitespace
if (!preg_match("~^\\d{1,2}[/.-]\\d{2}[/.-]\\d{4}$~",$date)) {
$dateErr = "Enter correct date of birth";
}
}
if (empty($_POST["luggage"])) {
$luggageErr = "Choose one of the options";
} else {
$luggage =($_POST["luggage"]);
}
$weight = $_POST['weight'];
$height = $_POST['height'];
if(isset($_POST['submit']))
{
$total = ($weight+$height)/10;
}
header("Location: thankyou.php");
}
?>
<h2 id="title">London Flight Agency</h2><!-- This tag is used to define HTML heading (h1 to h6), this particular one defines the most important heading -->
<form id="form" method="post" name="myForm" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" onsubmit="return validateForm(this);"><!-- The <form> tag is used to create an HTML form for user input -->
<h4 class="subtitle"><strong>Personal Details</strong></h4>
<fieldset>
Enter here all your details (all of them are compulsory.)
<br />
<br />
<select name="Title" id="title" value="<?php echo $title;?>" onblur="validateSelect(name)">
<option value="empty">Title</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
</select><br /><br />
<span class="validateError" id="titleError" style="display: none;">You must select a title!</span>
<span id="error8"><?php echo $titleErr;?></span>
<table
<tr>
<td><label for="firstname">First Name:</label></td>
<td><input type="text" name="name" value="<?php echo $name;?>" id="name" onblur="validateName(name)"></td>
<span id="nameError" style="display: none;"></span>
<span id="error1"><?php echo $nameErr;?></span>
</tr>
<tr>
<td><label for="surname">Surname:</label></td>
<td><input type="text" name="surname" value="<?php echo $surname;?>" id="name" onblur="validateSurname(surname)"></td>
<span id="surnameError" style="display: none;"></span>
<span id="error2"><?php echo $surnameErr;?></span>
</tr>
<tr>
<td><label for="email">Email address:</label></td>
<td><input type="text" name="email" value="<?php echo $email;?>" id="email" onblur="validateEmail(email)"></td>
<span id="emailError" style="display: none;"></span>
<span id="error3"><?php echo $emailErr;?></span>
</tr>
<tr>
<td><label for="telephone">Telephone No:</label></td>
<td><input type="text" name="telephone" value="<?php echo $telephone;?>" id="telephone" onblur="validateTelephone(telephone)"></td>
<span id="telephoneError" style="display: none;"></span>
<span id="error4"><?php echo $telephoneErr;?></span>
</tr>
<tr>
<td><label for="date">Date of birth:</label></td>
<td><input type="text" name="date" value="<?php echo $date;?>" id="date" onblur="validateDate(date)"></td>
<span id="dateError" style="display: none;"></span>
<span id="error5"><?php echo $dateErr;?></span>
</tr>
</table>
</fieldset>
<h4 class="subtitle"><strong>Flight Details</strong></h4>
<fieldset>
<p>Hand luggage:</p><br />
<input type="radio" name="luggage" <?php if (isset($luggage) && $luggage=="Yes") echo "checked";?>value="Yes" id = "myRadio" required onblur="myFunction()">Yes
<input type="radio" name="luggage" <?php if (isset($luggage) && $luggage=="No") echo "checked";?>value="No" id = "myRadio" required onblur="myFunction()">No
<span id="luggageError" style="display: none;"></span>
<span id="error6"><?php echo $luggageErr;?></span>
<br /><br />
<label for="extrabag">Include free extra bag</label>
<input type="checkbox" name="extra" id="check" value="bag">
<br />
<br />
<select name="option" id="card" onblur="validatePayment(card)">
<option value="empty">Payment Card</option>
<option value="Visa">Visa Debit Card</option>
<option value="MasterCard">MasterCard</option>
<option value="PayPal">PayPal</option>
<option value="Maestro">Mestro</option>
<option value="Visa Electron">Visa Electron</option>
</select><br />
<span class="validateError" id="cardError" style="display: none;">You must select your payment card!</span>
</fieldset>
<h4 class="subtitle"><strong>Luggage Details</strong></h4>
<fieldset>
<p>Enter weight and height of your luggage.</p>
Your Weight(kg): <input type="text" name="weight" size="7"><br />
Your Height(cm): <input type="text" name="height" size="7"><br />
<span id="error7"><?php echo $measureErr;?></span>
<input type="button" value="Tot. price" onClick="totalPrice()"><br /><br />
Total Price: <input type="text" name="total" value="<?php echo $total?>" size="10"><br /><br />
<input type="reset" value="Reset">
<input type="submit" value="Submit" name="submit">
</fieldset>
</form>
</body>
It doesn't show the first part of the code for I don't know which reason. I'll post it below:
London Flight Agency
The problem Is that you are checking if the method is post then redirect to the new page.
You must check if the your entries are valid then redirect.
For Example You can make an array of data and use array_push to push new data whenever there is something invalid and then check like this :
if( count( $array ) == 0 )
header('Location:YOUR_NEW_PAGE.php');
This is one of the simplest solutions you can implement.
Good luck
I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
I am working on a form that will be validated in Javascript and then, if it is valid, proceed to a PHP submission. The PHP is working fine and will not allow a submission if the input isn't valid. However, I can't get the form to stop before going to the PHP page if the validation function returns as false. Does anyone know what I can do to make this work?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Form</title>
<script src="contact.js" type="text/javascript"> </script>
</head>
<body>
<form name="contact"id="contact" action="contact.php" onsubmit="return formSub();"method="post" >
<h2 class="headingText"><em>What's your name?</em></h2>
<p>
<label for="firstName">First Name </label>
<input type="text" name="firstName" id="firstName" tabindex="7">
<span id="firstNameHTML" class="error"> </span>
</p>
<p>
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" tabindex="8">
<span id="lastNameHTML" class="error"> </span>
</p>
<p> </p>
<h2 class="headingText"><em>What's your preferred email address?</em></h2>
<p>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" tabindex="9">
<span id="emailHTML" class="error"> </span>
</p>
<p> </p>
<h2 class="headingText"><em>What would you like to contact us about?</em><br><span id="interestHTML"></span>
</h2>
<p>
<label>
<input type="checkbox" name="Interest" value="training" id="Interest_training" tabindex="10">
Training Services</label>
<br>
<label>
<input type="checkbox" name="Interest" value="testing" id="Interest_testing" tabindex="11">
Testing Services</label>
<br>
<label>
<input type="checkbox" name="Interest" value="remediation" id="Interest_remediation" tabindex="12">
Remediation Services</label>
<br>
<label>
<input type="checkbox" name="Interest" value="General Question" id="Interest_general" tabindex="13">
General Question</label>
<br>
<label>
<input type="checkbox" name="Interest" value="error" id="Interest_error" tabindex="14">
Report a Website Error</label>
<br>
<label>
<input type="checkbox" name="Interest" value="other" id="Interest_other" tabindex="15">
Other</label>
</p>
<p>
<label for="comment"><span class="headingText">Please enter your question or comments here. </span></label><br>
<span id="commentHTML"></span>
<textarea name="comment" id="comment" cols="45" rows="5" width="100px" tabindex="16"></textarea>
</p>
<input name="submit" type="submit" value="Submit the Form" tabindex="17">
<input name="reset" type="reset" value="Reset the Form" tabindex="18">
</form>
<p> </p>
<p> </p>
</body></html>
Javascript Document:
// JavaScript Document
function checkForm()
{
formReset();
var error=0;
//Check firstName has value
if (document.getElementById("firstName").value=="")
{
document.getElementById("firstNameHTML").innerHTML="<strong> Please provide a first name</strong>";
error++;
if(error==1)
{
document.getElementById("firstName").focus();
}
}
//Check lastName has value
if (document.getElementById("lastName").value=="")
{
document.getElementById("lastNameHTML").innerHTML="<strong> Please provide a last name</strong>";
error++;
if(error==1)
{
document.getElementById("lastName").focus();
}
}
//Check email is valid
if (document.getElementById("email").value=="" || document.getElementById("email").value.search("#") < 0)
{
document.getElementById("emailHTML").innerHTML="<strong> Please provide a valid email address</strong>";
error++;
if(error==1)
{
document.getElementById("email").focus();
}
}
//Check Interest has value
if (document.getElementByName("Interest").value=="")
{
document.getElementById("InterestHTML").innterHTML="<strong> Please let us know what you are interested in contacting us about.</strong>";
error++;
}
//Check Comment has value
if (document.getElementById("comment").value=="")
{
error++;
document.getElementById("commentHTML").innerHTML="<strong> Please provide your questions or comments here</strong><br>";
if(error==1)
{
document.getElementById("comment").focus();
}
}
if (error==0)
{
alert("Passed");
return true;
}
alert("Failed");
return false;
}
function formReset(){
document.getElementById("firstNameHTML").innerHTML="";
document.getElementById("lastNameHTML").innerHTML="";
document.getElementById("emailHTML").innerHTML="";
alert("Reset");
}
function formSub(){
if(checkForm())
{
alert("Check is True");
document.getElementById("contact").submit();
return true;
}
alert("I'm sorry, your submission cannot be completed.");
return false;
}
You should do:
onsubmit="return formSub();"
delete javascript:
If your function returns false the form wont be submitted.
You have made a mistake on checkForm function. getElementsByName returns an array of elements. So, in order to check if all of them are unchecked, you have to replace the code with this:
//Check Interest has value
var interests = document.getElementsByName("Interest");
var noneChecked = true;
for(var i = 0; i < interests.length; i++) {
if (interests[i].checked) {
noneChecked = false;
}
}
if (noneChecked) {
document.getElementById("interestHTML").innterHTML="<strong> Please let us know what you are interested in contacting us about.</strong>";
error++;
}
Then your function will work as you wanted.