0

I currently have a form that looks like this (using Bootstrap): current look

I've traditionally processed the form via post to another php file like so

<form action="complete.php" method="post" class="form-inline" role="form">

However, it kind of ruins the user experience when they're taken to a different page, and I've seen something before, where after submitting a form, the text just changed if it was valid. So, the text and form of the above image might just be replaced with "Thank you, your email has been accepted" if they offer a valid email.

So this question is two-part:

First, how do I do this on the backend? I'm using php for simplicity since it was so easy to install.

Second, how do I do this on the front end? Is there a common reference term for this kind of action in JS?

Answering either part of this (both if you can!) would be wonderful. If you have reference documents for me that aren't too complicated (I'm new to this), I'd be more than happy to read them too.

Thank you!

Community
  • 1
  • 1
FinalJon
  • 365
  • 2
  • 4
  • 13

4 Answers4

2

I'm going to extend on what Sam Sullivan said about the Ajax method.

Ajax basically runs any script in the background, making it virtually unnoticeable to the user. Once the script runs you can return a boolean or string to check if the result is true or false.

JS:

function validateForm(){
    $.ajax({
        type: 'POST',
        url: '/path/to/processForm.php',
        data: $('#yourForm').serialize(),
        success: function(output){
            if(output){ // You can do whatever JS action you want in here
                alert(output);
            }else{
                 return true; // this will redirect you to the action defined in your form tag, since no output was found.
            }
        }
    });
    return false;
}

Then in your processForm.php script, you validate the data through $_POST. Whatever you echo out in this script, will be your output.

For more, http://api.jquery.com/jquery.ajax/

rcorrie
  • 921
  • 8
  • 26
1

Either include the PHP and form logic on the same page:

<?php
if(isset($_POST['submit'])) {
  // Submit logic
  echo 'Success';
}
?>

<form action="" method="POST">
  <!-- etc -->
  <input type="submit" name="submit" value="Submit" />
</form>

Or you can submit it with AJAX:

<form action="" method="POST" onsubmit="submitForm(this); return false;">
  <!-- etc -->
  <input type="submit" name="submit" value="Submit" />
</form>

<script type="text/javascript">
  function submitForm(form)
  {
    // This can use AJAX to submit the values to a PHP script
  }
</script>

If you have jQuery, you don't need to use an inline event handler (which is better):

<script type="text/javascript">
  $('form').submit(function(event) {
    event.preventDefault();

    $form = $(event.target);
    // AJAX here
  });
</script>

This should be enough to get started..let me know if you have specific questions.

Sam
  • 20,096
  • 2
  • 45
  • 71
0

Change the form to

<form action="[whatever the page name is]" method="post" class="form-inline" role="form">

First, how do I do this on the backend? I'm using php for simplicity since it was so easy to install.

At the top of the page, add

<?php
if(isset($_POST)){
// Check for the $_POST variables and process
// $content = "<div> ... </div>"  // Then echo out the content in place of the original for

}
?>
ZombieBunnies
  • 268
  • 2
  • 11
0

You can just put form action="filename-of-the-form-processor" or leave it blank for same page. If you can't avoid to put php module on the same page where your form reside make a view.php file then just include it.

index.php <- where form process happends index.view.php <- where form tags reside so you will have a cleaner line of codes.

Note: this is not the best way to do it.