简体   繁体   中英

Form Validation page with php

I'm trying to create a simple page where a user is able to see his formatted input (the way it would look like in the email that's supposed to be send) after he clicked on "Submit" on the previous HTML form.

As I also want to use this site with the formatted form data as a final "manual validation" point for 6 different forms, the eMail with all data from the form should only be sent if the user clicks on the submit button on this site only.

I have no trouble displaying the user input on this site, however, it seems that I have a scoping problem with the variables.

I'm running the if statement of the submit button right after the if that is displaying the user input from the previous site. However, even if declaring $content as global it still stays empty. Maybe that's also because the other if statement didn't run this time.

Is there a way to use $content in the other if statement as well? Or is there a prettier solution?

I'm quite new to php and this is my first php project I ever tried, so some input is really appreciated.

<?php
require ('/scripts/PHPMailer-master/PHPMailerAutoload.php');

if (isset($_POST['new_mailbox']))
{
    // new Mail Form Data
    $Name_Applicant                     = $_POST['Name_Applicant'];
    $FirstName_Applicant                = $_POST['First_Name_Applicant'];
    $email_Applicant                    = $_POST['email_Applicant'];
    $Telephone_Applicant                = $_POST['Telephone_Applicant'];

    $head = "<h1>Please check your data before submitting the request</h1>";
    $part_one = "
        <table class=\"pdf_table\" border=\"1\">
        <tr><td class=\"table_header\" colspan=\"2\"><b><u>Applicant</u></b></td></tr>
        <tr class=\"odd\"><td class=\"first\">Name:</td><td>".$Name_Applicant."</td></tr>
        <tr class=\"even\"><td class=\"first\">First Name:</td><td>".$FirstName_Applicant."</td></tr>
        <tr class=\"odd\"><td class=\"first\">eMail:</td><td>".$email_Applicant."</td></tr>
        <tr class=\"even\"><td class=\"first\">Telephone Number:</td><td>".$Telephone_Applicant."</td></tr>
        </table>";
$part_two = "";

echo $head;
    if (isset($part_one)){$content.=$part_one;}
    if (isset($part_two)){$content.=$part_two;}
    if (isset($part_three)){$content.=$part_three;}
    if (isset($part_four)){$content.=$part_four;}
    if (isset($content)){echo $content;}

echo "
<form method=\"POST\">
    <input name=\"form_eMail8\" type=\"hidden\">
    <input name=\"form_OK\" type=\"submit\" value=\"OK\">
</form>
";
}

if(isset($_POST["form_OK"]))
    {
        $CSS = file_get_contents('form.css');
        $html = '<style>'.$CSS.'</style>';          
        $html .= "{$content}";  
}
?>

I think you are correct when you say:

Maybe that's also because the other if statement didn't run this time.

You need to execute the code that populates $content so it runs in both cases.

Perhaps something like this:

<?php
require ('/scripts/PHPMailer-master/PHPMailerAutoload.php');

// new Mail Form Data
$Name_Applicant                     = $_POST['Name_Applicant'];
$FirstName_Applicant                = $_POST['First_Name_Applicant'];
$email_Applicant                    = $_POST['email_Applicant'];
$Telephone_Applicant                = $_POST['Telephone_Applicant'];

$head = "<h1>Please check your data before submitting the request</h1>";
$part_one = "
    <table class=\"pdf_table\" border=\"1\">
    <tr><td class=\"table_header\" colspan=\"2\"><b><u>Applicant</u></b></td></tr>
    <tr class=\"odd\"><td class=\"first\">Name:</td><td>".$Name_Applicant."</td></tr>
    <tr class=\"even\"><td class=\"first\">First Name:</td><td>".$FirstName_Applicant."</td></tr>
    <tr class=\"odd\"><td class=\"first\">eMail:</td><td>".$email_Applicant."</td></tr>
    <tr class=\"even\"><td class=\"first\">Telephone Number:</td><td>".$Telephone_Applicant."</td></tr>
    </table>";
$part_two = "";

if (isset($part_one)){$content.=$part_one;}
if (isset($part_two)){$content.=$part_two;}
if (isset($part_three)){$content.=$part_three;}
if (isset($part_four)){$content.=$part_four;}
if (isset($content)){echo $content;}

if (isset($_POST['new_mailbox']))
{
echo $head;

echo "
<form method=\"POST\">
    <input name=\"form_eMail8\" type=\"hidden\">
    <input name=\"form_OK\" type=\"submit\" value=\"OK\">
</form>
";
}

if(isset($_POST["form_OK"]))
{
    $CSS = file_get_contents('form.css');
    $html = '<style>'.$CSS.'</style>';          
    $html .= "{$content}";  
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM