0

I am new to ajax and jQuery. I am trying get html form value using ajax and jQuery. I am getting the value but i can not pass that value to another php file. I don't know what i am missing.. can someone help me please..

Here is my form.php file code:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Form</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#submit').click(function(){
                var srt = $("#input").serialize();
                // alert is working perfect
                alert(srt);
                $.ajax({
                    type: 'POST',
                    url: 'database.php',
                    data: srt,
                    success: function(d) {
                        $("#someElement").html(d);
                    }
            });
         });
       });
    </script>
</head>
<body>
<form id="input" method="post" action="">
    First Name:<input type="text" name="firstName" id="firstName">
    Last Name: <input type="text" name="lastName" id="lastName">
   <input type="submit" id="submit" value="submit " name="submit">
</form>
<div id="someElement"></div>
</body>
</html>

Here is my database.php file code:

<?php
if(isset($_POST['submit']))
{
    $firstName = $_POST['firstName'];

    $lastName = $_POST['lastName'];

    echo $firstName;

    echo $lastName;
}
0

4 Answers 4

2

You need to add:

return false;

to the end of your click handler. Otherwise, the default submit button action takes place, and the page is refreshed.

Also, remove the line:

if (isset($_POST['submit']))

Submit buttons aren't included when you call .serialize() on a form, they're only sent when the submit button is performing normal browser submission (in case there are multiple submit buttons, this allows the server to know which was used).

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

Comments

0

You need to do the following

 $('#submit').click(function(){
                var srt = $("#input").serialize();
                // alert is working perfect
                alert(srt);
                $.ajax({
                    type: 'POST',
                    url: 'database.php',
                    data: srt,
                    success: function(d) {
                        $("#someElement").html(d);
                    }
            return false;
});

Form default event will fire if you dont use return false.

Comments

0

You can use as below

$("#input").submit(function(event) {
    var srt = $("#input").serialize();
    // alert is working perfect
    alert(srt);
    $.ajax({
        type: 'POST',
        url: 'database.php',
        data: srt,
        success: function(d) {
            $("#someElement").html(d);
        }
    })
    return false;
}); 

2 Comments

Isn't this the same as S Russell's answer?
Yes i do agreed but i posted before him :) and i was used submit event.
0

You need to prevent the default action of click event or the form will be processed using the default form action and your ajax call will never be complete.

$("#input").submit(function(event) {
    event.preventDefault(); // prevent default action
    var srt = $("#input").serialize();
    // alert is working perfect
    alert(srt);
    $.ajax({
        type: 'POST',
        url: 'database.php',
        data: srt,
        success: function(d) {
            $("#someElement").html(d);
        }
    });    
}); 

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.