We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44891
    • 35 Posts
    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
    exit;
    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
      • 4172
      • 5,888 Posts
      With exit, you will exit MODX, too.

      you should collect your output and return everything at the end of your snippet.
      Don't echo your output.

      I would use formit and a formit-hook, for exampe the formit2db - hook.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44891
        • 35 Posts
        Quote from: Bruno17 at Nov 21, 2017, 07:15 AM
        With exit, you will exit MODX, too.

        you should collect your output and return everything at the end of your snippet.
        Don't echo your output.

        I would use formit and a formit-hook, for exampe the formit2db - hook.

        Thanks Bruno, appreciate your respons. I never explored Formit and Formit2db thoroughly. Just scratched the surface. For now I'll just remove the Else part:

        else
                {
                    echo "<script type= 'text/javascript'>alert('Error: No message is send.');</script>";
                }
        


        That will do for the moment.