0

If my form is filled out completely how do I prompt "Thanks for completing the form, "name"!"

function submit_onclick() {
  if(confirm("Thanks for completing the form " + form.name.value))
    document.forms[0].submit();
  else
    return false;
}

jquery-1.10.2.min.js script src="jquery.validate.min.js those are my externals via jquery. it validates next to the form. I just need prompt telling the "Name" thanks for completing.

jsFiddled here

this prompts even when form is empty.

13
  • 16
    As a potential user of your web application, I ask that you not implement this at all. Sep 27, 2013 at 16:58
  • 1
    BTW I agree with @SeanBright Sep 27, 2013 at 17:03
  • 1
    Please don't do this, please don't do this, please don't do this....
    – Bucket
    Sep 27, 2013 at 17:06
  • 2
    @user2807515 STOP adding information in 'comments'. Insert relevant information in you question. Sep 27, 2013 at 17:11
  • 1
    You can update your question by clicking Edit, then making the appropriate changes.
    – Bucket
    Sep 27, 2013 at 17:59

2 Answers 2

3

Expanding on DesertIvy's answer, I think it would be much less annoying for the user if you simply just displayed a message on the web page after the form has completed validation. It would be much less obtrusive. Something like this:

<form id="myForm" action="http://example.com/formSubmit.php" method="post">
  <div>
    <input type="text" name="name">
    <input type="submit">
  </div>
</form>

JavaScript:

function validForm() {
    // form validation - return true or false
}

function submit_onclick() {
    if(validForm()) {
        $('#myForm').submit(); // this will submit the form to the form action
    }
}

Then on load of the formSubmit.php page, you could display your message in the HTML body. This is if your server side language is PHP:

<?php
    if(isset($_POST['name'])
        echo "Thanks for completing the form " . $_POST['name'] . ".";
    else
        renderFormBody(); // function for rendering the form body
?>
3
  • what about displaying that echo string on the same page, under the form? its a dummy form
    – 2Truth
    Sep 27, 2013 at 17:48
  • It's possible to post to the same form that you fill out in PHP. You just need to check that $_POST['name'] is set and then display this message if it is set. I'll update my answer to show how it's done. Sep 27, 2013 at 17:51
  • 1
    My name is DesertIvy and I approve this message. :)
    – Bucket
    Sep 27, 2013 at 17:58
0

I'm going to bite the bullet and give you the answer you want, but I strongly encourage you to not ever do this, not in a million years.

Users hate clicking, and even though your dialog box is "personable" (read: addresses them by whatever name they submit), it's extremely annoying and I don't like it. But here's how you'd do it, since you asked, using jQuery:

function isFormValid() {
    //validate your form here, and return either true or false
}

function submit_onclick() {
    if(isFormValid() && confirm("Thanks for completing the form " + $('#nameField').val()) {
        $('#myForm').submit();
    }
}
1
  • I'll remember the info. Thanks for the help
    – 2Truth
    Sep 27, 2013 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.