$ (' #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.