2016-07-28 6 views
0

Ich möchte Ajax nur in den div (#usersDiv) istWie verhindert man, dass Ajax die unnötigen Elemente wiederholt lädt? wiederholt

Wenn Wähler in ‚Körper‘ geändert anwenden es die ganze Seite geladen wird. (Das Feld kann nicht eingegeben werden.)

Wenn der Selektor jedoch als #userDiv geändert wird, wird das Suchfeld zweimal auf der Seite angezeigt. In der ersten Box kann getippt werden, aber wieder wird die zweite Box immer wieder geladen.

PHP Datei ist wie folgt (test.php)

<?php 
    $connection = mysqli_connect('localhost', 'root', '', 'users'); 

    function users($connection){ 
     if(!empty($_POST)){ 
      $country = $_POST['userCountry']; 

      $sql = "SELECT * FROM users WHERE country = '$country' "; 
      $result = mysqli_query($connection, $sql); 

      if (mysqli_num_rows($result) > 0) { 
       while ($row = mysqli_fetch_assoc($result)) { 
        $userName = $row['username']; 
        $city = $row['city']; 

        echo '<div><h4>'. $userName. " ". $city. '</h4></div>'; 
       } 
      } else { 
       echo "Use search box!"; 
      } 
     } else { 
      echo "Use Search Box!"; 
     } 
    } 
?> 

<html> 
<head><script src = "jquery.min.js"></script> 
     <script> 
      $(document).ready(function(){ 
      $.getJSON("http://freegeoip.net/json/", function(data) { 
       var country = data.country_name; 
       $.ajax({ 
        method:"POST", 
        url:"test.php", 
        data:{userCountry:country}, 
        success:function(result){ 
         $('#usersDiv').html(result); 
        } 
       }); 
       }); 
      }); 
     </script> 
</head> 

<body> 
    <form name = "searchForm" action = "search.php" method = "POST"> 
     <input type = "text" name = "searchPlace" required /> 
     <input type = "submit" value = "Search"/> 
    </form> 
    <div id = "usersDiv"> <?php users($connection); ?> </div> 
</body> 
<html/> 
+0

Erforderliche Lektüre: [Wie kann ich SQL-Injektion in PHP verhindern?] (Https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

+0

Sie müssen wickle deinen PHP-Code in einen 'if ($ _POST)' -Wrapper, sonst lädt er die gesamte Seite bei jedem Ajax-Aufruf – MikeF

+0

@MikeF - meinst du, eine 'POST'-Ajax-Anfrage wird keine' $ _POST'-Variable haben? –

Antwort

0

ich Ihren Code geändert haben Ihre PHP-Funktion innerhalb einer if($_POST) wickeln die gesamte Seite Lade

<?php 
$connection = mysqli_connect('localhost', 'root', '', 'users'); 
if($_POST){ // Check if form has been submitted 
    $country = $_POST['userCountry']; 

    $sql = "SELECT * FROM users WHERE country = '$country' "; 
    $result = mysqli_query($connection, $sql); 

    if (mysqli_num_rows($result) > 0) { 
     while ($row = mysqli_fetch_assoc($result)) { 
      $userName = $row['username']; 
      $city = $row['city']; 

      echo '<div><h4>'. $userName. " ". $city. '</h4></div>'; 
     } 
    } else { 
     echo "Use search box!"; 
    } 
}else{ // If it hasn't then show the search form 
?> 

<html> 
<head><script src = "jquery.min.js"></script> 
    <script> 
     $(document).ready(function(){ 
     $("#searchForm").on("submit",function(e){ // Check for form submission 
      $.getJSON("http://freegeoip.net/json/", function(data) { 
        var country = data.country_name; 
        $.ajax({ 
         method:"POST", 
         url:"test.php", 
         data:{userCountry:country}, 
         success:function(result){ 
          $('#usersDiv').html(result); 
         } 
        }); 
       }); 
      }); 
     }); 
    </script> 
</head> 

<body> 
<form name = "searchForm" action = "search.php" method = "POST" id="searchForm"> 
    <input type = "text" name = "searchPlace" required /> 
    <input type = "submit" value = "Search"/> 
</form> 
<div id = "usersDiv"></div> 
</body> 
<html/> 
<?php } ?> 

Wie zu verhindern Alexander schlägt vor, lesen Sie SQL Injection

How can I prevent SQL injection

+0

Vielen Dank für die Antwort. Aber ich brauche das Suchfeld, um immer auf der Seite zu erscheinen. – AnushkaM