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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?