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

时间:2014-01-26 19:59:05

标签: php jquery forms

我正在使用php进行表单验证。我正在尝试从我的表单(<form action="appoint.php">)访问php文件。但它显示了每个表单元素的未定义变量错误。(代码工作原理)当我使用时     <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">这意味着我在同一个文件中使用了form(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['time']))
     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>

这是我的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); }


 }

 ?>  
 `

1 个答案:

答案 0 :(得分:0)

您可以尝试不同的方法:

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

<input type="text" name="fname" value="<?php echo $fname;?>" required> 

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

$fname = '';

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