2015-05-31 50 views
0

所以我是新来的ajax,我面临一个问题,从我的窗体获取所有值。传递ajax数组到数据:

所以我想插入名称reply_txt和$ NewSID的,以我的表

HTML表单:

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

AJAX:我思我必须在某种方式传递一个数组中的数据: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:错误是不确定的变量$ 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')"); 
} 
+0

你不是每个人都在php –

+0

中每个出了什么东西?什么?我只想插入到我的表中,但我需要它在数据中:我认为 – user3426191

+0

对不起,我用.each()编程,所以我对此感到困惑..我的意思是回声出来..你应该在PHP中回应出来的东西获得成功的反应功能 –

回答

2

实际上,你可以通过一个JS对象Ajax调用的data属性:

$(".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); 
     } 
    }); 
}); 

另外我希望response.php中的代码仅用于演示/测试! 在SQL语句中使用未经转义的$ _POST值是非常危险的

+0

谢谢,我要去尝试一下,是的,现在只是测试。当它工作时,我会将其更改为PDO作为项目的其余代码 – user3426191

+0

它的工作,谢谢:) – user3426191

2

您只发送AJAX数据中的reply_txt值。您需要添加replyToPost字段的值过:

var replyData = 'reply_txt=' + $(".replyText").val() + '&replyToPost=' + $('input[name="replyToPost"]').val(); 

或者(最好),你可以使用serialize()让jQuery将自动为您创建一个序列化的字符串:

$(".replySubmit").click(function(event){ 
    event.preventDefault(); //prevent action from button 
    $.ajax({ 
     type: "POST", // POST type 
     url: "response.php", //call ajax on page 
     dataType: "text", //type of data html/ajax/jason ... 
     data: $(this).serialize(), //Form variables intups 
     success:function(response){ 
      $(".respondsReply").append(response); 
      $(".replyText").val(''); //empty text field on successful 
     }, 
     error:function (xhr, ajaxOptions, thrownError){ 
      alert(thrownError); 
     } 
    }); 
});