gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:45:30 26 4
gpt4 key购买 nike

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

我尝试过的

$search = $_GET['searchterm'];

然后将 $search 绑定(bind)到 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
})
}
}

最佳答案

$_GET 是一个“超全局”,无论您从哪里访问它,它都应该返回相同的结果,除非您在此过程中删除其内容。

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

Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

These superglobal variables are:

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

关于php - 如何从另一个页面获取$_GET url参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44671714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com