【发布时间】:2013-02-13 12:45:28
【问题描述】:
我在 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>
【问题讨论】:
-
发布您的代码.. 您的验证目前是如何工作的。是服务器端还是js中的?
-
如果你真的想看我不介意的文件,我已经将它附加到我的 OP 中。
-
表单甚至没有动作,这对 NOW 来说很好。它为我没有为其输入值的每个字段提供一个弹出窗口。太棒了!基本上我只想检查是否有任何必需的元素具有值。动态是可能的。提交按钮可以做到这一点,为什么我不能:(不提交。
-
谢谢,但不是这样。我不想创建自己的方法,而是调用提交调用的方法。