jquery - Manipulate serialize ajax data to php -


i'm struggling reading post data ajax post in php code. i'm neither expert php nor jquery, learn few weeks ago. sorry if dont use yet terminology. serialize data ajax because have more field in form. here sake of clarity, show 1 field. i'm trying read each variable, example comment in case, try print_r($_post) , got error. cannot see how do, converting or syntax error. appreciate insights

php file

public function ajaxsave($post_id)     {      print_r($_post);      } 

jquery script

$('body').on('click','#savecomment',function(e) {               $("#comment-form").submit(function(e) {                 var postdata = $(this).serialize();                 alert(postdata);                 $.ajax(                  {                     url : "ajaxsave.php",                     type: "post",                     data : postdata,                     datatype:"json",                     success:function(data)                         {                             alert('success');                         },                     error: function(jqxhr, textstatus, errorthrown)                          {                             alert("error");                         }                 });                 e.preventdefault(); //stop default action                 });             $("#comment-form").submit();          }); 

form

<input id="comment_comment" type="text" name="comment[comment]" maxlength="140" size="60"> 

in firebug

in post tag, have

comment[comment]    mycomment comment[post_id]    16 

source

comment%5bcomment%5d=mycomment&comment%5bpost_id%5d=16 

in html tag, have

array ( [comment] => array ( [comment] => [post_id] => 16 ) )  

you're expecting json back, returning print of $_post superglobal, , that's parseerror.

remove datatype , log printout console see it

$('body').on('click', '#savecomment', function (e) {     e.preventdefault();     $("#comment-form").trigger('submit'); });  $("#comment-form").submit(function(e) {     e.preventdefault();     var postdata = $(this).serialize();     alert(postdata);     $.ajax({         url: "ajaxsave.php",         type: "post",         data: postdata,         success: function (data) {             console.log(data)         },         error: function (jqxhr, textstatus, errorthrown) {             alert("error");         }     }); }); 

and don't bind submit event inside click event


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -

PHPMotion implementation - URL based videos (Hosted on separate location) -