Skip to content
Advertisement

jQuery Ajax post to php not catching variable

What am i doing wrong. PHP doesn’t seem to catch title and wrapper from $.ajax. Does the code look correct. The success message i get indicate an error that title is not found.

jQuery main.html

$.ajax({
   type: "POST",
   url: "process.php",
   data: 'title=test&wrapper=testing',
   success: function(msg){
     alert( "Data Saved: " + msg );
   } 
});

PHP process.php

<?php 
$title = $_REQUEST['title'];
$wrapper = $_REQUEST['wrapper'];
...
?>

Advertisement

Answer

Take a look: jQuery.ajax()

The data parameter is better to be a Key/Value pairs object, it’s cleaner and easier to debug 🙂

$.ajax({
   type: "POST",
   url: "process.php",
   data: {
     title: 'test',
     wrapper: 'testing'
     },
   success: function(msg){
     alert( "Data Saved: " + msg );
   } 
});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement