$ _Post失败(发送Ajax)

时间:2016-07-19 09:29:36

标签: php jquery ajax

有人可以告诉我正确的方法,我可以添加这两行代码

data: {name: info, me: '<?php echo $me; ?>'},

data: dataString ,

所以我可以用$ _POST将它们发送到我的action.php,我已经尝试了几种方法来做到这一点,但是无法将它们成功传递给我的action_comments.php我理解我是使用数据时遗漏了一些东西:下面或没有正确格式化我的代码。我是一个初学者,经验很少,很抱歉,如果我缺乏良好的练习,但希望我的调试能更好。感谢任何帮助我通过这个的人。

这是完整的代码,用于概述我在做什么

<script type="text/javascript">

$(function() {  
 // call the submit button ref: name
$(".submit_button").click(function() { 
declare textcontent 
var textcontent = $("#content").val(); 
//dataString = 'content' globel var
var dataString = 'content='+ textcontent; 
declare info 
var info = $('#name').val();
// option no text inputted
if(textcontent=='')
{
// show message if no text 
alert("Enter some text..");
$("#content").focus();
}
else
{       
//display text animation loading
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
//var info equals the string

var info = $('#content').val();
//start ajax call
$.ajax({    
type: "POST",
//path to my action.php
url: "actions/action_comment.php",

//Need to undestand how to correctly format these lines so 
//they are both succesful when submitted to my action_comment.php
$me is declared (not-shown here it holds integer)
data: {name: info, me: '<?php echo $me; ?>'},

// pass the string from textarea to $_POST
data: dataString ,
// I can get one or the other to work but not both

cache: true,

// feed the success my data
success: function(html){    

$("#show").after(html);

document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();  

}  
});
}

return false;
});
});

</script>

我在action_comment.php中有我的$ _POST,如下所示

echo $me = $_POST['me'];


//DATASTRING FROM TEXTAREA  
echo $content= $_POST['content'];

1 个答案:

答案 0 :(得分:1)

var dataString ='content ='+ textcontent;

$.ajax({    
type: "POST",
url: "actions/action_comment.php",
data: {name: info, me: '<?php echo $me; ?>',txt_data: dataString},
....
});

在同一个ajax请求中不能多次使用数据属性。在php文件中你可以访问$ _POST ['txt_data']来获取textarea内容和其他参数的相同方式;

定义一次数据属性并传递所有数据,如上所示。

如果您想发布整个表单数据,可以使用这种方式

var form = $('#my_form');
  $.ajax( {
      type: "POST",
      url:  form.attr( 'action' ),
      data: form.serialize(),
      ..
      ..
      });
相关问题