1

This is probably very simple, but I'm really in over my head. Basically, I have a form with one input: email, and a submit button. This was not done by me so I understand very little of it. I tried to look up the .ajax jQuery command, but it still makes no sense.

Basically, here is what I have on the index.php.

The JS and HTML:

<!-- Subscription -->
<script>        
    $(function() {
        $('form input.submit').click(function (event) {
            event.preventDefault();
            event.returnValue = false;
            $.ajax({
                type: 'POST',
                url: 'subscribe.php',
                data: {email: $('form input[type=email]').val()},
                success: function(data) {
                    if (data == 'successful') {
                        $('#subscribe_status').html('Thanks for subscribing! We will let you know.').slideDown();
                    } else if (data == 'already_subscribed') {
                        $('#subscribe_status').html('This email is already subscribed.').slideDown();
                    } else if (data == 'invalid_email') {
                        $('#subscribe_status').html('This email is invalid.').slideDown();
                    } else {
                        $('#subscribe_status').html('Something went wrong. Please try again.').slideDown(); 
                    }
                },
                error: function () {
                    $('#subscribe_status').html('Subscription is not available.').slideDown();
                }
            });
        });
    });
</script>   
<form action="#">
    <input type="email" value="Your e-mail address..." onfocus="value=''" />
    <input type="submit" class="submit" value="Notify me" />
</form>

Subscribe.php

include 'go.php';
$email = $_POST['email'];
$ip=$_SERVER['REMOTE_ADDR'];
$timestamp = date("m/d/y H:i A");

mysql_query("INSERT INTO subscribers (ip, email, timestamp) VALUES (INET_ATON('$ip'), '$email', '$timestamp') ") or die(mysql_error());

I basically just keep getting "Something went wrong. Please try again."

6
  • in your subscribe.php put this: "var_dump($_POST);" and see what comes through Commented Aug 15, 2011 at 23:36
  • In your success function, the data parameter will be whatever your PHP file echo'd. So if you echo nothing on successful data entry (which it looks like might be happening), then the JavaScript will think that something went wrong. Can you confirm that the data is actually being entered into the database? Commented Aug 15, 2011 at 23:41
  • No, the data is not going in the database. Am I supposed to have 'echo "successful";' as my last line of my php? EDIT: That didn't work. Commented Aug 15, 2011 at 23:42
  • Then the next step is just assuming some dummy value for $ip and executing the SQL query by hand. What error message(s) do you get when you do that? Commented Aug 15, 2011 at 23:46
  • You should really attach a debugger and look at the response you're getting. Run it in Chrome and hit F12. Go to the network tab and look at what the server is telling you. (Sidenote: I hope this is just test/example code, as it's vulnerable to ... a lot.) Commented Aug 15, 2011 at 23:50

1 Answer 1

2

Subscribe.php doesn't return anything, so data in your JS is always emtpy, hence the last else is always executed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.