duan111112 2016-03-22 19:38
浏览 209

注册表单提交到数据库PHP

I am a beginner at PHP and I am trying to build a register form for a project, I made all the validations possible, however I am stuck from here as where it should go, I want the form to validate all the following then submit if all is true.

PHP code:

<?php

//register form v1.0

error_reporting(0);

//declares register form


$formFields = array('reg-username' => 'اسم المستخدم', 
    'reg-email' => 'البريد الإلكتروني', 
    'reg-password' => 'كلمة المرور', 
    'reg-confirmPassword' => 'تأكيد كلمة المرور');




function checkBlank(){

global $formFields;


//now I want the browser to check each field if its empty

foreach($formFields as $fieldName => $fieldRealName){
    if(empty($_POST[$fieldName])){
        echo '<ul class="ErrorMessage"><li>لم تدخل '. $fieldRealName .' * </li></ul>';
        echo '<style>.'. $fieldName .'{
            border-color: red;
        }
        .'. $fieldName .'::-webkit-input-placeholder {
   color: red;
}
        .'. $fieldName .'-h{
            color: red;
        }
        #asetrik{
            display: none;
        }
        </style>';
}
}

}



//blank fields have been checked
function checkPass(){

        $regPassword = $_POST['reg-password'];
        $regConfPassword = $_POST['reg-confirmPassword'];



    if($regPassword !== $regConfPassword){
        echo '<ul class="ErrorMessage"><li>كلمات المرور غير متطابقة *</li></ul>';
    } //if the fields are not empty i want it to check if the passwords match
    }   


    function checkEmail(){



        $regEmail = $_POST['reg-email'];

        if (!filter_var($regEmail, FILTER_VALIDATE_EMAIL)) {

                echo '<ul class="ErrorMessage"><li>البريد الإلكتروني المدخل غير صحيح *</li></ul>';
    }

    function checkName(){
        $regUsername = $_POST['reg-username'];
if ( !preg_match('/^[A-Za-z][A-Za-z0-9]{5,31}$/', $regUsername)){
  echo '<ul class="ErrorMessage"><li>اسم المستخدم يجب أن يبدأ بحرف *</li></ul>'; 
}
    }



function checkExist(){

        $regUsername = $_POST['reg-username'];
        $regEmail = $_POST['reg-email'];
        $connectToDB  = mysql_connect('localhost', 'root', '') or die(mysql_error());
        $selectDB = mysql_select_db('supermazad') or die(mysql_error());
        $checkIfExist = mysql_query("SELECT * FROM users WHERE username LIKE '".$regUsername."' OR email LIKE '".$regEmail."' ");


        if(mysql_num_rows($checkIfExist) > 0){
        echo '<ul class="ErrorMessage"><li>اسم المستخدم/ البريد الإلكتروني موجود *</li></ul>';

}
}















?>
  • 写回答

3条回答 默认 最新

  • drqwbh2150 2016-03-22 19:54
    关注

    See my comment. There are different ways to handle submits, and often pages have multiple forms on them. Let's say your form has a submit button.

    <input type="submit" name="submit-form" value="Done">
    

    Pseudo code for this would be something like this:

    $formErrors = array();
    // You would need to add this as a global to all your functions, and as mentioned in my comment, add your errors to this array rather than echoing them out from the functions
    
    if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['submit-form'])) && checkblank() && checkPass() && ...etc) {
        //form was submitted and is ok
    } else {
        //You echo out your form.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)