3

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.com/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

2
  • remove action="get.php" from form and add return false in ajax request at end Commented Oct 14, 2016 at 10:55
  • Thanks,but have tried this and it dose not work Commented Oct 14, 2016 at 10:59

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

and form like <form action="get.php" method="post" name="sendform" onsubmit="return send_value();">
check this post stackoverflow.com/questions/39948002/… you will find what you need
1

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>

9 Comments

Thanks @Rory, but dose not seem to fix the problem
Why's that? Any error messages? Which method have you attempted to use?
No its justprinting out the same as before and not getting the varibles
Have you checked the request in the console to ensure the data is being sent correctly?
@devpro nice, ill add a new jquery CDN and try again with Rory's answer. thanks
|
0

Please Use this form code

<head>

       <script src="//code.jquery.com/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();">

3 Comments

Would this work local like through wamp, as it just dose not work for me ?
I just get array (size=0)
I have also checked on wamp. Please make files name correct in form action attribute and in ajax url parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.