javascript - Using POST via AJAX and PHP to send data - Stack Overflow

I have an object I am sending in an AJAX request:function send_value() {$.ajax({type: 'post',

I have an object I am sending in an AJAX request:

 function send_value() {
    $.ajax({
        type: 'post',
        url: 'get.php',
        data: {
            source1: "some text",
            source2: "some text 2",
            uniId: 3
        },
        success: function (data) {
            console.log(data);
        }
    });
    return false;
}

I am trying to post them on click of a button

<head>
    <script src="./script.js"></script>
    <script src="//code.jquery/jquery-1.12.0.min.js"></script>
</head>
<body>
    <form action="get.php" method='post' name='sendform' onSubmit='send_value()'>
        <input type='submit' value='Test1'>
    </form>
</body>

Then I print the variables using PHP:

<?php
    if (!empty($_POST["uniId"])) 
    {
        if ($_POST["uniId"] == 3) 
        {
            echo 'your logged in as '; 
            echo $_POST['uniId'];

            $src1 = $_POST['source1'];
            $src2 = $_POST['source2'];
            echo $src1;
            echo $src2;
        } 
        else 
        {
            echo 'sorry uniID is not correct';
        }
    } 
    else 
    {
        echo "Im sorry the page was not able to load ";
        var_dump($_POST);
    }

Now the uniID is 3 so I hope to see:

your logged in as 3 some text some text2

But instead I get:

Im sorry the page was not able to load
C:\wamp64\www\mysite\get.php:20:
array (size=0)
empty

What is wrong in my code that the variables are not being posted and printing out in my PHP?

Thanks

I have an object I am sending in an AJAX request:

 function send_value() {
    $.ajax({
        type: 'post',
        url: 'get.php',
        data: {
            source1: "some text",
            source2: "some text 2",
            uniId: 3
        },
        success: function (data) {
            console.log(data);
        }
    });
    return false;
}

I am trying to post them on click of a button

<head>
    <script src="./script.js"></script>
    <script src="//code.jquery./jquery-1.12.0.min.js"></script>
</head>
<body>
    <form action="get.php" method='post' name='sendform' onSubmit='send_value()'>
        <input type='submit' value='Test1'>
    </form>
</body>

Then I print the variables using PHP:

<?php
    if (!empty($_POST["uniId"])) 
    {
        if ($_POST["uniId"] == 3) 
        {
            echo 'your logged in as '; 
            echo $_POST['uniId'];

            $src1 = $_POST['source1'];
            $src2 = $_POST['source2'];
            echo $src1;
            echo $src2;
        } 
        else 
        {
            echo 'sorry uniID is not correct';
        }
    } 
    else 
    {
        echo "Im sorry the page was not able to load ";
        var_dump($_POST);
    }

Now the uniID is 3 so I hope to see:

your logged in as 3 some text some text2

But instead I get:

Im sorry the page was not able to load
C:\wamp64\www\mysite\get.php:20:
array (size=0)
empty

What is wrong in my code that the variables are not being posted and printing out in my PHP?

Thanks

Share Improve this question edited Oct 14, 2016 at 10:59 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Oct 14, 2016 at 10:51 Benjamin OatsBenjamin Oats 5711 gold badge10 silver badges26 bronze badges 2
  • remove action="get.php" from form and add return false in ajax request at end – devpro Commented Oct 14, 2016 at 10:55
  • Thanks,but have tried this and it dose not work – Benjamin Oats Commented Oct 14, 2016 at 10:59
Add a ment  | 

3 Answers 3

Reset to default 1

You're missing the dataType

function send_value() {
    $.ajax({
        type: 'POST',
        url: 'get.php',
        dataType: "json",
        data:({"uniId":"test"}),
        success: function (data) {
            console.log(data);
        }
    });
    return false;
}

The issue is because you're not preventing the standard form submission. Hence your form element is sent with no data as it contains no form control elements.

To fix this you can return the function output to the event handler:

<form action="get.php" method="post" name="sendform" onsubmit="return send_value()">

However and much better approach is to attach the submit event using unobtrusive JS and prevent the standard form submission. As you're using jQuery already, here's how you can do that:

$(function() {
    $('form').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: 'get.php',
            data: {
                source1: "some text",
                source2: "some text 2",
                uniId: 3
            },
            success: function (data) {
                console.log(data);
            }
        });
    });
});
<form action="get.php" method='post' name='sendform'>
    <input type='submit' value='Test1'>
</form>

Please Use this form code

<head>

       <script src="//code.jquery./jquery-1.12.0.min.js"></script>
</head>
<body>
    <form action="get.php" method="post" name="sendform" onsubmit="return send_value();">
        <input type='submit' value='Test1'>
    </form>
</body>
 <script>

 function send_value() {
    $.ajax({
        type: 'post',
        url: '2.php',
        data: {
            source1: "some text",
            source2: "some text 2",
            uniId: 3
        },
        success: function (data) {
            console.log(data);
        }
    });
    return false;
}

</script>

"get.php" FILE

<?php
print_r($_POST);
    if (!empty($_POST["uniId"])) 
    {
        if ($_POST["uniId"] == 3) 
        {
            echo 'your logged in as '; 
            echo $_POST['uniId'];

            $src1 = $_POST['source1'];
            $src2 = $_POST['source2'];
            echo $src1;
            echo $src2;
        } 
        else 
        {
            echo 'sorry uniID is not correct';
        }
    } 
    else 
    {
        echo "Im sorry the page was not able to load ";
        var_dump($_POST);
    }
    ?>

This is the main thing:

<form action="2.php" method="post" name="sendform" onsubmit="return send_value();">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272795a4566183.html

相关推荐

  • javascript - Using POST via AJAX and PHP to send data - Stack Overflow

    I have an object I am sending in an AJAX request:function send_value() {$.ajax({type: 'post',

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信