weixin_33739523 2016-07-19 09:29 采纳率: 0%
浏览 50

$ _Post失败(发送Ajax)

Can someone please show me the correct way I can add these two lines of code

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

data: dataString ,

So that I can send them in a $_POST to my action.php , I have tried several ways to do this but cannot get both of them successfully to be passed on to my action_comments.php I understand I'm missing something possible when using data: below or have not correctly formatted my code . I'm a total beginner with very little experience so sorry if I lack good practice but hopefully can be better from my debugging . Thanks to anyone who helps me get passed this .

Here is complete code to give overview what Im doing

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

I have my $_POST as follows in action_comment.php

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


//DATASTRING FROM TEXTAREA  
echo $content= $_POST['content'];
  • 写回答

1条回答 默认 最新

  • csdn产品小助手 2016-07-19 09:47
    关注

    var dataString = 'content='+ textcontent;

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

    Cannot use data attribute multiple times in same ajax request. Within php file you can access like $_POST['txt_data'] to get textarea content and same way for other parameters;

    define data attribute once and pass all the data like as shown above.

    if you want to post whole form data you can use this way

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

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊