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
action="get.php"from form and add return false in ajax request at end