简体   繁体   English

使用jQuery发布HTML内容的问题 - Ajax Post方法

[英]Issue on Posting HTML Content Using jQuery - Ajax Post Method

I am trying to POST a div section to a new page using jquery ajax as: 我正在尝试使用jquery ajax将div部分发布到新页面:

<!DOCTYPE html>
<body>
  <div id="edit_content">
    <p>This is a test</p>    
   </div>
  <a href="out.php" id="submit_script">Submit</a>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
   $( document ).ready(function() {
          var htmlData = $('#edit_content').html();
         $.post('out.php', {'html': htmlData },function(){
      });
   });
</script>
</body>
</html>

and in the out.php page I have: out.php页面中我有:

<?php
$content = $_POST['html'];
echo $content;

but when I run the code I am getting this error: 但是当我运行代码时,我收到此错误:

Notice: Undefined index: html in G:\\Apps\\out.php on line 2 注意:未定义的索引:第2行的G:\\ Apps \\ out.php中的html

can you please let me know why this is happening and how I can stop it? 能不能让我知道为什么会这样,我怎么能阻止它?

Your $.post function has a typo in the data field, try this instead: 您的$.post函数在数据字段中有拼写错误,请尝试以下操作:

$.post('out.php', {html: htmlData }, function(response){

Since you are sending an object, the key does not need quotes. 由于您要发送对象,因此该密钥不需要引号。

or better yet, but all your data ouside and juste reference them in your post: 或者更好,但是你的所有数据都在你的帖子中引用它们:

var postData = {html: $('#edit_content').html() }
$.post('out.php', postData, function(response){

Your code works as it is - at least in terms of the post. 您的代码按原样运行 - 至少就帖子而言。 To illustrate this change it to the following (the only change is to actuallly do something with the response to the ajax request): 为了说明这一点,将其更改为以下内容(唯一的变化是实际上对ajax请求的响应做了一些事情):

<!DOCTYPE html>
<body>
  <div id="edit_content">
    <p>This is a test</p>    
   </div>
  <a href="out.php" id="submit_script">Submit</a>

  <div id="out"></div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
  $( document ).ready(function() {
        var htmlData = $('#edit_content').html();
        $.post('out.php', {'html': htmlData }, function(data){  
            $('#out').html(data);
        });
  });
</script>
</body>
</html>

I think you need to explain what you are trying to do (or what you are expecting to see). 我认为你需要解释你想要做什么(或者你期望看到的)。 As your code stands you are running the ajax request as soon as the page loads (in document ready) but not doing anything with the response. 正如您的代码所示,您一旦页面加载(在文档就绪)但是没有对响应做任何事情,就会运行ajax请求。 You then have a link to the out.php ( <a href="out.php" id="submit_script">Submit</a> ). 然后,您有一个指向out.php的链接( <a href="out.php" id="submit_script">Submit</a> )。 Are you expecting to see the result in out.php when you click the link? 当您点击链接时,您是否希望在out.php中看到结果? If so then that isn't going to happen. 如果是这样,那就不会发生。 What is happening is that when the page loads it runs a request to out.php with the post data and gets a response (which it then ignores). 发生的事情是,当页面加载时,它会使用post数据向out.php运行请求并获得响应(然后忽略)。 When you click the link you run a new request to out.php without the post data so you see nothing. 当您单击该链接时,您会在没有发布数据的情况下向out.php运行新请求 ,因此您什么都看不到。

If I have guessed right then you want to replace the link with a form submission triggered by the click of the link (with the data fetched first). 如果我猜对了,那么你想用点击链接触发的表单提交替换链接(首先获取数据)。 Something like 就像是

<!DOCTYPE html>
<body>
  <div id="edit_content">
    <p>This is a test</p>    
   </div>
  <a href="#" id="submit_script">Submit</a>

  <form action="out.php" method="post" id="out-form"  style="display: none;">
    <input type="hidden" id="hidden-html" name="html" value="" />
  </form>  

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
  $( document ).ready(function() {
        $('#submit_script').click(function(){
            $('#hidden-html').val($('#edit_content').html());
            $('#out-form').submit();
            return false;
        })
  });
</script>
</body>
</html>

The 'html' data is not being posted to your php page. 'html'数据未发布到您的php页面。 Change your php file to this: 将您的php文件更改为:

<?php
  if(isset($_POST['html']))
  {
    $content = $_POST['html'];
    echo $content;
  }
?>

This should stop the error and at least point you in the right direction. 这应该可以阻止错误并至少指向正确的方向。 Without more code, I am not able to tell you why the 'html' data is not being posted. 如果没有更多代码,我无法告诉您为什么没有发布'html'数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM