Not really a problem but I get a blank page when showing form on page.
The remarkable thing is in the 3th last line of the php snippet. When I put in the Else statement:
echo "<script type= 'text/javascript'>alert('Error: No message is send.');</script>";
everything works fine and I only get the message
"Error: No message is send." which is fine because no message is send. But when I replace that previous code with
and I surf to that webpage I get a blank page.
I want to go to that webpage without showing that error message all the time and without getting a blank page. How do I do that?
<?php
$hostname='localhost';
$username='myusername';
$password='mypassword';
// define variables and set to empty values
$name = $email = $message = "";
if ($_SERVER["REQUEST_METHOD"] == 'POST')
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
}
// Check fields with function test_input()
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
try {
$dbh = new PDO('mysql:host=localhost;dbname=mydatabase;charset=utf8', $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Line: ' . $e->getLine() . '<br />';
echo 'File: ' . $e->getFile() . '<br />';
echo 'Error: ' . $e->getMessage();
echo '</pre>';
}
if (strlen($name) > 0 && strlen($email) > 0 && strlen($message) > 0)
{
$name = trim($name);
$email = trim($email);
$message = trim($message);
$stmt = $dbh->prepare("INSERT INTO students (name, email, message)VALUES (:name, :email, :message)");
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':message', $message, PDO::PARAM_STR);
// set parameters and execute
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$stmt->execute();
echo "<script type= 'text/javascript'>alert('Your Message is succesfully submitted.');</script>";
}
else
{
echo "<script type= 'text/javascript'>alert('Error: No message is send.');</script>";
}
$dbh = NULL;
site:
https://www.bertverberne.com/modx/index.php?id=10
Using: MODX Revolution 2.5.7-pl
php version: 7.1