JS in the Ajax submitted to PHP JSON data, PHP can not get

Source: Internet
Author: User
Tags php json
$ (' #saveNewData '). Click (function () {////To save the data when the button is clicked, get the current data var type = $ (' select[name= "type"] Optio            N:selected '). Val ();            var title = $ (' input[name= "title"] "). Val ();            var imgsrc = $ (' input[name= "imgsrc"] "). Val ();            var author = $ (' input[name= "author"] "). Val ();            var Createdat = $ (' input[name= "Createdat"] "). Val ();                      var content = $ (' textarea[name= "content"] "). Val ();                Package data var = {Type:type, title:title, IMGSRC:IMGSRC,            Author:author, Createdat:createdat, content:content};                Ajax Commit Data $.ajax ({type: "POST", url: ' insert.php ', Data:data,                DataType: ' JSON ', error:function (request) {alert ("Save Failed"); }, Success:function (msg) {ALERT ("saved successfully");                alert (data);        }            }); })

Determines the data that can be obtained to the form element, the HTML address bar submits the time can display all the submission data
In the insert.php

$type =  $_POST['type'];$title = $_POST['title'];$imgSrc = $_POST['imgSrc'];$author = $_POST['author'];$createdAt = $_POST['createdAt'];$content = $_POST['content'];

Unable to get the data passed in, prompt
notice:undefined Index:type in d:xampphtdocs8-1baidunewsinsert.php on line 3

notice:undefined Index:title in d:xampphtdocs8-1baidunewsinsert.php on line 4

notice:undefined index:imgsrc in d:xampphtdocs8-1baidunewsinsert.php on line 5

notice:undefined Index:author in d:xampphtdocs8-1baidunewsinsert.php on line 6

notice:undefined Index:createdat in d:xampphtdocs8-1baidunewsinsert.php on line 7

notice:undefined index:content in d:xampphtdocs8-1baidunewsinsert.php on line 8

The first time with PHP, previously written JS and node data interaction with the data transfer form, but PHP can not get, which God showed me the code, extremely grateful

Reply content:

$ (' #saveNewData '). Click (function () {////To save the data when the button is clicked, get the current data var type = $ (' select[name= "type"] Optio            N:selected '). Val ();            var title = $ (' input[name= "title"] "). Val ();            var imgsrc = $ (' input[name= "imgsrc"] "). Val ();            var author = $ (' input[name= "author"] "). Val ();            var Createdat = $ (' input[name= "Createdat"] "). Val ();                      var content = $ (' textarea[name= "content"] "). Val ();                Package data var = {Type:type, title:title, IMGSRC:IMGSRC,            Author:author, Createdat:createdat, content:content};                Ajax Commit Data $.ajax ({type: "POST", url: ' insert.php ', Data:data,                DataType: ' JSON ', error:function (request) {alert ("Save Failed"); }, Success:function (msg) {ALERT ("saved successfully");                alert (data);        }            }); })

Determines the data that can be obtained to the form element, the HTML address bar submits the time can display all the submission data
In the insert.php

$type =  $_POST['type'];$title = $_POST['title'];$imgSrc = $_POST['imgSrc'];$author = $_POST['author'];$createdAt = $_POST['createdAt'];$content = $_POST['content'];

Unable to get the data passed in, prompt
notice:undefined Index:type in d:xampphtdocs8-1baidunewsinsert.php on line 3

notice:undefined Index:title in d:xampphtdocs8-1baidunewsinsert.php on line 4

notice:undefined index:imgsrc in d:xampphtdocs8-1baidunewsinsert.php on line 5

notice:undefined Index:author in d:xampphtdocs8-1baidunewsinsert.php on line 6

notice:undefined Index:createdat in d:xampphtdocs8-1baidunewsinsert.php on line 7

notice:undefined index:content in d:xampphtdocs8-1baidunewsinsert.php on line 8

The first time with PHP, previously written JS and node data interaction with the data transfer form, but PHP can not get, which God showed me the code, extremely grateful

Because you're sending an object data to the background, I guess you can get $_post[' data ' from the background.

Let me just experiment.
Yours

            //封装数据            var data = {                type:type,                title:title,                imgSrc:imgSrc,                author:author,                createdAt:createdAt,                content:content            };

This paragraph of the question, without the quotation marks? Type:type the type before the string is the variable, you feel the next ....

My Code:

1.html

    
  
                           

2.php


  
   

The data you passed was wrong. The upstairs has been made very clear.

Because you encapsulate the JS object instead of JSON, the regular JSON, the keys are all quoted, you can use Json.stringify to convert the object into a string and then submit, it is recommended that you go to the JSON specification.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.