使用ajax,jquery,php发送表单值

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;
}

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).

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.

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;
}); 

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);
        }
    });    
});