2

I'm trying to post data to the same page.

I can see that the post from the ajax is working correctly but the $_POST in my index.php isn't finding the post after form submission.

<form method="post" id="classic_login" action="">
    <input type="text" name="user" placeholder="Username" class="classic_field" id="user_field" />
    <input type="text" name="pass" placeholder="Password"  class="classic_field" id="pass_field" />
    <input type="submit" name="login" value="Login" class="classic_button" id="login_button" />
    <input type="submit" name="register" value="Register" class="classic_button" id="register_button" />
</form> 

and I've tried both print_r($_POST) and isset both don't change after submission

print_r($_POST);
if(isset($_POST['user']) && isset($_POST['pass']))
   echo "works";

printing formdata after a test submission i get: user=testuser&pass=testpass

$("#classic_login").submit(function(event) {
    var formdata    =   $(this).serialize();
    event.preventDefault();

    $.ajax
    ({
        url: "",
        type: 'POST',
        data: formdata,
        //success: function(response) { alert(response); },
        error:  function() { alert("fail"); }
    });

});  
3
  • 2
    why you commented your success method? Commented Nov 9, 2014 at 7:38
  • As far as My understanding is concerned, If you want to post the form to the same php file,you can simply use action="your.php". Then why do you use ajax? Commented Nov 9, 2014 at 7:59
  • @Abdul no reason, was just testing. Forgot to uncomment. Commented Nov 9, 2014 at 8:31

1 Answer 1

1

Alternatively, you could use document.URL to make a request on the same page. Then in PHP, include an exit; after the request:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo '<pre>';
    print_r($_POST);
    exit; // this is important!
}

?>

<form method="post" id="classic_login" action="">
    <input type="text" name="user" placeholder="Username" class="classic_field" id="user_field" />
    <input type="text" name="pass" placeholder="Password"  class="classic_field" id="pass_field" />
    <input type="submit" name="login" value="Login" class="classic_button" id="login_button" />
    <input type="submit" name="register" value="Register" class="classic_button" id="register_button" />
</form> 

<!-- this is no brainer, of course you need to load the library -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("#classic_login").submit(function(event) {
    var formdata  = $(this).serialize();
    event.preventDefault();

    $.ajax({
        url: document.URL, // you can use this
        type: 'POST',
        data: formdata,
        success: function(response) { 
            alert(response); 
        }
    });
});
</script>

Sample Output

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

9 Comments

Forgot to mention, the js is external it's path to the index is: js/main.js
@joe sure, this answer's straightforward anyways, there's no need to big modifications, kindly try this one
Ok tried and still nothing, the print_r never executes or changes from "Array()"
@joe aside from the fact that there is already a working demo, i don't know to help more than that. its already on the same page. acutally you can just copy this answer straight up. here is the demo codepad.viper-7.com/h2UY4u
my response when alerted is very different to yours, mine is printing the entire html out in the alert: i.imgur.com/J5GMQLC.png could that have anything to do with it? Like i said my JS is external and not in index.php
|

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.