duanlin1931 2011-04-19 14:42
浏览 23
已采纳

Php验证表格

GOAL: Trying to make sure none of the fields are left blank including the radio button set and the dropdown. Anyone?

HTML

<form action="add_p_c.php" method="post"> 
    Professor<input type="radio" name="addType" />&nbsp;&nbsp;Course<input type="radio" name="addType" /> 
    <br><br>Name: <input type="text" name="name" /><br> 
    Department: <select name="deptName"><option>Department 1</option> <option>Department 2</option></select>
    Email: <input type="text" name="email" /><br>
    <input type="submit" name="submit" /> 
</form> 

**PHP (add_p_c.php) **

 <?php
if (isset($_POST['submit'])) {
if (empty($selected_radio)){ echo "You need to select a prof or course";} else(return;)
    $selected_radio = $_POST['addType'];
if (empty($course_prof_name)){ echo "You need to enter a name";} else(return;)
    $course_prof_name = $_POST['name'];
if (empty($select_dep)){ echo "You select a dept";} else(return;)
    $select_dep = $_POST['deptName'];
$email = $_POST['email'] = "myemail@email.com"; 
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { 
  return; 
} 
else { 
  echo "<span color='red;'>Invalid email address.</span>"; 
} 
}
?>
  • 写回答

4条回答 默认 最新

  • dongzhang1987 2011-04-19 15:03
    关注

    For the sake of safety, you should add in certain things to improve security, because if you do any queries to a DB, you could get an SQL injection. Try this:

    <?php
    if(isset($_POST['submit']){
    
        $_POST['name']= trim(strip_tags(addslashes($string)));
        $_POST['deptName']= trim(strip_tags(addslashes($string)));
        $_POST['email']= trim(strip_tags(addslashes($string)));
    
                /* I can't remember if it should be '' or NULL, but some simple testing will let you know which it is*/
        if($_POST['addType'] != ''){
             if($_POST['name'] != ''){
                if($_POST['deptName'] != ''){
                    if($_POST['email']) != ''){
                        $selected_radio = $_POST['addType'];
                        $course_prof_name = $_POST['name'];
                        $select_dep = $_POST['deptName'];
                        $email = $_POST['email'] = "myemail@email.com"; 
                        if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { 
                            return; 
                        } 
                        else { 
                            echo "<span color='red;'>Invalid email address.</span>"; 
                        } 
                    }
                    else{
                        // email wasn't set
                    }
                }
                else{
                    //deptName wasn't set
                }
            }
            else{
                //name wasn't set
            }
        }
        else{
            // add type wasn't set
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路