使用 jquery 从 php 获取数据,然后再次传递给 php


Get data from php using jquery then pass again to php

这是我的设置:

<div class="container">
    <ul>
        <li data-owner="93"><a href="javascript:void(0);"></a></li>
        <li data-owner="94"><a href="javascript:void(0);"></a></li>
        <li data-owner="95"><a href="javascript:void(0);"></a></li>
    </ul>
    <div class="below_list">
        <div class="popup">
            <a href="javascript:void(0);" class="popup-close">
                <img src="<?php echo get_template_directory_uri(); ?>/images/close.jpg">
            </a>
            <div class="slider-wrapper">
                <div class="slider-inner">
                    <img src="<?php echo get_template_directory_uri(); ?>/images/slider.png" />
                    <img src="<?php echo get_template_directory_uri(); ?>/images/slider2.png" />
                    <img src="<?php echo get_template_directory_uri(); ?>/images/slider3.png" />
                </div>
            </div>
            <div class="description">
                <h1 class="title"></h1>
                <h3 class="subtitle"></h3>
                <p class="description"></p>
            </div>
        </div>
    </div>
</div>

每当我单击列表项时,我都会使用:var formId = $(this).attr('data-owner'); .这将用作 ID。

然后我将不得不使用$('.title').append('<?php echo get_the_title(formId); ?>');附加到空白h1

我知道PHP只执行一次,但我不确定如何提出解决方案。

请注意,所有内容都发生在一个页面中,无需刷新。

为此

,您需要使用 ajax。您的 php 最初是在页面加载时处理的。但之后的任何事情都不会被处理。查看 http://api.jquery.com/jQuery.post/上的指南。

您需要将选定的 id 传递给另一个 php 页面(在本例中为后端.php),让它进行处理(在您的情况下获取标题),并在后端.php中回显结果。

示例代码如下所示。

$.ajax({
    type:"POST",
    url: "backend.php",
    data:"id="+formId , //add the variable you need to pass to the backend.php
    success: function(html){
        $("#response").html(data); //shows the output given in the backend.php in the response div.
    }
});

两次调用 ajax

   $.ajax({
        url: 'a.php',
        data: {name: 'xxxxxx'},
        dataType: 'jsonp',
        success: function(data){
            // do stuff
            // call next ajax function
            $.ajax({ ....call to b.php....});
        }
    });

如果您必须单独更改h1文本而不进行任何数据库更改,则可以使用

$('.container ul li').on('click',function(){
      var formId = $(this).attr('data-owner');
      $.ajax({
          type:"POST",
          url: "saveData.php",
          data:"formId="+formId ,
          success: function(data){
               $(".title").text(data); 
          }
      });
});

saveData.php

<?php

您可以使用$_POST['formId'];获取表单ID

$formId = $_POST['formId'];

编写用于从database获取相应formId数据的query

然后echo您从query结果中获得的标题,例如

echo $formTitle;

这些数据将在ajax success

希望这对你的伴侣有帮助.. :)