从表单处理php文件时未定义的变量错误

时间:2014-01-27 04:52:25

标签: php forms

我正在使用PHP进行表单验证。我正在尝试从我的表单(<form action="appoint.php">)访问PHP文件。但它显示了每个表单元素的未定义变量错误。 (当我使用<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">时代码运行良好,这意味着我在同一个文件中使用了表单(HTML)和PHP。

这是我的表单代码:

<form method="post" action="appoint.php">
First Name: <input type="text" name="fname" value="<?php echo $fname;?>">
<span class="error">* <?php if(isset($error['fname']))
echo $error['fname'];?></span>
<br><br>
Last Name: <input type="text" name="lname" value="<?php echo $lname;?>">
<span class="error">* <?php if(isset($error['lname']))
echo $error['lname'];?></span>
<br><br>   
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">*<?php if(isset($error['email']))
echo $error['email'];?></span> 
<br><br>
Phone-no: <input type="text" name="phone_no" value="<?php echo $phone_no;?>">
<span class="error">* <?php if(isset($error['phone_no']))
echo $error['phone_no'];?></span>
<br><br>   
Date: <input type="text" name="date" value="<?php echo $date;?>">
<span class="error">* <?php if(isset($error['date']))
echo $error['date'];?></span>
<br><br>
Time: <input type="text" name="time" value="<?php echo $time;?>">
<span class="error">*<?php if(isset($error['phone_bo']))
echo $error['time'];?></span>
<br><br>
Physician:<input type="text" name="physician" value="<?php echo $physician;?>">
<span class="error">* <?php if(isset($error['physician']))
echo $error['physician'];?></span>
<br><br>
Remarks : <input type="text" name="remarks" value="<?php echo $remarks;?>">
<span class="error">*<?php if(isset($error['remarks'])) 
echo $error['remarks'];?></span>
complaint: <textarea name="complaint" rows="5" cols="40"value="
<?php echo$complaint;?>"> </textarea>

<span class="error">* <?php if(isset($error['complaint']))
echo $error['complaint'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>

这是我的appoint.php

 <?php
 // define variables and set to empty values
 $error=array();
 $fname= $lname= $email = $phone_no = $date = 
  $time= $physician= $remarks=$complaint=$data = "";
 function test_input($data)
  {

 $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); }


 }

 ?>  
 `

1 个答案:

答案 0 :(得分:0)

你在test_input函数中缺少一个全局声明(比如“global $ error;”)。

编辑:事实并非如此,请参阅评论。