Jump to content

[SOLVED] Post form to self after JS validation returns true


ultrasound0000

Recommended Posts

Hi

I have a form that is validated by JavaScript when submitted.  When the JavaScript code returns true it submits the form.  I want to post the form on the same page, so I tried having an if statement.

 

if javascript validated then display a thank you message

else

display form

 

Is this possible?

The PHP/HTML code:

<?php
if(isset($_POST["first_name"]))
{

?>
<h4>Thank you <?php echo $_POST["first_name"]; ?> <?php echo $_POST["last_name"]; ?> for your submission</h4>

<?php
} else {
// Display the form
?>
  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="formConnect" id="myForm">

<label for="first_name">First Name*</label>
<span class="txt"><input id="first_name" name="first_name" type="text" tabindex="1"  /></span>

<label for="last_name">Last Name*</label>
<span class="txt"><input id="last_name" name="last_name" type="text" tabindex="2"  /></span>

<label for="company">Company*</label>
<span class="txt"><input id="company" name="company" type="text" tabindex="3"  /></span>

<label for="phone">Phone Number*</label>
<span class="txt"><input id="phone" name="phone" type="text" tabindex="4"  /></span>

<label for="email">Email*</label>
<span class="txt"><input id="email" name="email" type="text" tabindex="5"  /></span><br />

<div><label>Your Question*</label></div>
<div class="textarea"><textarea name="comment" wrap="soft" cols="10" rows="10"></textarea></div><br />

<span>
	<input class="submitBtn" type="submit" id="submitConnect" name="submitConnect" tabindex="6" value="submit" /><br />										
	<em>*Required Fields</em>
</span>
  </form>
<?php
}
?>

 

The JavaScript Code:

function submitForm()
{
var form;
form = document.forms["myForm"];

var name = form.first_name.value;
var last_name = form.last_name.value;
var email = form.email.value;
var phone = form.phone.value;
var company = form.company.value;  
var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;


if(name == "") {
  inlineMsg('first_name','<strong>Error</strong><br />Please enter your name.',2);
  return false;
}  
if(last_name == "") {
  inlineMsg('last_name','<strong>Error</strong><br />Please enter your last name.',2);
  return false;
} 
if(company == "") {
  inlineMsg('company','<strong>Error</strong><br />Please enter your company name.');
  return false;
}  
if(phone == "") {
  inlineMsg('phone','<strong>Error</strong><br />Please enter a phone number.',2);
  return false;
} 
if(email == "") {
  inlineMsg('email','<strong>Error</strong><br />Please enter your email.',2);
  return false;
}
if(!email.match(emailRegex)) {
  inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
  return false;
}

document.forms["formBusSolutions"].submit();  
return true;
}

 

change your form tag to this

 

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="formConnect" id="myForm" onsubmit="return submitForm()">

 

if it returns true, the form is submitted, if it returns false it will do nothing

 

then, a second suggestion would be to change your submission URL

<form action="<?php echo $_SERVER["PHP_SELF"]."?action=Submit"; ?>" method="post" name="formConnect" id="myForm" onsubmit="return submitForm()">

 

and then check based on the $_GET instead of a random $_POST variable

<?php
if(isset($_GET['action']) && $_GET['action']=='Submit') {
   echo "Thank you " . $_POST['first_name'] . " " . $_POST['last_name'] . " for your submission.";
}
else {
...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.