I looked for ages to prevent this being a duplicate but i can't seem to find an answer.
On a previous jquery page i used the following ajax call:
var daynum1 = $(this).text();
var month1 = $('.ui-datepicker-month').text();
var year1 = $('.ui-datepicker-year').text();
var myDate = daynum1 + " " + month1 + " " + year1;
$('#headingcontent').html(myDate);
$.ajax({
url: "events.php",
type: "POST",
data: { myDate: myDate },
dataType: "html",
success: function(html) {
$("#eventcontent").empty();
$("#eventcontent").append(html);
}
});
This worked absolutely perfectly. events.php was able to pick up the variable using:
if(isset($_POST['myDate']))
However I've come to use a different AJAX call to send data to a different page:
var titleevent = $("#dropdowntextbox").val();
$.ajax({
url: "editevents2.php",
cache: false,
data: { titleevent: titleevent },
dataType: "html",
success: function(html) {
$("#editeventspopup").empty();
$("#editeventspopup").append(html);
}
});
However I can't for the life in me get it working! The page that should receive the call has this set:
if(isset($_POST['titleevent']))
{
$title = $_POST['titleevent'];
echo "hooray found it";
}
else
{
echo "hello";
}
Every single time, hello
is all that is echoed. I've tried to add and alert to print the data back to me on success but i'm quite new to this so it just stopped it from working altogether.
My guess (which doesn't mean much) is because this variable is .val and not .html so its down the the data type. But i'm not sure what else i can set this to.
Thanks for the help.
Based on your edit it seems that the problem is just the type. The default type used by jQuery's $.ajax()
method is GET. In order to use POST, you need to specify that:
$.ajax({
url: "editevents2.php",
type: "post", // specify the type as POST
cache: false,
data: { titleevent: titleevent },
dataType: "html",
success: function(html) {
$("#editeventspopup").empty();
$("#editeventspopup").append(html);
}
});
As you are posting data with titleevent you will receive this on the server as titleevent.
You have this as your js code:
var titleevent = $("#dropdowntextbox").val();
$.ajax({
url: "editevents2.php",
cache: false,
data: {titleevent: titleevent},
dataType: "html",
success: function(html){
$("#editeventspopup").empty();
$("#editeventspopup").append(html);
}
});
You will receive it as:
if(isset($_POST['titleevent']))
{
$title = $_POST['titleevent'];
echo "hooray found it";
}
else
{
echo "hello";
}