如何从另一个页面获取$ _GET url参数

时间:2017-06-21 09:13:33

标签: php mysql

我有fetch.php和index.php,fetch php用于动态地将数据库中的内容和ajax脚本一起加载到index.php中,我想根据搜索词(如果存在的话)过滤它加载的结果。

我尝试了什么

$search = $_GET['searchterm'];

然后将$ search绑定到sql查询,但我有两个问题:

1-当参数在index.php上时,它试图从fetch.php获取_GET参数

2-当查询用于加载所有结果而没有任何搜索词输入时,它显示错误

<?php
include("config.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
    header('HTTP/1.1 500 Invalid page number!');
    exit();
}
//get current starting point of records
$position = (($page_number-1) * $item_per_page);

//fetch records using page position and item per page.
$results = $mysqli->prepare("SELECT name, location, score, img, id, type FROM artists "
if(isset($_GET['searchterm'])){

}
" ORDER BY score DESC LIMIT ?, ?");

//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
//for more info https://www.sanwebe.com/2013/03/basic-php-mysqli-usage
$results->bind_param("ii", $position, $item_per_page);
$results->execute(); //Execute prepared Query
$results->bind_result($name, $location, $score, $img, $id, $type); //bind variables to prepared statement

//output results from database

while($results->fetch()){ //fetch values
  echo  "<div class=\"card feed_item\" style=\"width: 20rem;\"><a id=\"wraplink\" href=\"profile.php?id=". $id."\">";
  echo  "<img class=\"card-img-top feed_img\" src=\"img/" . $img . ".jpg\" alt=\"Card image cap\">";
  echo  "<div class=\"card-block\">";
  echo  "<h4 class=\"card-title\">" . $name . "</h4>";
  echo  "<p class=\"card-text\">" . $location . "</p>";
  echo  "</div><div class=\"card-footer\">";
  echo  "<small class=\"text-muted\">". $type ."</small>";
  echo  "</div>";
  echo  "</a></div>";
}
?>

以下是index.php

<?php
// including the config file
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Setch</title>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div id="wrappy">

  <!-- navbar -->
  <nav class="navbar navbar-toggleable-sm navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" id="logo" href="index.php">Setch</a>
  <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
  <ul class="navbar-nav mr-auto mt-2 mt-md-0">
    <li class="nav-item active">
      <a class="nav-link" href="#">Add Artist <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Feedback</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">About</a>
    </li>
  </ul>
  <form class="form-inline my-2 my-lg-0 input-group" action="search.php" method="get">
    <input class="form-control mr-sm-2" type="text" name="searchterm" placeholder="Search" id="search">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="search">Search</button>
  </form>
  <div id="back_result"></div>
  </div>
  </nav>
<div class="container">
<!-- buttons -->
<br>
<div class="row justify-content-md-center">
  <div class="col col-lg-3 justify-content-md-center"><button id="ph" type="button" class="btn btn-secondary btn-lg">Photographers</button></div>
  <div class="col col-lg-3 justify-content-md-center"><button id="mk" type="button" class="btn btn-secondary btn-lg">Makeup Artists</button></div>
</div></div>

<!-- cards -->
<div id="feed">

<!-- artists are here -->
</div>
<!-- content -->
<div class="loading"><img src="img/load.gif" />
</div><!-- container -->
</div>
<script src="js/script.js"> </script>
</body>
</html>

也是script.js

var track_page = 1; //track user scroll as page number, right now page number is 1
var loading  = false; //prevents multiple loads
var fetch = 'fetch_pages.php';
load_contents(track_page); //initial content load

$(window).scroll(function() { //detect page scroll
    if($(window).scrollTop() + $(window).height() >= $(document).height()) { //if user scrolled to bottom of the page
        track_page++; //page number increment
        load_contents(track_page); //load content
    }
});
//Ajax load function
function load_contents(track_page){
    if(loading == false){
        loading = true;  //set loading flag on
        $('.loading').show(); //show loading animation
        $.post( fetch, {'page': track_page}, function(data){
            loading = false; //set loading flag off once the content is loaded
            if(data.trim().length == 0){
                //notify user if nothing to load
                $('.loading').html("");
                return;
            }
            $('.loading').hide(); //hide loading animation once data is received
            $("#feed").append(data); //append data into #results element

        }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
            alert(thrownError); //alert with HTTP error
        })
    }
}

2 个答案:

答案 0 :(得分:1)

$ _ GET是一个“超全球”,无论你从哪里访问它,它都应该返回相同的结果,除非你在这个过程中的某个地方擦除它的内容。

http://php.net/manual/en/language.variables.superglobals.php

  

PHP中的几个预定义变量是“superglobals”,这意味着   它们在整个脚本的所有范围内都可用。没有必要   做全局$变量;在函数或方法中访问它们。

     

这些超全球变量是:

$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV

答案 1 :(得分:0)

在使用method =&#34; get&#34;提交HTML表单后,可以使用PHP $_GET来收集表单数据。

$_GET可以收集网址中发送的数据。

假设网址类似于此http://example.com/?searchterm=Hello

然后您可以$search = $_GET['searchterm'];访问。