Busqué años para evitar que esto sea un duplicado, pero parece que no puedo encontrar una respuesta.
En una página anterior de jquery, utilicé la siguiente llamada ajax:
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); } });Esto funcionó absolutamente a la perfección. events.php pudo recoger la variable usando:
if(isset($_POST['myDate']))Sin embargo, he llegado a usar una llamada AJAX diferente para enviar datos a una página diferente:
var titleevent = $("#dropdowntextbox").val(); $.ajax({ url: "editevents2.php", cache: false, data: { titleevent: titleevent }, dataType: "html", success: function(html) { $("#editeventspopup").empty(); $("#editeventspopup").append(html); } });¡Sin embargo, no puedo por la vida en mí hacer que funcione! La página que debe recibir la llamada tiene este conjunto:
if(isset($_POST['titleevent'])) { $title = $_POST['titleevent']; echo "hooray found it"; } else { echo "hello"; } Cada vez, hello es todo lo que se repite. Intenté agregar y alertar para imprimirme los datos en caso de éxito, pero soy bastante nuevo en esto, por lo que simplemente impidió que funcionara por completo.
Mi conjetura (que no significa mucho) es porque esta variable es .val y no .html, por lo que depende del tipo de datos. Pero no estoy seguro de qué más puedo configurar esto.
Gracias por la ayuda.
Según su edición, parece que el problema es solo el tipo. El tipo predeterminado utilizado por el método $.ajax() de jQuery es GET. Para usar POST, debe especificar que:
$.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); } });A medida que publica datos con titleevent , los recibirá en el servidor como titleevent .
Tienes esto como tu código js:
var titleevent = $("#dropdowntextbox").val(); $.ajax({ url: "editevents2.php", cache: false, data: {titleevent: titleevent}, dataType: "html", success: function(html){ $("#editeventspopup").empty(); $("#editeventspopup").append(html); } });Lo recibirás como:
if(isset($_POST['titleevent'])) { $title = $_POST['titleevent']; echo "hooray found it"; } else { echo "hello"; }