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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?