使用PHP的POST表单数据


POST Form Data using PHP

请注意,这个问题没有重复,我已经搜索了现有的相关问题。嗨,我有一个REST API,我将使用它发布表单中的数据。我不想刷新页面(我已经看到了答案)。我的表格是:

        <form class="t1-form tweet-form
        condensed
        is-autogrow
        "
      method="post"
        data-condensed-text="Compose new Tweet...lll"
      action="/finals/post.php"
      enctype="multipart/form-data">
  <span class="inline-reply-caret">
    <span class="caret-inner"></span>
  </span>
  <div class="tweet-content">

    <div aria-labelledby="tweet-box-mini-home-profile-label"id="tweet-box-mini-home-profile" class="tweet-box rich-editor " contenteditable="true" spellcheck="true" role="textbox"
      aria-multiline="true">

    </div> 
      <input type="submit" value="                   Add Another Note                   " style="float: right;background-color:White;">
    <div class="rich-normalizer"></div>
  </div>
</form>

我知道我可以在这里使用文本框,但当前的div与一些第三方css配合得很好,所以我不想更改任何布局。将要发布的内容写入的部门:

<div aria-labelledby="tweet-box-mini-home-profile-label"id="tweet-box-mini-home-profile" class="tweet-box rich-editor " contenteditable="true" spellcheck="true" role="textbox"
  aria-multiline="true">


但是它没有任何名称可以通过PHP中的$_POST["name"]访问?如何在这个div中写入内容?然后调用像post.php这样的php脚本,而不刷新页面,并且在调用post.php之后,div内容应该重置。非常感谢你的帮助。

相同问题的更简单版本:http://jsfiddle.net/davidThomas/682pG/我想使用div(Only thediv)从用户那里获取输入,并将该输入用于PHP的post请求。

需要注意的是,当您单击类型为"提交"的按钮时,它总是会触发回发。。

如果您想在不刷新页面的情况下执行此操作。您需要使用ajax并防止按钮的默认行为

 <input type="submit" id='btnSubmit' value="Add Another Note" style="float: right;background-color:White;">

编写脚本

$( "#btnSubmit" ).click(function( event ) {
event.preventDefault();
var valueOfDiv = $("#divThatYouCanWriteStuffIn").text();
$.ajax({
    url : 'yourphp.php',
    type: 'post',
    data: {content: valueOfDiv},
    success: function(data){
       // do something on the response
    }
});
});

PHP代码

$content = $_POST['content'];
// process it
// echo your response

未测试,但逻辑可能有效:

1) 使用以下方法之一获取div的内容,可以是html,也可以是文本:

$("#divThatYouCanWriteStuffIn").html();

$("#divThatYouCanWriteStuffIn").text();

2) 一旦单击提交按钮,就将其作为隐藏参数注入表单中,或者使用ajax发送具有从上述方法检索到的值的请求。类似于:

$.post( "your-rest-uri.php", {'yourdivcontent': $("#divThatYouCanWriteStuffIn").html()} );

3) 然后,您可以在PHP中使用:来访问它

$_POST["yourdivcontent"]