PHP发回空身 [英] PHP sends back empty body
问题描述
我正在尝试将表单数据从Vue Resource发送到我的PHP页面。我可以看到它显示在PHP页面上,但是当我以JSON的形式发回它时,我得到一个空响应。为什么发送空响应?
I'm trying to send form data from Vue Resource to my PHP page. I can see it displayed on the PHP page but when I send it back as JSON I get an empty response. Why is it sending an empty response?
看起来问题是问题是提交按钮未在PHP中设置。我不确定为什么会这样。尝试使用 $ _ REQUEST
和 axios / $。发布
,但没有区别。
It looks like the problem is that the value for the submit button is not set in PHP. I am not sure why that is happening. Tried using $_REQUEST
and axios/$.post
but it makes no difference.
if(isset($_POST['submit']){
echo json_encode($_POST);
}
JS:
JS:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
.then(data => console.log(data));
HTML:
HTML:
<form class="col s12" id="submitalbum" method="post" action="addalbum.php">
<div class="row">
<div class="input-field col s6">
<input name="artist" placeholder="Artist" type="text">
</div>
<div class="input-field col s6">
<input name="title" placeholder="Title" type="text">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="genre" placeholder="Genre">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="released" type="number" name="released" placeholder="Year Released">
</div>
<button @click.prevent="addNewAlbum" name="submit" class="waves-effect waves-light btn">Submit</button>
</div>
</form>
推荐答案
由于多种原因,返回空响应。请参阅下面的解释。
The empty response is returned for multiple reasons. See them explained below.
看来用于查找表单的选择器使用jQuery id selector:
It appears that the selector for finding the form uses the jQuery id selector:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
But the jQuery function (i.e. $()) returns a collection (an array):
var submitAlbumCollection = $('#submitalbum');
console.log('submitAlbumCollection: ', submitAlbumCollection);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="col s12" id="submitalbum" method="post" action="addalbum.php"></form>
然而 FormData()需要 HTML < form>
元素 1 ,不是数组。
However FormData() needs "An HTML <form>
element"1, not an array.
因此,如果您打算使用jQuery函数,请从中获取第一个元素以获取对表单的引用:
So if you are going to use the jQuery function, take the first element from it to get the reference to the form:
var forms = $('#submitalbum');
if (forms.length) {
this.$http.post('addalbum.php', new FormData(forms[0]))
按钮元素不发送值
A < button>
不是表单输入,因此表单数据中没有相应的值。如果您想检查表单值,请尝试检查艺术家,标题等。
button elements don't send value
A <button>
is not a form input and hence there would not be a corresponding value in the form data. This if you want to check for form values, try checking for the artist, title, etc.
if(isset($_POST['artist'])) {
echo json_encode($_POST);
}
如果你真的想检查 $ _ POST [ 'submit']
是真的,你可以:
If you really wanted to check if $_POST['submit']
is truthy, you could:
-
添加一个隐藏的输入:
add a hidden input:
<input type="hidden" name="submit" value="1">
将按钮转换为提交按钮
convert the button to a submit button
<input type="submit" name="submit" value="1">
take the suggestion from this answer and manually append an entry to the form data
在这个phpfiddle 。请注意,phpfiddle不允许多个页面,因此来自 addalbum.php 的代码放在顶部,对它的引用被替换为<?php echo $ _SERVER [ 'PHP_SELF'];?>
(即在客户端代码中)。
See a demonstration in this phpfiddle. Note that phpfiddle doesn't allow multiple pages so the code from addalbum.php was placed at the top, and references to it were replaced with <?php echo $_SERVER['PHP_SELF'];?>
(i.e. in the client-side code).
此外,PHP代码的第一行有一个缺少的括号:
Additionally, there is a missing parenthesis on the first line of the PHP code:
if(isset($_POST['submit']){
要纠正此问题,请添加缺少的右括号以关闭表达式:
To correct this, add the missing closing parenthesis to close the expression:
if(isset($_POST['submit'])){
1 https://developer.mozilla.org/en-US/docs/Web/API/FormData/ FormData
这篇关于PHP发回空身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!