duanshan188866 2017-03-23 06:21
浏览 166
已采纳

如何通过返回空数据字符串来解决Ajax“POST”方法?

I am fairly new to posting on StackOverflow so I hope that my post follows the correct criteria, if it doesn't I apologise. I shall try to keep my question and explanation of my issue as short as possible.

I am trying to create a input field which allows the user to update their About section on their profile for other users to be able to see. I have looked at similar posts but none of them really had solutions that matched my problem from what I could find.

I am using a form with a POST method which has a hidden field that includes the value of the userId and stores the string from the input field. I am using an Onclick function associated to the button with the id = "post" which calls an Ajax request which stores the value of the input into a variable known as var text which I want then to be stored into a database with the userId and the comment. However, the console returned "an empty string" (image linked at the very bottom). I am not to sure how to figure it out since I am still very new to using Ajax, and I'm trying to learn as much as I can.

myAccount.php

<h2>About</h2>      
<form method='POST' id="postForm">
<input type='hidden' name='posterId' value="<?php echo $user['userId']?>">
<input name="post" id='post' value="">
<br/>
<br/>
<button type="button" onclick='setComment()' name='submit'>Post it</button>
</form> 

main.js

function setComment() {
    var text = $("#post").val();
    $.ajax({
        type: 'POST', 
        url: 'setAbout.php',
        data: {'text': text},
        success: function(data) {
            console.log(data);
        }
    });
}

Return "an empty string"

Note: If you post a solution please include an explanation as to why you took that particular approach since I am here to learn. Thank you!

  • 写回答

2条回答 默认 最新

  • douyan1882 2017-03-23 06:30
    关注
    <input type='hidden' name='posterId' value="<?php echo $user['userId']?>" id = 'posterId'>
    
    function setComment() {
        var text = $("#post").val();
        var posterId=$('#posterId').val();
        $.ajax({
        type: 'POST', 
        url: 'setAbout.php',
        data: {'text': text, 'posterId':posterId},
        success: function(data){
            console.log(data);
        }
    });
    }
    

    you are checking for the variable posterId in your PHP script, which you have not set while doing the ajax call.

    and in the script you are just running the prepared query, you are not returning anything as response to your request. So even if the request succeeds, you will not get anything in the response to print in your console

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接