0

如果没有选择“分支”单选按钮,我想在下面的代码中添加一个条件语句,以防止 PHP Post 运行。

我找不到正确执行此操作的语法。

我不确定如何在条件语句中引用 HTML 标记。

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Data has been submitted!");
}
</script>
</head>
<body>

<h1>Reference Stats Input</h1>
<form  method="post">
<h2>Select Branch</h2>
<label><input type="radio" name="branches" value="AR">AR</label><br>
<label><input type="radio" name="branches" value="BK">BK</label><br>
<label><input type="radio" name="branches" value="BR">BR</label><br>
<label><input type="radio" name="branches" value="EM">EM</label><br>
<label><input type="radio" name="branches" value="MD">MD</label><br>
<label><input type="radio" name="branches" value="PT">PT</label><br>
<label><input type="radio" name="branches" value="UR">UR</label><br>
<label><input type="radio" name="branches" value="WA">WA</label><br>
<br>
<br>
<br>

Type of Reference question:<select name="refquestion" <br><br>
<option value="ADULT">ADULT</option>
<option value="CHILD">CHILD</option>
<option value="JUVENILE">JUVENILE</option>
</select><br><br>

<input name="Submit" type="submit" onclick="myFunction()" value="INPUT">

<?php
if(!empty("branches"){
$con = mysql_connect("localhost","root","REDACTED");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("refstats", $con);
$sql="INSERT INTO reftable (branch, statcat)
VALUES
('$_POST[branches]','$_POST[refquestion]')";

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

mysql_close($con)
}
?>

</form>
</body>
</html>
4

2 回答 2

3

You can not do this with php, php is serverside and only knows the selection after you posted it.

You have to do this with an eventhandler in javascript. Im going to give you an example with jQuery to illustrate it, I'll see if I can get it back to native JS:

// jQuery style (and therefor requires jQuery to work)
$('form').on('submit', function(e){
    if( $('[name="branches"]:selected').length === 0){
        e.preventDefault();
        alert('please select a branch');
    }
});

// Or native Javascript (this isn't tested 100%, but something like this):
document.getElementById("form").onsubmit=function(){
    // get all options
    var options = document.getElementsByName("branches");
    var itemChecked = false; // start with false, se to true if there is a selected option
    for (var i = 0; i < options.length; i++) {
        if (options[i].checked) { // if one option is found
            itemChecked  = true; // set to true
            break; // no need to continue the loop
        }
    }
    return itemChecked
};

You could always make on option default selected :)
IMPORTANT: Never trust javascript!! Always doublecheck clientside information serverside. Clientside information should ALWAYS be considered evil and you should make sure it is not.

于 2013-08-28T17:23:46.957 回答
0

你可以做一些事情来影响:

if(!isset($_POST['branches']) { 
// do something
}

elseif(isset($_POST['branches']) { 
$con = mysql_connect("localhost","root","REDACTED");
// continue with rest of code
于 2013-08-28T17:25:30.423 回答