0

私はphpファイルにフォームを持っています。

そのフォーム内には、必須のSELECT要素と必須のINPUT TEXT要素を含むテーブルがあります。

また、テーブルの下に送信ボタンがあります。

送信は、フォームの必須フィールドに値があるかどうかをチェックして問題なく機能し、値が必要な要素をユーザーに警告します。

問題:

別のボタンで基本的に送信ボタンが行うチェックを行いたいと思います。つまり、必須フィールドに値が必要であることを警告しますが、フォームを送信しないでください。

基本的:

フォームの完了をチェックするメソッドを使用できるように、送信ボタンをクリックすると呼び出される関数は何ですか?

ContactCard.php

<?php
    require_once("config.php");

    //Connect to our database
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    //Return an error if we have connection issues
    if ($mysqli->connect_error)
    {
        die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
    }

    //Query the database for the results we want
    $query = $mysqli->query("SELECT field FROM `contact_fields`");

    //Create an array of objects for each returned row
    while($array[] = $query->fetch_object());

    //Remove the blank entry at end of array
    array_pop($array);

    //Print our array results
    //print_r_html($array);
?>

<?php
    //Free result set and close connection 
    $query->close();
    $mysqli->close();
?>

<html>
<head>
<title>Add a Contact</title>
<link <link rel="stylesheet" type="text/css" href="ContactCard.css">
<script>
function addOptRow()
{

    var optTable        = document.getElementById("optTable");
    var optTableRows    = optTable.rows.length;
    var newOptRow       = optTable.insertRow(optTableRows);


    //create the dropbox fields for the new row
    var optFieldNames   = document.createElement("select");
    optFieldNames.name  = "optFieldNames";
    optFieldNames.options[optFieldNames.options.length] = new Option("",null);
    "<?php foreach($array as $option) : ?>"
        optFieldNames.options[optFieldNames.options.length] = new Option("<?php echo $option->field; ?>",null); 
    "<?php endforeach; ?>"  
    var optFieldNamesCell   = newOptRow.insertCell(0);
    optFieldNamesCell.appendChild(optFieldNames);

    //create the field value for the new row
    var optFieldValue   = document.createElement("input");
    optFieldValue.type  = "text";
    optFieldValue.name  = "optFieldValue";
    optFieldValue.required  = "true";
    var optFieldValueCell   = newOptRow.insertCell(1);
    optFieldValueCell.appendChild(optFieldValue);

    //create the field remove for the new row
    var optFieldRemove  = document.createElement("input");
    optFieldRemove.type = "button";
    optFieldRemove.value    = "Remove";
    optFieldRemove.onclick  = function(){removeOptRow(this);}
    var optFieldRemoveCell  = newOptRow.insertCell(2);
    optFieldRemoveCell.appendChild(optFieldRemove);
}

function removeOptRow(removeButton)
{
    var optTable = document.getElementById("optTable");
    optTable.deleteRow(removeButton.parentNode.parentNode.rowIndex);
}

function checkAllFieldsComplete()
{
    //if(!isset( stuck here :(
}
</script>

</head>

<body>
    <h1 id="title" >Add A Contact</h1>

    <form>  
    <fieldset id="reqFields">
    <legend>Required Fields</legend>
    <table id="reqTable">
    <tr>
        <td>Company Name</td><td><input type="text" required></input></td>
    </tr>
    <tr>
        <td>Company Code</td><td><input type="text" required></input></td>
    </tr>
    </table>
    </fieldset>


    <fieldset id="optFields">
    <legend>Optional Fields</legend>
    <table id="optTable">
    <tr><th></th><th></th><th><input type="button" value="Add" onclick=addOptRow()></button></th></tr>  
    </table>
    </fieldset> 
    <input type="submit"></input>   
    </form> 


    <div id="test" name="test">
    </div>
</body>

</html>
4

3 に答える 3

0

多くの方法でそれを行うことができます最初に使用してフォーム検証を使用しますrequired

<input type="text" required>

満たされていない場合は、クライアント側の検証にJavaスクリプトを使用して即時応答を行うことができます。そのための多くの解決策も見つかります

空の入力フィールドの JavaScript 検証

それ以外の場合は、サーバー側で行う必要があります

于 2013-02-13T12:48:43.977 に答える
0

「html」に関する質問に答えるには、次の方法でボタンをクリックしたときに JavaScript を呼び出すことができます。

<input type="submmit" onclick="java_validation_function();" />

基本的に、次の 2 つの可能性があります。

  1. すべてをPHPに投稿し、POST変数が存在するかどうかを確認し、存在しない場合はエラーページを返します

  2. サーバーに送信する前にJavaScriptを使用して確認してください。そのためにjquery検証を使用し、INPUTからonclickパラメーターでjquery検証を呼び出すことができます。

ここに jquery/validation の例があります: http://docs.jquery.com/Plugins/Validation

于 2013-02-13T12:48:51.330 に答える