Home > Backend Development > PHP Tutorial > The json data submitted to php by ajax in js cannot be obtained by php

The json data submitted to php by ajax in js cannot be obtained by php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-09-19 09:16:40
Original
1177 people have browsed it

<code>$('#saveNewData').click(function () {
            //保存数据的按钮被点击的时候,获得当前数据
            var type = $('select[name="type"] option: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();
          
            //封装数据
            var data = {
                type:type,
                title:title,
                imgSrc:imgSrc,
                author:author,
                createdAt:createdAt,
                content:content
            };
            //ajax提交数据
            $.ajax({
                type: "POST",
                url:'insert.php',
                data:data,
                datatype:'json',
                error: function(request) {
                    alert("保存失败");
                },
                success: function(msg) {
                    alert("保存成功");
                    alert(data);
                }
            });
        })</code>
Copy after login
Copy after login

Make sure you can get the data of the form element. When submitting the html address bar, all submitted data can be displayed
In insert.php

<code>$type =  $_POST['type'];
$title = $_POST['title'];
$imgSrc = $_POST['imgSrc'];
$author = $_POST['author'];
$createdAt = $_POST['createdAt'];
$content = $_POST['content'];</code>
Copy after login
Copy after login

Unable to obtain the passed data, 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

This is my first time using php. I used to use the data transfer form when writing js and node data interaction, but php cannot get it. Can any master show me the code? I would be very grateful

Reply content:

<code>$('#saveNewData').click(function () {
            //保存数据的按钮被点击的时候,获得当前数据
            var type = $('select[name="type"] option: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();
          
            //封装数据
            var data = {
                type:type,
                title:title,
                imgSrc:imgSrc,
                author:author,
                createdAt:createdAt,
                content:content
            };
            //ajax提交数据
            $.ajax({
                type: "POST",
                url:'insert.php',
                data:data,
                datatype:'json',
                error: function(request) {
                    alert("保存失败");
                },
                success: function(msg) {
                    alert("保存成功");
                    alert(data);
                }
            });
        })</code>
Copy after login
Copy after login

Make sure you can get the data of the form element. When submitting the html address bar, all submitted data can be displayed
In insert.php

<code>$type =  $_POST['type'];
$title = $_POST['title'];
$imgSrc = $_POST['imgSrc'];
$author = $_POST['author'];
$createdAt = $_POST['createdAt'];
$content = $_POST['content'];</code>
Copy after login
Copy after login

Unable to obtain the passed data, 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

This is my first time using php. I used to use the data transfer form when writing js and node data interaction, but php cannot get it. Can anyone show me the code? I would be very grateful

Because you are sending an object data to the background, I guess you can get $_POST['data'] from the background.

I will do a simple experiment,
yours

<code>            //封装数据
            var data = {
                type:type,
                title:title,
                imgSrc:imgSrc,
                author:author,
                createdAt:createdAt,
                content:content
            };</code>
Copy after login

Is this a question written without quotation marks? type:type The type in front is a string and the type in the back is a variable. You can feel it...

My code:

1.html

<code><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>
    </title>
    <script src="http://lib.sinaapp.com/js/jquery/3.1.0/jquery-3.1.0.min.js"></script>
</head>
<body>
    <input id='qq' type="text" name='qq123' value='xiaole' >
    <input id='qq123' type="submit">
    <script>
    $('#qq123').click(function () {
        var data = $('#qq').val();
        // console.log(data);
        var data={'data':data};
        $.ajax({
                type: "POST",
                url:'2.php',
                data:data,
                datatype:'json',
                error: function(request) {
                    alert("保存失败");
                },
                success: function(msg) {
                    console.log(msg);
                    
                }
            });
    });
    </script>
</body>
</html></code>
Copy after login

2.php

<code>
<?php

$a = $_POST;

print_r($a);
echo json_encode($a);

?></code>
Copy after login

There is an error in the data you passed. The data should be written in json format. It's been made very clear above.

Because you are encapsulating JS objects instead of json. In regular json, the keys are all in quotes. You can use JSON.stringify to convert the object into a string before submitting it. It is recommended that you check the json specification.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template