0

I'm processing a form using PHP and JQuery and it's not processing the way it should normally process. My HTML is:

    <form action="" method="POST" name="forgot_pass_form">
      <label><h3>Please Enter your email!</h3></label><br>
      <input type="text" name="email_pass" id="email_pass" />
      <input type="submit" name="submit" id="submit" value="Submit" />
    </form>

My JQuery code which is in the same page as HTML code:

<script type="text/javascript">
    $(document).ready(function($) {

        var email = $('#email_pass').val();

        $("#submit").live('click', function(){
                $.post('email_password.php', 
                {
                'email_pass' : email
                }, 
                function(data) {
                    alert(data);                
                });

                return false;
            });
    }); 
</script>

My PHP code which is on email_password.php:

<?php


    $email = $_POST['email_pass'];

    if(empty($email)){
        echo  'empty';  
    } else {
        echo 'Not'; 
    }
?>

The problem here is it always alert me with empty even if I've entered something in the text box

I even tried this way:

 if(empty($_POST)){
    echo 'empty';    
   } else {
    // process the form 
    $email = $_POST['email'];
    if(empty($email)){
        echo 'Email is empty';
    } else {
        echo 'Email is not empty';  
    }
}

By trying this way, it alerts me with Email is empty. Please help

user3293145
  • 193
  • 1
  • 10
  • what your network console says , is it posting "post" params? – Dimag Kharab Mar 27 '14 at 12:23
  • 1
    What jQuery version are you using? – MonkeyZeus Mar 27 '14 at 12:25
  • I think it's `1.9` or `1.10`. The problem was that I was not using `var email` inside the `.live()` method. It's solved :) – user3293145 Mar 27 '14 at 12:27
  • @user3293145 Please check out my **[answer here](http://stackoverflow.com/questions/20150130/ajax-and-php-to-enter-multiple-forms-input-to-database/20150474#20150474)** for a thorough AJAX example – MonkeyZeus Mar 27 '14 at 12:30
  • @MonkeyZeus What to use for `.attr('action')` if the form is submitting to itself using `form action=""`? – user3293145 Mar 27 '14 at 12:36
  • If you are following my example then **your** form should start like this `
    `
    – MonkeyZeus Mar 27 '14 at 12:38
  • No problem, I hope my example helps you going forward! Also, `$.post()` is just a wrapper-function which calls upon `$.ajax()` and defaults a lot of the parameters so technically speaking `$.post()` takes `.001%` more processing overhead lol. I personally find `$.post()` far too restrictive for my needs. – MonkeyZeus Mar 27 '14 at 12:44
  • One last thing, check out **[this answer](http://stackoverflow.com/a/21617685/2191572)** for a guide to debugging AJAX calls because you will inevitably need it. – MonkeyZeus Mar 27 '14 at 12:46

4 Answers4

3

When you try to capture a field value, it must be the same name. The name you have in your form is 'email_pass' and in your PHP code you are waiting for 'email'

This should be:

if(empty($_POST)){
    echo 'empty';    
   } else {
    // process the form 
    $email = $_POST['email_pass']; // The field form name
    if(empty($email)){
        echo 'Email is empty';
    } else {
        echo 'Email is not empty';  
    }
}

Sorry for my english :s

wrivas
  • 509
  • 6
  • 12
2

use this code

<script type="text/javascript">
    $(document).ready(function($) {



        $("#submit").live('click', function(){
 var email = $('#email_pass').val();
                $.post('email_password.php', 
                {
                'email_pass' : email
                }, 
                function(data) {
                    alert(data);                
                });

                return false;
            });
    }); 
</script>
Ananth
  • 1,520
  • 3
  • 15
  • 28
2

The issue is that your making a $_POST call with jQuery and in that function you "post" under the new name email_pass. Update your PHP file to reference $_POST['email_pass'] and you'll be good to go.

Sam Miller
  • 166
  • 5
1

Use 0n() and get email value inside the click function

<script type="text/javascript">
    $(document).ready(function($) {

          $("#submit").on('click', function(){
                var email = $('#email_pass').val();
                $.post('email_password.php', 
                {
                'email_pass' : email
                }, 
                function(data) {
                    alert(data);                
                });

                return false;
            });
    }); 
</script>
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • Listening for the click of the button is improper. You must listen for form submission to properly catch the event in ALL browsers. `$('form[name="forgot_pass_form"]').on('submit', functione(){});` – MonkeyZeus Mar 27 '14 at 12:27
  • Would it make difference on different browser if `.on('click')` is used instead of `.on('submit')`? @MonkeyZeus – user3293145 Mar 27 '14 at 12:29
  • @user3293145 that is exactly what I said, yes. Listening for the click of a button is a different event compared to the submission of a form. Even though `click` is working for you, it is not 100% reliable. Listening for a submit is 100% reliable. – MonkeyZeus Mar 27 '14 at 12:33