Im trying to use Post to read something from my form, but even a simple Example from https://www.w3schools.com/php/php_forms.asp doesn't work. My own code is this:
<form id="eingabe" method="post" action="../php/suche.php">
<input id="suche" name="suche" type="text" size="50px" placeholder="Suche">
<input type="submit" value="Submit">
</form>
suche.php
$rows = array();
if (isset($_POST['suche'])) {
$suche = $_POST['suche'];
$sql= "SELECT * FROM Buch WHERE titel LIKE '%" . $suche . "%' OR autor LIKE '%" . $suche ."%' OR isbn LIKE '%" . $suche ."%' OR genre LIKE '%" . $suche ."%'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while ($row=$result->fetch_assoc()) {
$titel = $row['titel'];
$autor = $row['autor'];
$isbn = $row['isbn'];
$genre = $row['genre'];
$preis = $row['preis'];
$bild = $row['image'];
$beschreibung = $row['beschreibung'];
$rows[] = $row;
}
}
}
the var_dump($_POST)
is always array(0) { }
but in the var_dump($GLOBALS)
, i can find the word i sending:
array(6) { ["HTTP_RAW_POST_DATA"]=> string(10) "suche=test" ["_GET"]=> array(0) { } ["_POST"]=> array(0) { }...
Question is:
I am using php7.1, but already tried older versions. If i change to GET it works, but i really need to use POST. I also checked my php.ini but POST is activated and has 128MB for ussage. Does anyone have an idea why Post doesnt work for me?
PS: A friend of mine is using the exact same code and it works for him perfectly, so its not the code
You can write your code like this -
$rows = array();
if($_SERVER["REQUEST_METHOD"] == "POST"){
$suche = $_POST['suche'];
$sql= "SELECT * FROM Buch WHERE titel LIKE '%" . $suche . "%' OR autor LIKE '%" . $suche ."%' OR isbn LIKE '%" . $suche ."%' OR genre LIKE '%" . $suche ."%'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while ($row=$result->fetch_assoc()) {
$titel = $row['titel'];
$autor = $row['autor'];
$isbn = $row['isbn'];
$genre = $row['genre'];
$preis = $row['preis'];
$bild = $row['image'];
$beschreibung = $row['beschreibung'];
$rows[] = $row;
}
echo "<pre>";
print_r($rows);
}
}else{
echo "POST method not working";
}
Here if your POST method will not work then you will get an message "POST method not working" and if your POST method is working then you will get your data using print_r() function.
Note - This code is only for the testing purpose, Please modify it as per your need.