如何从表单访问另一个PHP文件?

我正在使用php进行表单validation。我正在尝试从我的表单中访问php文件(

)。但是它显示了每个表单元素的未定义变量错误。(我使用<form method="post" action="">这意味着我在同一个文件中使用了form(html)和php这是我的表格代码:

 First Name: <input type="text" name="fname" value=""> *  

Last Name: <input type="text" name="lname" value=""> *

E-mail: <input type="text" name="email" value=""> *

Phone-no: <input type="text" name="phone_no" value=""> *

Date: <input type="text" name="date" value=""> *

Time: <input type="text" name="time" value=""> *

Physician: <input type="text" name="physician" value=""> *

Remarks : <input type="text" name="remarks" value=""> * complaint: <textarea name="complaint" rows="5" cols="40" value=""> *

这是我的appointment.php`

  $data = htmlspecialchars($data); return $data; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["fname"])) {$error['fname']= "First Name is required";} else { $fname = test_input($_POST["fname"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$fname)) { $error['fname'] = "Only letters and white space allowed"; } } if (empty($_POST["lname"])) {$error['lname']= "Last Name is required";} else { $lname = test_input($_POST["lname"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$lname)) { $error['lname'] = "Only letters and white space allowed"; } } if (empty($_POST["email"])) {$error['email'] = "Email is required";} else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $error['email'] = "Invalid email format"; } } if (empty($_POST["phone_no"])) {$phone_no = '00-0000-0000';} else { $phone_no = test_input($_POST["phone_no"]); // check if phone.no is valid// if(!preg_match("/^[0-9]{2}-[0-9]{4}-[0-9]{4}$/", $phone_no)) { $error['phone_no'] = "Invalid Number"; } } if (empty($_POST["date"])) {$error['date'] = "Date is required";} else {$date= test_input($_POST["date"]);} if (empty($_POST["time"])) {$error['time'] = "Time is required";} else {$time = test_input($_POST["time"]);} if(empty($_POST["physician"])) {$error['physician']="select a physician";} else {$physician=test_input($_POST["physician"]); } if (empty($_POST["remarks"])) {$error['remarks'] ="";} else { $remarks = test_input($_POST["remarks"]);} if (empty($_POST["complaint"])) {$error['complaint'] = " complaint is required";} else { $complaint = test_input($_POST["complaint"]);} if(empty($error)) {$con=mysqli_connect("localhost","root","root","my_db1"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="INSERT INTO np_appointment(fname,lname,date,time,email,phone_no, physician,remarks,complaint) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[date]', '$_POST[time]','$_POST[email]','$_POST[phone_no]', '$_POST[physician]','$_POST[remarks]','$_POST[complaint]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); } } ?> ` 

您可以尝试不同的方法:

制作html中所需的所有字段。

  

然后在php上做这个。 在if-else部分之外定义变量:

 $fname = ''; if (empty($_POST["fname"])) {$error['fname']= "First Name is required";} else { $fname = test_input($_POST["fname"]); }