ultrasound0000 Posted March 24, 2009 Share Posted March 24, 2009 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? Link to comment https://forums.phpfreaks.com/topic/150931-solved-post-form-to-self-after-js-validation-returns-true/ Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 yes it is possible. post the code you have now so we can see where you stand Link to comment https://forums.phpfreaks.com/topic/150931-solved-post-form-to-self-after-js-validation-returns-true/#findComment-792927 Share on other sites More sharing options...
ultrasound0000 Posted March 24, 2009 Author Share Posted March 24, 2009 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; } Link to comment https://forums.phpfreaks.com/topic/150931-solved-post-form-to-self-after-js-validation-returns-true/#findComment-792943 Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 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 { ... Link to comment https://forums.phpfreaks.com/topic/150931-solved-post-form-to-self-after-js-validation-returns-true/#findComment-792956 Share on other sites More sharing options...
ultrasound0000 Posted March 24, 2009 Author Share Posted March 24, 2009 Hey lonewolf217 applied your suggestions and now it works great, thanks a ton! Link to comment https://forums.phpfreaks.com/topic/150931-solved-post-form-to-self-after-js-validation-returns-true/#findComment-793012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.