dotxxh0998 2013-06-20 09:40
浏览 24

用jquery验证并用php修改MSSQL

I recently tried to use the PDO classes of php with MSSQL. I have a form with textboxes and a submit button. Now what I like to do is to do the validation with jQuery to let it go faster. Whenever jQuery is done with the validation and everything is filled it as it should be, it should activate a php function that will add the form as a query with the PDO classes.
Html code:

<div id="registreren">
    <div class="titel">
            <p class="big"> Registreren| </p>
            <p class="small"> Registreer met jouw gewenste gegevens.</p>
    </div>
    <div id="form">
        <form method="POST" action="" onsubmit="return false;">
            <table border = '1'>
                <tr>
                    <td>Username:</td>
                    <td><input type="textbox" name="txtUsername" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="txtPassword" /></td>
                </tr>   
                <tr>
                    <td>E-mail:</td>
                    <td><input type="textbox" name="txtEmail" /></td>
                </tr>
                <tr>
                    <td>Telefoonnummer:</td>
                    <td><input type="textbox" name="txtTel" /></td>
                </tr>   
                <tr>
                    <td></td>
                    <td  class="showPass"><input type="checkbox" name="showPass" />Laat password zien</td>
                </tr>                   
                <tr>
                    <td><p class="warning4">&nbsp;<br /> &nbsp;</p></td>
                    <td><button type="submit" name="registreren" value="registreren" class="btnRegistrate">Registreren</button></td>
                </tr>
            </table>
        </form>
    </div>

JQuery code:

var registrateEmpty;

$('.btnRegistrate').click(function() {
    $('.warning4').html('');
    registrateEmpty = false;
    $('#registreren input').each(function() {
        if($(this).val() == "")
        {
            $('.warning4').html('Vul alle velden in!');
            registrateEmpty = true;
            return false;
        }
    })

    if(registrateEmpty!=true) {
        if($('input[name=txtUsername]').val().length < 5 || $('input[name=txtPassword]').val().length < 5) {
            $('.warning4').html('Username en password moeten minimaal uit 5 tekens bestaan.');
            return false;
        }
        else if($('input[name=txtTel]').val().match(/[a-zA-Z]/) || $('input[name=txtTel]').val().length != 10) {
            $('.warning4').html('Vul een geldige telefoonnummer in.');          
            return false;
        }
        else if($('input[name=txtEmail]').val().indexOf('@') == -1 || $('input[name=txtEmail]').val().length < 5) {
            $('.warning4').html('Vul een geldige e-mail adres in.');    
            return false;               
        }
        else {
            <?php 
                toevoegen();
            ?>
            $('.warning4').html('');                            
        }
    }
})

What this script actually does do is it will first check if it has any empty input. If everything is filled it, it will check for some specific lenghts of the value that is filled in. When everything is 'good', it will activate the php function

Php function:

include('connect.php');

function toevoegen() {
    global $pdo;
    try {
        $sql = "INSERT INTO account 
                                    (acc_Username, acc_Password, acc_Email, acc_Tel, join_Date, privilege) 
                                    VALUES 
                                    (:username, :password, :email, :tel, :jdate, 1);
                                    ";

        $s = $pdo -> prepare($sql); //doet hetzelfde als query() alleen moet je hier nog een trigger functie sturen

        $s->bindValue(':username', $_POST['txtUsername'], PDO::PARAM_STR);
        $s->bindValue(':password', $_POST['txtPassword'], PDO::PARAM_STR);
        $s->bindValue(':email', $_POST['txtEmail'], PDO::PARAM_STR);
        $s->bindValue(':tel', $_POST['txtTel'], PDO::PARAM_STR);                
        $s->bindValue(':jdate', date('d-m-Y'), PDO::PARAM_STR);

        $s->execute(); 

        $message = "Succeed!";
    }
    catch (PDOException $e)
    {
        echo 'Fout bij inserten van een rij: ' . $e->getMessage();
        exit;
    }
}

What this do is that it will first prepare to sql statement. Then it will fill the open variables from the statement with the textbox values.

Now what does happen is that it seems it executes the php function with out even validating with JQuery. My error :

Undefined index: txtUsername , Undefined index: txtPassword , Undefined index: txtEmail , Undefined index: txtTel

So I hope someone know what went wrong, or how to work with php jquery together like this. I hope that even with some things being other language, it is still understandable. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douhuan7862 2013-06-20 09:48
    关注

    you are simply calling a function from javascript everything is fine then how will you get them as post values in the function you pass them as parameters to function or simply return true if every thing is fine and put your function under

    $('.btnRegistrate').click(function() {
        $('.warning4').html('');
        registrateEmpty = false;
        $('#registreren input').each(function() {
            if($(this).val() == "")
            {
                $('.warning4').html('Vul alle velden in!');
                registrateEmpty = true;
                return false;
            }
        })
    
        if(registrateEmpty!=true) {
            if($('input[name=txtUsername]').val().length < 5 || $('input[name=txtPassword]').val().length < 5) {
                $('.warning4').html('Username en password moeten minimaal uit 5 tekens bestaan.');
                return false;
            }
            else if($('input[name=txtTel]').val().match(/[a-zA-Z]/) || $('input[name=txtTel]').val().length != 10) {
                $('.warning4').html('Vul een geldige telefoonnummer in.');          
                return false;
            }
            else if($('input[name=txtEmail]').val().indexOf('@') == -1 || $('input[name=txtEmail]').val().length < 5) {
                $('.warning4').html('Vul een geldige e-mail adres in.');    
                return false;               
            }
            else {
                $('.warning4').html('');                            
            }
        }
    })
    IF($_SERVER['REQUEST_METHOD'] == "POST") {
    //call your function
      toevoegen()
    } 
    
    function toevoegen() {
        global $pdo;
        try {
            $sql = "INSERT INTO account 
                                        (acc_Username, acc_Password, acc_Email, acc_Tel, join_Date, privilege) 
                                        VALUES 
                                        (:username, :password, :email, :tel, :jdate, 1);
                                        ";
    
            $s = $pdo -> prepare($sql); //doet hetzelfde als query() alleen moet je hier nog een trigger functie sturen
    
            $s->bindValue(':username', $_POST['txtUsername'], PDO::PARAM_STR);
            $s->bindValue(':password', $_POST['txtPassword'], PDO::PARAM_STR);
            $s->bindValue(':email', $_POST['txtEmail'], PDO::PARAM_STR);
            $s->bindValue(':tel', $_POST['txtTel'], PDO::PARAM_STR);                
            $s->bindValue(':jdate', date('d-m-Y'), PDO::PARAM_STR);
    
            $s->execute(); 
    
            $message = "Succeed!";
        }
        catch (PDOException $e)
        {
            echo 'Fout bij inserten van een rij: ' . $e->getMessage();
            exit;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来