PHP教程

PHP-表单

本文主要是介绍PHP-表单,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?php

/**
 * @Author: zwb
 * @Date:   2021-09-19 14:25:27
 * @Last Modified by:   zwb
 * @Last Modified time: 2021-09-19 15:23:19
 */

?>

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
   <div style="margin: 0 auto;width: 1000px">
      <form action="4.php" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <input type="email" class="form-control" name="email" id="email" placeholder="请输入邮箱" style="width: 300px">
        </div>
        <div class="form-group">
          
          <input type="text" class="form-control" name="idcard" id="idcard" placeholder="请输入身份证号码" style="width: 300px">
        </div>
       <div class="form-group">
          <input type="text" class="form-control" name="realname" id="realname" placeholder="请输入姓名" style="width: 300px">
        </div>
        <div class="form-group">
          <input type="file" id="avatar" name="avatar">
        </div>
        <button type="submit" class="btn btn-info">信息录入</button>
      </form>
   </div>
</body>
</html>



<?php

/**
 * @Author: zwb
 * @Date:   2021-09-19 14:29:59
 * @Last Modified by:   zwb
 * @Last Modified time: 2021-09-19 16:43:21
 */
$email = $_POST['email'];
if(empty($email)){
   echo "<script>alert('邮箱不能为空');window.location.href='3.php';</script>";
   //echo "邮箱不能为空";
   //header("refresh:2,url='http://www.1910a.com/day9/3.php'");
   //refresh:秒数
   //url= '跳转的路径'
   die();
}
$regEmail = '/^\w{5,30}@\w{2,6}\.(com|cn)$/';
if(!preg_match($regEmail, $email)){
   echo "<script>alert('邮箱格式不正确');window.location.href='3.php';</script>";
}

$idCard = $_POST['idcard'];
if(empty($idCard)){
   echo "<script>alert('身份证号码不能为空');window.location.href='3.php';</script>";
   die();
}

$idReg = '/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/';
if(!preg_match($idReg, $idCard)){
   echo "<script>alert('身份证号码格式不正确');window.location.href='3.php';</script>";
   die();
}

$realName = $_POST['realname'];

$nameReg = '/^[\x{4e00}-\x{9fa5}]{2,4}$/u';
if(!preg_match($nameReg, $realName)){
   echo "<script>alert('姓名格式不正确');window.location.href='3.php';</script>";
   die();
}

$name = mb_substr($realName, 0,1);//姓 截取中文
//410381199302085516

$strSex = substr($idCard, -2,1);
if($strSex %2 ==1){
   $sex = '男';
   $nameSex = '先生';
}else{
   $sex ='女';
   $nameSex = '女士';
}

$welcome = $name.$nameSex;

$year = date('Y');//2021
$age = $year-substr($idCard, 6,4);

if(strpos($email,'qq') >-1){
   $emailType= 'qq邮箱';
}
if(strpos($email,'sina') >-1){
   $emailType ='新浪邮箱';
}
if(strpos($email,'163') >-1){
   $emailType ='163邮箱';
}



$avatar = $_FILES['avatar'];
/*

Array
(
    [name] => 0eada5dcb4437eac.jpg
    [type] => image/jpeg
    [tmp_name] => C:\Windows\php23F5.tmp
    [error] => 0
    [size] => 226560
)
*/
switch ($avatar['error']) {
   case 1:
      echo "<script>alert('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');window.location.href='3.php';</script>";
      break;
   case 2:
      echo "<script>alert('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');window.location.href='3.php';</script>";
      break;
   case 3:
      echo "<script>alert('文件只有部分被上传');window.location.href='3.php';</script>";
      break;
   case 4:
      echo "<script>alert('没有文件被上传');window.location.href='3.php';</script>";
      break;
   case 6:
      echo "<script>alert('找不到临时文件夹');window.location.href='3.php';</script>";
      break;
   case 7:
      echo "<script>alert('文件写入失败');window.location.href='3.php';</script>";
      break;
   default:
      # code...
      break;
}

$allowType = ['image/jpeg','image/png','image/jpg','image/gif'];

if(!in_array($avatar['type'], $allowType)){
   echo "<script>alert('文件类型不合法');window.location.href='3.php';</script>";
   die();
}


$size = $avatar['size'];

if($size > 1024*1024){
   echo "<script>alert('文件过大');window.location.href='3.php';</script>";
   die();
}

$fileDir = './avatar/';
if(!file_exists($fileDir)){
   mkdir($fileDir);
}

$res = move_uploaded_file($avatar['tmp_name'],$fileDir.$avatar['name']);
if(!$res){
   echo "<script>alert('文件上传失败');window.location.href='3.php';</script>";
   die();
}

$path = $fileDir.$avatar['name'];


$link = mysqli_connect('127.0.0.1','root','root','1910a','3306');//扩展

$sql = "insert into info values(null,'$realName','$idCard','$email','$path')";

$res = mysqli_query($link,$sql); //bool
if($res){
   echo "<script>alert('信息录入成功');window.location.href='3.php';</script>";
}else{
   echo "<script>alert('信息录入失败');window.location.href='3.php';</script>";
}

mysqli_close($link);sss


//开启扩展三步骤

//1.将dll文件放到php扩展文件夹
//2.php.ini extension= mysqli.dll
//3.重启phpstudy

?>

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <h2>欢迎 <span style="color: red"><?php echo $welcome;?></span></h2>
   <p>年龄为:<span style="color: red"><?php echo $age;?></span></p>
   <p>身份号:<span style="color: red"><?php echo $idCard;?></span></p>
   <p>邮箱:<span style="color: red"><?php echo $email;?></span></p>
   <p>邮箱类型为:<span style="color: red"><?php echo $emailType;?></span></p>
   <p>头像:<img src="<?php echo $path;?>" width="100px" /></p>
</body>
</html>
这篇关于PHP-表单的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!