0

こんにちは、サーバー側とクライアント側の両方で 3 つのことを検証する方法を知りたいと思っていました。

データベース テーブル製品

私のデータベースはこのように見えますが、400 以上のデータがあります。

pid   name   size            
1     grey   12 inch          
2     blue   16 inch     
database table category

pid    category
1        vans
2        nike
database table itemised

pid      price
1        30.00
2        50.00

確認する必要があるフィールドがいくつかあります。空でないことを確認するために、すでに検証を行っています。

私のテーブルのフィールドの1つは次のようになります

  <select id="Category" name="Category">
<?php
        dbconnect(); 
        $stmt = $conn->prepare("SELECT Name FROM Category WHERE pid=:id");
        $stmt->bindParam('id',$id);
        $stmt->execute();
        $i = 0;
         $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
          foreach ($rows as $row) { 
        if ($i == 0) {

        echo '<option SELECTED value="'.$row['Name'].'">'.$row['Name'].'</option>
        ';
        }
        else {
        echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
        }
        $i++;
        }
    ?>
      </select>

形式ではなく表です。sizeas番号に気づいたら、それを入力してください。

私の質問 多かれ少なかれ、投稿された値を変更するための開発者ツールに精通しているすべての人。client(JS) と client(php) の両方を検証して、誰も値を台無しにしないようにします。

例として 空でないことを確認するためにこれを行いましたnormal

<option value="blue vans">blue vans</option>

not-normal

<option value="">blue vans</option> // in this one the value is `BLANKET` 

以下のjsはこれをチェックします

function isEmpty(aTextField,errormessage) //null may be used for errormessage to only change the colour of matching fields
{
    if (aTextField.value.length<=0)
    {
        if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,false]; } else { fields[fields.length-1] = [aTextField,false]; }
        lfield = aTextField;
        if (errormessage !== "")
        {
            if (counter < error_count || error_count == 0) { errors += errormessage+'\n'; } counter++;
        }
        return true;
    }
    else
    {
        if (lfield != aTextField || lfield === null) { fields[fields.length] = [aTextField,true]; }
        lfield = aTextField;
        return true;
    }
}

PHPで

function notEmpty($inputname,$error_message)
    {
        $this->js.='if (!isEmpty(formname.'.$inputname.',"'.$error_message.'")) return false;';
        if ( isset($_POST[$inputname]) && ( strlen($_POST[$inputname]) <= 0 ))
        {
            if ($error_message != null)
            {
                $this -> error_message .= $error_message.'<br>';
            }
        }
    }

これで、テーブル内のすべてのオプションのブランケット値を検証する方法がわかります。js と php の両方で検証するにはどうすればよいですか

最後に持っていなかったsize場合はちょっと簡単ですinchが、データベース内の1000を超えるデータを変更するのは面倒です。

どうすればこれを行うことができますか?

これを明確に説明したことを願っています。そうでない場合は、コメントを残してください。質問を言い換えてみます.

ありがとう

4

4 に答える 4

0

検証の代替方法を提供するためだけに。

ajax 経由でフォームを投稿することもできるので、サーバー側で検証するだけで済みます。

$('#myForm').ajaxForm({
    dataType: 'json',
    success: function(response) {
        if (response.error) {
            alert(response.error);
        } else {
            $('#myForm').text(response.message);
        }
    }
});

(これは、jQuery プラグインhttp://malsup.com/jquery/formを使用していると仮定しています)

次に、PHP で現在のリクエストが ajax かどうかを確認する必要があります。

function isAjax() {
    return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
        strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
}

...
if (isAjax() {
    $response = array();
    if (!empty($this->error_message)) {
        $response['error'] = str_replace('<br>', "\n", $this->error_message);
    } else {
        $response['message'] = 'Form submitted';
    }
    header('Content-type: application/json');
    echo json_encode($response);
    exit;
}
于 2013-07-27T10:58:13.787 に答える