混合php和html文件

时间:2016-06-02 15:24:18

标签: php html

现在我有2个文件,一个php文件和一个html文件,我想制作一个表格。在我的html文件中,我有网站的内容以及我的表格。在我的php文件(名为contact.php)中,我试图处理php中的输入。现在当我点击提交时,html表单将我带到我的php文件并且没有回显输入。我知道我可以把所有的html和php都放到一个php文件中,这样可以工作,但是有什么方法可以用我现在拥有的东西。

html代码

<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;



?> 

2 个答案:

答案 0 :(得分:5)

您过早地进行了empty检查。

更改

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

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

对其他变量定义进行类似的更改。

您还可以回复$nameError$emailError$subjectError,以便更好地了解正在发生的事情。

答案 1 :(得分:1)

<?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>