2017-04-04 7 views
-1

search.phpPHP-Suche, aber zeigen Ergebnis in der nächsten Seite

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!DOCTYPE html> 
<html> 
<head> 
<style> 
ul { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    background-color: #333; 
} 

li { 
    float: left; 
} 

li a { 
    display: block; 
    color: white; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
} 

li a:hover { 
    background-color: #111; 
} 
</style> 
</head> 

<body> 
<?php 
    $query = $_GET['query']; 
    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM register 
      WHERE (`Username` LIKE '%".$query."%') OR (`Firstname` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       echo "<p><h3>".$results['Username']."</h3>".$results['Contactnumber']."</p>"; 
       // posts results gotten from database(title and text) you can also show id ($results['id']) 
      } 

     } 
     else{ // if there is no matching rows do following 
      echo "No results"; 
     } 

    } 
    else{ // if query length is less than minimum 
     echo "Minimum length is ".$min_length; 
    } 
?> 
</body> 

index.php

<!DOCTYPE html> 
<html> 
<head> 
<style> 
ul { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    background-color: #333; 
} 

li { 
    float: left; 
} 

li a { 
    display: block; 
    color: white; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
} 

li a:hover { 
    background-color: #111; 
} 
</style> 
</head> 
<body> 
<body bgcolor="#919191"> 

<ul> 
    <li><a class="active" href="#home">Dashboard</a></li> 
    <li><a href="#news">Add Package</a></li> 
    <li><a href="#contact">View Customer</a></li> 
    <li><a href="#about">View Order</a></li> 
</ul> 

<form action="search.php" method="GET"> 
     <input type="text" name="query" /> 
     <input type="submit" value="Search" /> 
    </form> 



</body> 
</html> 

ist das möglich Show Ergebnis auf derselben Seite anstatt zu zeigen, auf Die nächste Seite? Da mein Ergebnis leer angezeigt wird, wollte ich auf meiner Seite zeigen, wer weiß wo das Problem zu beheben ist? Ich habe versucht hinzuzufügen, nicht zu helfen.

+2

merge search.php Code in index.php wird das Problem –

+2

vermeiden Verwendung von mysql function() lösen, da diese jetzt in PHP 7 –

Antwort

0

Was Sie suchen, ist AJAX (Asynchronous JavaScript and XML). Es ermöglicht Ihnen, eine Backdoor-Anfrage zu machen, während die aktuelle Seite tatsächlich geladen ist.

Es ist wie Ihr Gespräch mit jemandem und dann schnell etwas flüstern zu einem anderen Mann und der erste Mann nicht weiß, über diese. In diesem Beispiel sind Sie der Browser, der erste Typ ist Sie als Person (Client) und der dritte ist der Webserver.

über AJAX So ist es möglich, Daten zu einer PHP-Datei zu senden und eine Antwort von ihm zu bekommen. Bitte schau es dir selbst an.

1

Nun, ziemlich einfach, es gibt keine Möglichkeit, es in PHP zu tun ... Sie müssen AJAX verwenden. Mein Freund machte eine große AJAX-Bibliothek Sie auf Github finden:

https://github.com/PDKnight/XXHR

Dort können Sie die Grundlagen lernen und Ihre Suche Arbeit machen, wie Sie wollen!

ODER

Sie können es mit Website Refresh tun, das sieht nicht gut aus, aber was auch immer ...

  1. Ändern Sie die Form Aktion auf "" (Ja, lassen Sie es leer)
  2. Fügen Sie Ihre PHP
  3. Rufen Sie Ihre PHP nur auf index.php wenn einreichen genannt wurde:

    if ($ _ POST [ "Eintragen"]) {// Ihre php es }

So könnte es wie so arbeiten .. Benutzer die Seite betritt, passiert nichts, dann Werte, Klicks zu bilden Eingänge Senden Sie, die Site zu aktualisieren verursacht, und sehen Sie dann die Ergebnisse, weil PHP weiß, dass er die Übermittlung gedrückt hat. Einfaches Peasy Zitrone squeze ...

0

es ist sehr einfach, nur search.php von Formular-Tag entfernen.

<?php 
    $query = $_GET['query']; 
    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM register 
      WHERE (`Username` LIKE '%".$query."%') OR (`Firstname` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       echo "<p><h3>".$results['Username']."</h3>".$results['Contactnumber']."</p>"; 
       // posts results gotten from database(title and text) you can also show id ($results['id']) 
      } 

     } 
     else{ // if there is no matching rows do following 
      echo "No results"; 
     } 

    } 
    else{ // if query length is less than minimum 
     echo "Minimum length is ".$min_length; 
    } 
?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
    ul { 
     list-style-type: none; 
     margin: 0; 
     padding: 0; 
     overflow: hidden; 
     background-color: #333; 
    } 

    li { 
     float: left; 
    } 

    li a { 
     display: block; 
     color: white; 
     text-align: center; 
     padding: 14px 16px; 
     text-decoration: none; 
    } 

    li a:hover { 
     background-color: #111; 
    } 
    </style> 
    </head> 
    <body> 
    <body bgcolor="#919191"> 

    <ul> 
     <li><a class="active" href="#home">Dashboard</a></li> 
     <li><a href="#news">Add Package</a></li> 
     <li><a href="#contact">View Customer</a></li> 
     <li><a href="#about">View Order</a></li> 
    </ul> 

    <form action="" method="GET"> 
      <input type="text" name="query" /> 
      <input type="submit" value="Search" /> 
     </form> 



    </body> 
    </html> 
+0

Hinweise sind veraltet: Undefined index: Abfrage in D: \ xampp \ htdocs \ adminsearch1.php in Zeile 2 – thenewsboy

Verwandte Themen