当我第一次进入我的表单时,它会调用我的php函数并显示输入空白的错误。我如何停止它,只在我提交而不是在页面加载时调用该函数?我觉得我需要做一个" if(isset"某个地方,但我无法弄清楚。
<head>
<html>
<center>
<body onLoad="document.chip_insert.chip_number.focus()";>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
function validateName(){
var x=document.forms["chip_insert"]["order_number"].value;
if (x.length<=10) {
document.forms["chip_insert"]["order_number"].focus();
document.getElementById("order_number").style.border='3px solid red';
document.getElementById("erron").innerHTML = "Invalid";
return false
}
if (x.length=11) {
document.getElementById("order_number").style.border='1px solid black';
document.getElementById("erron").innerHTML = "";
}
if (x==null || x=="")
{
document.forms["chip_insert"]["order_number"].focus();
document.getElementById("order_number").style.border='3px solid red';
document.getElementById("erron").innerHTML = "Invalid";
return false;
}
}
function validateForm(){
var y=document.forms["chip_insert"]["chip_number"].value;
if (y.length<=14) {
document.forms["chip_insert"]["chip_number"].focus();
document.getElementById("chip_number").style.border='3px solid red';
document.getElementById("errcn").innerHTML = "Invalid";
return false
}
if (y==null || y=="")
{
document.forms["chip_insert"]["chip_number"].focus();
document.getElementById("chip_number").style.border='3px solid red';
document.getElementById("errcn").innerHTML = "Invalid";
return false;
}
}
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
function isaNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
window.addEventListener('keydown', keydownCallback);
function tabOnEnter(field, event) {
if (event.keyCode === 13) {
if (event.preventDefault) {
event.preventDefault();
} else if (event.stopPropagation) {
event.stopPropagation();
} else {
event.returnValue = false;
}
getNextElement(field).focus();
return false;
} else {
return true;
}
}
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 keydownCallback(event) {
if (event.keyCode === 191) {
setTimeout(setFocusToTextBox);
}
}
function setFocusToTextBox() {
document.getElementById("order_number").focus();
document.forms["chip_insert"]["order_number"].value="";
document.forms["chip_insert"]["chip_number"].value="";
if (event.preventDefault) {
event.preventDefault();
} else if (event.stopPropagation) {
event.stopPropagation();
} else {
event.returnValue = false;
}
}
</script>
</center>
<center>
<div
style="width:1000px;
height:300px;
border:6px ridge
blue;">
<?php
$value = "";
if( isset( $_POST ["order_number"] )) $value = $_POST ["order_number"];
?>
</script>
<style>
h1 {
font-size: 22pt;
font-family: verdana;
}
</style>
<h1>Jeffers HomeAgain Microchip Entry</h1>
<style>
body {
background-image: url("http://JVSIntranet/microchip/image.jpg");
}
</style>
<form name="chip_insert" id="chip_insert" action="<?php echo $PHP_SELF;?>" onsubmit="return validateForm()" method="post" onkeydown="keydownCallback(event)">
<label style="font-size:18pt; color:blue; font-family:verdana;" for="order_number">Order Number</label><br>
<input tabindex="1" style="height:40px;font-size:16pt; font-family:verdana;" maxlength="11" type="text" name="order_number" onblur="validateName(order_number)" id="order_number" value="<?php echo $value; ?>" onkeypress="return isNumber(event)" required="required" onkeydown="return tabOnEnter(this, event)" onfocus="this.focus();this.select()" /><span id="erron"></span> <br /><br />
<label style="font-size:18pt; color:blue; font-family:verdana;" for="chip_number">Chip Number</label><br>
<input tabindex="2" style="height:40px;font-size:16pt; font-family:verdana;" maxlength="15" type="text" name="chip_number" id="chip_number" required="required" onkeydown="validateName()" onfocus="this.focus();this.select()" onkeypress="return isaNumber(event)" /><span id="errcn"></span> <br /><br />
<input tabindex="7" type="submit" value="Enter" />
</form>
</center>
<center>
<style>
font { color: red; font-size: 25pt; font-family: verdana; }
</style>
<font>
<?php
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname"
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
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 "ERROR: NUMBER IS ALREADY IN DATABASE! SCAN YOUR CHIP NUMBER AGAIN!";
}
mysqli_close($conn);
?>
</center>
</font>
<center>
<?php
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sqli = "SELECT * FROM MICROCHIP_TBL ORDER BY entry_date desc LIMIT 20";
$result = $conn->query($sqli);
if ($result->num_rows > 5) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " " . $row["entry_date"] . "::You scanned Order Number--" . $row["order_number"] . "--with Chip Number--" . $row["chip_number"]. "<br>";
}
}else{
echo "0 results";
}
$conn->close();
?>
</center>
</div>
</body>
</head>
</html>
答案 0 :(得分:4)
你可以这样做:
if (isset($_POST['submit'])) {
// code to execute on submit
} else {
// code to execute on first request
}
// code to always execute
假设&#39;提交&#39;是提交按钮的名称,表单方法是post。
答案 1 :(得分:0)
我看到的一种方法是将隐藏字段放入表单中,例如:
<input type="hidden" name="IsSubmitted" id="IsSubmitted" value="1" />
然后在您的代码中检查:
if (isset($_POST["IsSubmitted"]) && $_POST["IsSubmitted"] != "")
{
// Do you stuff
}