简体   繁体   中英

Mixing up php and html files

Right now I have 2 files, a php file and an html file and I want to make a form. In my html file I have the contents of the website as well as my form. In my php file (named contact.php) I am trying to process the inputs in php. Right now when I click submit, the html form brings me to my php file and doesn't echo the inputs. I know I could make take all the html and php into one php file and this would work, but is there any way to do it with what I have now.

html code

<form action = "contact.php" method = "POST">
name: <input type = "text" name = "name"> <br>
email: <input type = "text" name = "email"> <br>
subject: <input type = "text" name = "subject"> <br>
<input type = "submit" value = "submit" name = "submit"> <br>
</form>

php code

 <?php 
$name = "";
$email = "";
$subject = "";
$nameError = "";
$emailError = "";
$subjectError = "";
$x = 5;
function filterData($data)  {
$data = htmlspecialchars($data);
$data = stripslashes($data);
return $data;
}
$connection = mysqli_connect('x', 'x', 'x'); 
if (!connection) { 
    die('Could not connect: ' . mysqli_error()); 
} 

mysqli_select_db(contact); 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
//handles the name 
if (empty($name)) {
$nameError = "please don't leave the name field blank";
}
else {
$name = filterData($_POST["name"]);
}
//handles the email
if (empty($email)) {
$emailError = "please don't leave the email field blank";
}
else {
$email = filterData($_POST["email"]);
}
//handles the subject
if (empty($subject)) {
$subjectError = "please don't leave this field blank";
}
else {
$subject = filterData($_POST["subject"]);
}

}

echo $name;
echo $subject;
echo $email;



?> 

You're doing your empty check too early.

Change

if (empty($name)) {
    $nameError = "please don't leave the name field blank";
}
else {
    $name = filterData($_POST["name"]);
}

to

$name = filterData($_POST["name"]);
if (empty($name)) {
    $nameError = "please don't leave the name field blank";
}

Make similar changes to the other variable definitions.

You could also echo $nameError , $emailError and $subjectError to see a bit better what's going on.

<?php 
    $connection = mysqli_connect("x", "x", "x", "x");
    // Evaluate The Connection
       if (mysqli_connect_errno()) {
          echo mysqli_connect_error();
          exit();
       };
?><?php
// Set Var
    $name = "";
    $email = "";
    $subject = "";
    $nameError = "";
    $emailError = "";
    $subjectError = "";
    $p_test = false; $e_test = false; $n_test = false; $Error = '';
    $x = 5;
// Check Post
    if(isset($_POST['name'])){
        $n_test = true;
        $name = mysqli_real_escape_string($connection,filter_var(($_POST['name']), FILTER_SANITIZE_STRING));
    }else{
        $Error.= ' Name Required ';
    }
    if(isset($_POST['email'])){
        $e_test = true;
        $email = mysqli_real_escape_string($connection,filter_var(($_POST['email']), FILTER_SANITIZE_STRING));
    }else{
        $Error.= ' Email Required ';
    }
    if(isset($_POST['password'])){
        $p_test = true;
        $password = mysqli_real_escape_string($connection,filter_var(($_POST['password']), FILTER_SANITIZE_STRING));
    }else{
        $Error.= ' Password Required ';
    }
// Database
    if($n_test === true && $e_test === true && $p_test === true){
        $sql = "INSERT INTO users (name, email, password) VALUES('$name','$email','$password')";
            if (mysqli_query($connection, $sql)){$Error = 'success'; exit();}
            else {echo 'Connection Error'; exit();}
    }else{$Error = $Error.' Fields Required <hr>';}
?>
<div id="display_message">
     <?=$error;?>
</div>


<form action = "contact.php" method = "POST">
name: <input type = "text" name = "name"> <br>
email: <input type = "text" name = "email"> <br>
subject: <input type = "text" name = "subject"> <br>
<input type = "submit" value = "submit" name = "submit"> <br>
</form>

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