简体   繁体   中英

how to get the $_GET url parameters from another page

I have fetch.php and index.php, fetch php is used to dynamically load content from the database along with ajax script into index.php, I want to filter the results it loads based on the search term if it exists.

What I've tried

$search = $_GET['searchterm'];

then binding $search to the sql query but I have two problems:

1- it tries to get the _GET parameters from fetch.php while the parameter is on index.php

2- it shows an error when the query is used to load all the results without any search term input

<?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>";
}
?>

and below is 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>

also 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 is a "superglobal" and no matter from where you access it, it should return the same results, unless you wipe its content somewhere during the process.

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 can be used to collect form data after submitting an HTML form with method="get".

$_GET can collect data sent in the URL.

Assuming the URL has something like this http://example.com/?searchterm=Hello .

Then you can access by $search = $_GET['searchterm']; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM