使用 ajax、php 和 jQuery 更改 DIV 内容

IT技术 php javascript jquery ajax
2021-01-28 16:11:54

我有一个 div,其中包含数据库的一些文本:

<div id="summary">Here is summary of movie</div>

和链接列表:

<a href="?id=1" class="movie">Name of movie</a>
<a href="?id=2" class="movie">Name of movie</a>
..

这个过程应该是这样的:

  1. 点击链接
  2. Ajax 使用链接的 url 通过 GET 将数据传递到 php 文件/同一页面
  3. PHP 返回字符串
  4. div改成这个字符串
6个回答
<script>

function getSummary(id)
{
   $.ajax({

     type: "GET",
     url: 'Your URL',
     data: "id=" + id, // appears as $_GET['id'] @ your backend side
     success: function(data) {
           // data is ur summary
          $('#summary').html(data);
     }

   });

}
</script>

onclick在您的列表中添加事件

<a onclick="getSummary('1')">View Text</a>
<div id="#summary">This text will be replaced when the onclick event (link is clicked) is triggered.</div>

通过注册锚点的点击事件(使用 class="movie")并使用该方法发送 AJAX 请求并替换摘要 div 的内容,您可以使用jQuery轻松实现这一点.load()

$(function() {
    $('.movie').click(function() {
        $('#summary').load(this.href);

        // it's important to return false from the click
        // handler in order to cancel the default action
        // of the link which is to redirect to the url and
        // execute the AJAX request
        return false;
    });
});
但是我需要传递一个 GET 请求,并不是每个摘要都有一个页面。有一个 php 文件可以获取电影的 ID 并从数据库中提取摘要。:-)
2021-04-07 16:11:54

试试这个

   function getmoviename(id)
   {    
     var p_url= yoururl from where you get movie name,
     jQuery.ajax({
     type: "GET",             
     url: p_url,
     data: "id=" + id,
      success: function(data) {
       $('#summary').html(data);

    }
 });    
 }

你的 html 部分是

  <a href="javascript:void(0);" class="movie" onclick="getmoviename(youridvariable)">
  Name of movie</a>

  <div id="summary">Here is summary of movie</div>

这对我有用,您不需要内联脚本:

Javascript:

    $(document).ready(function() {
    $('.showme').bind('click', function() {

        var id=$(this).attr("id");
        var num=$(this).attr("class");
        var poststr="request="+num+"&moreinfo="+id;
        $.ajax({
              url:"testme.php",
              cache:0,
              data:poststr,
              success:function(result){
                     document.getElementById("stuff").innerHTML=result;
               }
        }); 
    });
 });

HTML:

    <div class='request_1 showme' id='rating_1'>More stuff 1</div>
    <div class='request_2 showme' id='rating_2'>More stuff 2</div>
    <div class='request_3 showme' id='rating_3'>More stuff 3</div>

    <div id="stuff">Here is some stuff that will update when the links above are clicked</div>

请求被发送到 testme.php:

    header("Cache-Control: no-cache");
    header("Pragma: nocache");

    $request_id = preg_replace("/[^0-9]/","",$_REQUEST['request']);
    $request_moreinfo = preg_replace("/[^0-9]/","",$_REQUEST['moreinfo']);

    if($request_id=="1")
    {
        echo "show 1";
    }
    elseif($request_id=="2")
    {
        echo "show 2";
    }
    else
    {
        echo "show 3";
    }

jQuery.load()

$('#summary').load('ajax.php', function() {
  alert('Loaded.');
});