通过ajax使用值作为段落php发送数据

时间:2016-07-08 13:46:41

标签: php html ajax

在PHP方面我真的很缺乏经验,而且我一直在努力编写自己的“管理页面”,这是一个非常简单的“管理页面”。问题是当我将带有ajax的值从adminpage传递到索引页面时,该值不会出现在段落中。

html adminpage.php(发送ajax请求的页面):

<div class="col-lg-6">
        <textarea class="form-control textarea"  rows="5" placeholder="Tak text 1"></textarea>
        <button class="btn btn-primary text-change1">Submit</button>
        <textarea class="form-control textarea" rows="5" placeholder="Tak text 2"></textarea>
        <button class="btn btn-primary text-change2">Submit</button>
</div>

index.php,正在接收请求的页面

<?php

 $text = $_POST['text'];

?> 
<div class="desc-text">
   <p class="roof">
      <?= $text ?>
   </p>
</div>

的Ajax:

 $(".text-change1").click(function(){

 if($(this).attr("class").indexOf("text-change1") != -1){
   $.ajax({
    type:"POST",
    url:"index.php",
    data:{ text:text },
    success:function(){
    alert("message sent!");
    }, error:function(){
      alert("Something went wrong");
    }
   });
  });

这里的目标是将通过ajax发送的数据添加到上面提到的段落html中。

2 个答案:

答案 0 :(得分:2)

此:

success:function(){

您永远不会捕获PHP脚本的输出/返回值,因此不要在页面中插入任何内容。你需要更像这样的东西:

success: function(response) {
     $('#place_to_insert_response').html(response);
}

答案 1 :(得分:0)

我将文本发送到带有ajax的文本文件然后我在$(document).ready之后读取文本文件以从管理页面更新段落。