dongwei2882 2015-05-31 15:57
浏览 32
已采纳

将ajax数组传递给数据:

so i'm new to ajax and i'm facing a problem getting all the values from my form.

So i wanna insert the name reply_txt and the $ newsId to my table

html form:

<form>
   <input type="hidden" name="replyToPost" value="<?php echo $newsId;?>">
   <textarea name="reply_txt" class="replyText"></textarea>
   <button class="replySubmit">Answer</button> 
</form>

ajax: Ithink i have to pass an array in somehow into the data:replyData

$(".replySubmit").click(function(event){
        event.preventDefault();//prevent action from button

        var replyData = 'reply_txt='+ $(".replyText").val(); //build a post data structure

        $.ajax({
            type: "POST", // POST type
            url: "response.php", //call ajax on page
            dataType:"text", //type of data html/ajax/jason ...
            data:replyData, //Form variables intups

            success:function(response){
                $(".respondsReply").append(response);
                $(".replyText").val(''); //empty text field on successful
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
});

response.php: The error is undefined variable $newsId

if(isset($_POST["reply_txt"]) && strlen($_POST["reply_txt"])>0){
   $replyToSave = filter_var($_POST["reply_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
   $newsId = $_POST["replyToPost"]; 

$userId = $_SESSION['userId'];
$reply_row = $db->query("INSERT INTO replyPost(message,newsId_fk,userId_fk) VALUES('$replyToSave','$newsId','$userId')");
}
  • 写回答

2条回答 默认 最新

  • dougua3705 2015-05-31 16:10
    关注

    You can actually pass a JS object to the data attribute of the ajax call:

    $(".replySubmit").click(function(event){
        event.preventDefault();//prevent action from button
    
        // Build the data object for ajax request:
        var replyData = {
          "reply_txt" : $(".replyText").val(),
          "replyToPost": $("input[name=replyToPost]").val()
        };
    
        $.ajax({
            type: "POST", // POST type
            url: "response.php", //call ajax on page
            dataType: "text", //type of data html/ajax/jason ...
            data: replyData, //data object
    
            success: function(response){
                $(".respondsReply").append(response);
                $(".replyText").val(''); //empty text field on successful
            },
            error: function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
    });
    

    Also I hope that the code in response.php is only for demonstration/testing! Using un-escaped $_POST values in the SQL statement is EXTREMLY dangerous

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分