PHP发回空身

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?

Edit:

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.

PHP:

if(isset($_POST['submit']){
   echo json_encode($_POST);
}

JS:

this.$http.post('addalbum.php', new FormData($('#submitalbum')))
.then(data => console.log(data));

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>
doulianxing4015
doulianxing4015 仍然无法正常工作。
3 年多之前 回复
du3932066
du3932066 好的,因为输出为空,这意味着检查$_POST['submit']存在失败。尝试将提交按钮更改为<input@click.prevent=“addNewAlbum”type=“submit”name=“submit”value=“Submit”class=“waves-effectwaves-lightbtn”>
3 年多之前 回复
duanben1909
duanben1909 更新了我的帖子。
3 年多之前 回复
dongzai2952
dongzai2952 向我们展示您的#submitalbum表格
3 年多之前 回复
dow66098
dow66098 PHP是疯了,我会离开,抱歉我的脑袋...
3 年多之前 回复
dongtuan8547
dongtuan8547 使用echo语句。如果它不是$_POST数组,它可以正常工作。
3 年多之前 回复
duanbing6955
duanbing6955 在上面的代码中你在哪里“发回”?
3 年多之前 回复

1个回答

The empty response is returned for multiple reasons. See them explained below.

jQuery 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>

However FormData() needs "An HTML <form> element"1, not an array.

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]))

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);
}

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

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).

Missing parenthesis

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'])){

1https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

</div>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐