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 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题