Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 76267

Post variables not being received after form submission

$
0
0

So I have a HTML file with the following form:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form action="pringles.php" method="post">
      First name:<br>
      <input type="text" name="firstname" value="Mickey">
      <br>
      Last name:<br>
      <input type="text" name="lastname" value="Mouse">
      <br><br>
      <input type="submit" value="Submit">
      </form>
  </body>
</html>

Then I have a PHP file names pringles.php which contains the following code:

<?php 
// server, username, password, db, port
$servername = "********";
$username = "*******";
$password = "********";
$dbname = "*********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

foreach ($_POST as $key => $value) {
  $postdata2 = $postdata2 . "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value) . "////";
}

$sql = "INSERT INTO `test`(`text`) VALUES ('{$postdata2}')";
if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>

However, when I submit the form it inserts a row into my database but the text field is empty. The post variables are not being sent for some reason. Additionally, I have another .php file where a form works perfectly. They are in the same folder and everything, therefore I don't understand why this isn't working.

Thanks!


Viewing all articles
Browse latest Browse all 76267

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>