我有一个html表单,可以自动选中表单中的所有元素。提交后,它调用第一个元素(order_number),并且光标停留在第二个字段中,这样某人可以使用相同的order_number扫描Multiple(Chip_numbers)。
要重置整个表单,我想刷新页面,然后弹出错误消息,光标自动放入第二个元素。
这是我的剧本:
<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "JVSIntranet";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MICROCHIP_TBL (chip_number,order_number)
VALUES
('$_POST[chip_number]','$_POST[order_number]')";
IF (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
$value = "";
if( isset( $_POST ["order_number"] )) $value = $_POST ["order_number"];
mysqli_close($conn);
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
function getNextElement(field) {
var form = field.form;
for ( var e = 0; e < form.elements.length; e++) {
if (field == form.elements[e]) {
break;
}
}
return form.elements[++e % form.elements.length];
}
function tabOnEnter(field, evt) {
if (evt.keyCode === 13) {
if (evt.preventDefault) {
evt.preventDefault();
} else if (evt.stopPropagation) {
evt.stopPropagation();
} else {
evt.returnValue = false;
}
getNextElement(field).focus();
return false;
} else {
return true;
}
}
</script>
</head>
<body onLoad="document.chip_insert.chip_number.focus();">
<center>
<h1>Jeffers HomeAgain Microchip Entry</h1>
<form name="chip_insert" id="chip_insert" action="<?php echo $PHP_SELF;?>" method="post" >
Order Number: <input tabindex="1" maxlength="12" type="text" name="order_number" id="order_number" value="<?php echo $value; ?>" required="required"onkeydown="return tabOnEnter(this,event)" /><br /><br />
Tag Number: <input tabindex="2" maxlength="15" type="text" name="chip_number" id="chip_number" required="required" /><br /><br />
<input tabindex="7" type="submit" />
</center>
</form>
</body>
</html>
答案 0 :(得分:2)
无论何时打开此页面,无论是否有帖子,都会插入数据库。 因为没有帖子控制(发布或不控制)下面的代码块:
IF (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
这部分应该是这样的:
if(isset($_POST)){
IF (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
}
答案 1 :(得分:0)
你的php部分应该是这样的
<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "JVSIntranet";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$value = "";
if (isset($_POST ["order_number"])) {
$value = $_POST ["order_number"];
$sql = "INSERT INTO MICROCHIP_TBL (chip_number,order_number)
VALUES
('$_POST[chip_number]','$_POST[order_number]')";
if (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
}
mysqli_close($conn);
?>
并且html </center>
应该在</form>
标记之后,尽管它不是大问题
答案 2 :(得分:0)
此代码还有一些问题......
IF (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
应该像
if(isset($_POST['chip_number'] && $_POST['chip_number'] != '')
{
IF (mysqli_query($conn, $sql)) {
echo "Great Job! You've entered it correctly!";
} else {
echo "Error: TRY AGAIN HUMAN!";
}
}
和
$sql = "INSERT INTO MICROCHIP_TBL (chip_number,order_number)
VALUES
('$_POST[chip_number]','$_POST[order_number]')";
应该有点像
$sql = "INSERT INTO MICROCHIP_TBL (chip_number,order_number)
VALUES
('{$_POST['chip_number']}','{$_POST['order_number']}')";
甚至最后一件事都不是正确的类型来称呼它...... 你可以查看http://php.net/manual/en/mysqli-stmt.bind-param.php
实际上会是什么样的
$stmt = mysqli_prepare($link, "INSERT INTO MICROCHIP_TBL VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, 'dd', $_POST['chip_number'], $_POST['order_number']);
//....
mysqli_execute();
或
$stmt = mysqli_prepare($link, "INSERT INTO MICROCHIP_TBL SET chip_number=?,order_number=?");
mysqli_stmt_bind_param($stmt, 'dd', $_POST['chip_number'], $_POST['order_number']);
//....
mysqli_execute();
这些样本不是像$ db = new mysqli(...)这样的对象构建的;但你应该考虑以这种方式使用它:)