2017-06-12 37 views
0

有4个不同的页面。 product-listing.php,product-single-page.php,product-lis.js和action.php。 当我在product-single-page.php中搜索某些东西时,product-lis.js正在接收数据,通过ajax将searchred项的关键字发送到action.php,并且在成功时它必须显示结果产品列表页面有Id(get_product)。但它并未在产品列表页面中显示其结果。我需要在点击搜索按钮后在产品列表页面中显示搜索结果。显示结果在ajax的成功功能另一页的划分

代码

product-listing.php 

<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 
     <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
     <script src="bootstrap/js/jquery.min.js" ></script> 
     <script src="bootstrap/js/bootstrap.min.js" ></script> 
     <script src="test.js" ></script> 
    </head> 
    <body> 
    <div class="container"> 
     <div class="col-md-12"> 
      <div id="get_something"></div> 
     </div> 
    </div> 
    </body> 
</html> 

product-single-page.php 

<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
      <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> 
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
      <script src="bootstrap/js/jquery.min.js" ></script> 
      <script src="bootstrap/js/bootstrap.min.js" ></script> 
      <script src="test.js" ></script> 
    </head> 
    <body> 
     <div class="col-md-12"> 
      <input type="text" placeholder="I'm looking for..." class="for-control" name="name" id="name" autocomplete="off" value=""> 
      <button type="submit" class="btn btn-primary" id="search_btn" name="submit">GO</button> 
     </div> 
    </body> 
</html> 


product-lis.js 

$(document).ready(function() 
{ 
    $("body").delegate("#search_btn","click",function(event) 
    { 
    event.preventDefault(); 
    var keyword = $("#name").val(); 
    var id = $('#name').val(); 
    $.ajax({ 
    url: "action.php", 
    method: "POST", 
    data : { search : 1 , id : id}, 
    success:function(data) 
     { 
     $("#get_something").html(data); 
     } 
    })  
    }); 
}) 


action.php` 

<?php 
if(isset($_POST['search'])) 
{ 
    $val=$_POST['id']; 
    ?> 
    <h1><?php echo $val; ?></h1> 
    <?php 
} 
?> 
+0

**解析错误,意外';'(;)!**你的php脚本有错误 –

+0

为了得到更好的答案,你应该问好问题。正确设置代码格式,突出显示主要问题等...您应该检查格式化问题的提示。 –

回答

0

你没有其他页面的访问DIV直接
,因为它有定义的另一个页面,以便其在同一个页面无法访问您进入另一个页面DIV那不是可能

但您可以使用您的ajax response重定向到该页面,并将响应数据设置为不同页面的特定div

试试这一个

1)产品listing.php

<?php 
[email protected]$_REQUEST['data']; 

?> 

<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 
     <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
     <script src="bootstrap/js/jquery.min.js" ></script> 
     <script src="bootstrap/js/bootstrap.min.js" ></script> 
     <script src="test.js" ></script> 
    </head> 
    <body> 
    <div class="container"> 
     <div class="col-md-12"> 
      <div id="get_something"><?=$data?></div> 
     </div> 
    </div> 
    </body> 
</html> 

2)产物单page.php文件

<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
      <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> 
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
      <script src="bootstrap/js/jquery.min.js" ></script> 
      <script src="bootstrap/js/bootstrap.min.js" ></script> 
      <script src="test.js" ></script> 
    </head> 
    <body> 
     <div class="col-md-12"> 
      <input type="text" placeholder="I'm looking for..." class="for-control" name="name" id="name" autocomplete="off" value=""> 
      <button type="submit" class="btn btn-primary" id="search_btn" name="submit">GO</button> 
     </div> 
    </body> 
</html> 

<script type="text/javascript" src="../../library/jquery-3.2.1.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() 
{ 
    $("body").delegate("#search_btn","click",function(event) 
    { 
    event.preventDefault(); 
    var id = $('#name').val(); 
    $.ajax({ 
    url: "action.php", 
    method: "POST", 
    data : { search : 1 , id : id}, 
    success:function(data) 
     { 
      location.href="product-listing.php?data="+data; 
     } 
    });  
    }); 
}) 

</script> 

3)action.php的

<?php 
if(isset($_POST['search'])) 
{ 
    $val=$_POST['id']; 
    ?> 
    <h1><?php echo $val; ?></h1> 
    <?php 
} 
?>