[英]How to make sure form cannot be submitted if text field is empty when user selects False Hit radio button?
我正在做一些事情,以防止用戶提交HTML表單而不會留下他們對False Hits的評論。
條件:
當前,我已經使用JavaScript來檢查文本字段(備注)是否為空。
function empty() {
var x;
x = document.getElementById("roll-input").value;
if (x == "") {
alert("Please enter remarks for false hits selected.");
return false;
};
我還可以檢查使用JavaScript選擇了哪個單選按鈕。
<input type="radio" name="select" id="truehit" value="true" />
<input type="radio" name="select" id="falsehit" value="false" />
if(document.getElementById('falsehit').checked) {
//False Hit radio button is checked
}
else if(document.getElementById('truehit').checked) {
//True Hit radio button is checked
}
但是,我非常想將兩個條件聯系在一起。 有沒有更簡單的方法呢? 任何幫助是極大的贊賞。 先感謝您。
這就是我的表格的樣子。
<body bgcolor="#FFFFFF">
<br />
<div align="center" class="h1">
<h1>Entity Search</h1><br />
</div>
</html>
<br />
<?php
include("db_connect.php"); //connect to database
@$loginid = strtoupper(base64_decode($_POST['LanID'])); //to capture
the user's LanID as the UserID
if($loginid == "")
{
$userID = $_SESSION['loginid'];
}
else
{
$_SESSION['loginid'] = $loginid;
$userID = $_SESSION['loginid'];
}
$userID = $_SESSION['loginid'];
//to insert date and time of the records
date_default_timezone_set('Asia/Singapore');
$Date = date('Y-m-d');
$Time = date ('h:i:s a');
$userID = $_SESSION['loginid'];
$loginid = "";
$entityfromform = $_GET["entity"];
?>
<br />
<?php
//connection to database
$connectionInfo = array( "Database"=>"test","UID"=>$test,"PWD"=>$test);
$db_connect=sqlsrv_connect($serverName, $connectionInfo);
//to retrieve the department name from the user database and insert
department name of the user into the records
$namesql = "SELECT Name, DeptName, Role FROM RPDB.dbo.Userdb WHERE
LanID='$userID';";
$name_query = sqlsrv_query($db_connect,$namesql);
while($row = sqlsrv_fetch_array($name_query, SQLSRV_FETCH_ASSOC))
{
$nameontop = iconv("gb2312","UTF-8",$row['Name']);
$admincheck = iconv("gb2312","UTF-8",$row["Role"]);
$deptnamecheck = iconv("gb2312","UTF-8",$row["DeptName"]);
}
$negative = "Negative Hit: No results found.<br />";
$result = "$entityfromform <br />Negative Hit: No results found.<br />";
//variable to insert into the table as result
//search for keyword from form in the Entity Table List where Name =
keyword
$entitysql = "SELECT * FROM RPDB.dbo.entitysearch_table WHERE Name LIKE
'%" . $entityfromform . "%' OR Name = '".$entityfromform."'";
$entity_query = sqlsrv_query($db_connect,$entitysql);
//variables
$i=0;
while($row = sqlsrv_fetch_array($entity_query, SQLSRV_FETCH_ASSOC))
{
$name[$i] = $row["Name"];
$brn[$i] = $row["Business_Registration_Number"];
$address[$i] = $row["Registered_Address"];
$group[$i] = $row["Applicable_Limb_of_Related_Party_Group"];
$selectradio[$i] = 'selectradio'.$i;
$text[$i] = 'text'.$i;
$i++;
}
if(@$name != NULL) //Positive Result
{
echo "<input type='button' name=uncheck' value='Uncheck All'
class='uncheck' onClick='window.location.reload()'>"; //uncheck all
function where it clears all changes
echo "<h2><div align='center'>Results found for:
$entityfromform</div></h2><br />";
//table to display the data
echo "<div align='center'>
<table>
<tr>
<th>Name</th>
<th>Business Registration Number</th>
<th>Registered Address</th>
<th>Applicable Limb of Related Party Group</th>
<th>True Hit</th>
<th>False Hit</th>
<th>Unable To Ascertain</th>
<th>Remarks, for false hit</th>
</tr>
</div>";
echo "<strong><div class='wording'>Positive Hit: </div></strong>";
echo "<br /><br />";
$x=0;
?>
<!--The form data is sent for processing to action_handler.php by HTTP POST method-->
<form method="post" id="form" action="action_handler.php" enctype="multipart/form-data">
<?php
//data displayed in tables along with checkboxes for true hit, false hit, unable to ascertain and text box for remarks where applicable
for($x=0;$x<$i;$x++)
{
echo "<tr>
<td>" . $name[$x] . "</td>
<td>". $brn[$x] . "</td>
<td>". $address[$x] . "</td>
<td>". $group[$x] . "</td>
<td>
<label class='container'>
<input type='radio' id='truehit' name='".$selectradio[$x]."' value='true hit' class='btn' required>
<span class='checkmark'></span>
</label>
</td>
<td>
<label class='container'>
<input type='radio' id='falsehit' name='".$selectradio[$x]."' value='false hit' class='btn'>
<span class='checkmark'></span>
</label>
</td>
<td>
<label class='container'>
<input type='radio' id='unabletoascertain' name='".$selectradio[$x]."' value='unabletoascertain' class='btn'>
<span class='checkmark'></span>
</label>
</td>
<td>
<label><input type='text' name='".$text[$x]."'>
</label>
</td>
</tr>" ;
}
echo "</table>"; //table closed
echo "<br /><br /><br />";
echo "<input type='button' value='Submit' class='submit' onClick='empty()'>"; //submit button to confirm
?>
</form>
<?php
//sessions to be used in others
$_SESSION['num'] = $x;
$_SESSION['entityfromform'] = $entityfromform;
}
else
{
echo "<div align='center'><h2>Results found for: $entityfromform</h2>
<br />";
echo "<div class='wording' align='center'>$negative</div><br><br>
<br>";
}
?>
</table>
</body>
如果文本框具有值,那么您的empty()函數將不返回任何值,因此對於該條件,該值是未定義的; 您可以根據自己的需求創建一個函數,例如:
function doSubmit()
{
if(document.getElementById('falsehit').checked)
{
if(document.getElementById("roll-input").value == '')
{
alert("Please enter remarks for false hits selected.");
return false;
}
}
return true;
}
並調用此方法進行驗證,因此,如果checked
falsehit
且remarks
具有值,則它將返回true
或false
。
希望這可以幫助; 如果想念您的問題,請告訴我。
祝您編程愉快...
您可以使用一組if
語句,如下所示。 這基本上檢查是否選中了falsehit
單選按鈕。 如果是,則檢查文本是否為空。 如果文本為空,則會提示您想要的內容。 如果文本不為空,則提交表單。
另外,如果未選中falsehit
,它將自動提交表單。
按照注釋中的要求:現在,我得到每個單選按鈕,並循環瀏覽它們以檢查是否已選中它們。 如果不是,您會收到一條警告消息,提示您Please select all accordingly
。 否則,它的工作原理與以前相同。
function empty() { var text; var groups = document.querySelectorAll("[id^=group]"); var selected = {}; var radioButtons; var submitForm = true; groups.forEach(e1 => { radioButtons = e1.querySelectorAll("input[type=radio]"); radioButtons.forEach(e2 => { if (e2.checked) { selected[e1.id] = true; } }); if (selected[e1.id] == undefined) { selected[e1.id] = false; } }); groups.forEach(e => { text = e.children[0].value; for (var i = 0; i < e.children.length; i++) { if (e.children[i].id == "falsehit") { if (e.children[i].checked) { if (text.length == 0) { alert("Please enter remarks for false hits selected."); submitForm = false; } } else if (selected[e.id]) { //Don't need to do anything. Just need to prevent the next else from accessing these values } else { alert("Please select all accordingly."); submitForm = false; } } } }); if (submitForm) { document.getElementById("form").submit(); } }
<form id="form"> <fieldset id="group1"> <input type="text" id="roll-input" /> <input type="radio" name="group1" id="truehit" value="true" /> <input type="radio" name="group1" id="falsehit" value="false" /> </fieldset> <fieldset id="group2"> <input type="text" id="roll-input" /> <input type="radio" name="group2" id="truehit" value="true" /> <input type="radio" name="group2" id="falsehit" value="false" /> </fieldset> <input type="button" value="Submit" onclick="empty()" /> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.