Processing json serialized form data in php

I need to save a variable from a json string named "msg" in my database, but I cannot catch it with $ msg = $ _POST ['msg']; How to catch him?

In addition, I want the contents of $ msg to be displayed on the webpage.

HTML

<div id="addCommentContainer"> <form id="addCommentForm" action=""> <textarea name="msg" id="msg" cols="82" title="Your comment" rows="2">Your comment...</textarea> <br /> <input type="text" name="author" title="name" value="<?php echo $_SESSION['username']; ?>" id="author" /> <br /> <div id="personal"> <input type="text" name="city" id="city" title="city (optional)" value="" /> <br /> <input type="text" name="email" id="email" title="e-mail (optional)" value="" /> <br /> <input type="text" name="url" id="url" title="website (optional)" value="" /> <input type="hidden" id="cam_id" class="hd" name="cam_id" value="<?php echo $cam_id ?>" /> </div> <input type="submit" id="submit" value="Comment" /> </form> </div> 

Javascript

 //if submit button is clicked $('#submit').click(function () { //start the ajax $.ajax({ //this is the php file that processes the data url: "/comment/insert.php", type: "POST", data: $("#addCommentForm").serialize(), contentType: "json", //success success: function (html) { //if returned 1/true (process success) if (html == 1) { //show the success message $('.done').fadeIn('slow'); //if process.php returned 0/false (send mail failed) } else alert('Sorry, unexpected error. Please try again later.'); } }); //cancel the submit button default behaviours return false; }); 

Php

  $msg = $_POST['msg']; // echo $msg; $author = $_POST['author']; $email = $_POST['email']; $url = $_POST['url']; $city = $_POST['city']; // include ("/home/sionvalais/domains/skiweather.eu/public_html/v3/functions/strip.php"); if ($cam_id>1) { if ($author=='NAME') { $author='Anonymous'; } $host = gethostbyaddr($ip); // mysql_query ("set character_set_results='utf8'"); mysql_query("INSERT INTO sv_review (author,email,msg,cam_id,url,lud) VALUES ( N'".$author."', '".$email."', N'".$msg."', '".$cam_id."', '".$url."', NOW() )"); } 
+7
source share
6 answers

As far as I know, the default value for the contentType property is "application/x-www-form-urlencoded; charset=UTF-8" , which is great for most cases.

But it looks like the data you are sending to the server is not a JSON object, setting contentType does not convert the data into a JSON object, it just declares the data type.

So, if data is just a serialized name / value pair, delete contentType: "application/json", and try again.

and if it is a valid JSON type, decode the hosted JSON object on the server using: $array = json_decode($json, true);


Accessing the JSON Object

You can follow the approach described below to get the JSON object on the server:

 $json = @file_get_contents('php://input'); $array = json_decode($json, true); // See what happens print_r($array); 
+2
source

$ POST ['msg'] must be $ _POST ['msg']

Also, to output your POST variables so that you can check them, use:

 echo print_r($_POST); 
+1
source

Use should cast the data in JSON: JSON.stringify($("#addCommentForm").serializeArray()) . $("#addCommentForm").serialize() does not do JSON.

+1
source

try these changes -

 //if submit button is clicked $('input#submit').click(function () { //start the ajax $.ajax({ //this is the php file that processes the data url: "/comment/insert.php", //GET method is used type: "POST", data: $("form#addCommentForm").serialize(), dataType: "application/json", //Do not cache the page cache: false, //success success: function (html) { //if returned 1/true (process success) if (html == 1) { //hide the form $('.form').fadeOut('slow'); //show the success message $('.done').fadeIn('slow'); //if process.php returned 0/false (send mail failed) } else alert('Sorry, unexpected error. Please try again later.'); } }); //cancel the submit button default behaviours return false; 

});

0
source

.serialize forms a query string that can be sent to the server. It does not create json, warns $("#addCommentForm").serialize() you should see the query string, not json, so remove the contentType: "application/json" $.ajax from $.ajax and you should be able to get msg in $_POST['msg'] in php

0
source

Do not use: contentType: "json"

Is this a post, rigth? therefore it will use:

 contentType: "multipart/form-data" 

or

 contentType: "application/x-www-form-urlencoded" 

Or just do not use someone to simplify. Remove all contentType or dataType , jQuery select one by default because the type is POST.

0
source

All Articles