如何从表单访问另一个php文件? [英] how to access another php file from a form ?

查看:61
本文介绍了如何从表单访问另一个php文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing the form validation using php.I'm trying to access the php file from my form(<form action="appoint.php">).But it shows me undefined variable error at each of the form elements.(code works well when i use the <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">which means i used form(html) and php in the same file. here is my form code:

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

这是我的appoint.php `

and here is my appoint.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填写所有必填字段.

Make all the fields required in html.

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

然后在php上执行此操作.在if-else部分之外定义变量.

Then on the php do this. Define the variable outside the if-else sections.:

$fname = '';

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

这篇关于如何从表单访问另一个php文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆