gpt4 book ai didi

javascript - 接受错误后重新加载页面

转载 作者:行者123 更新时间:2023-12-03 10:55:07 26 4
gpt4 key购买 nike

我有一个 PHP 页面,它会在出现错误时生成 JavaScript 警报。

    if (strpos($this->color,':') !== false) {
echo '<script type="text/javascript">alert("Please use the RBG format of 255:255:255 for color.");</script>';
echo '<script type="text/javascript">location.reload();</script>';
die($conn);
}
正如您可能猜测的那样,我想在收到错误后重新加载页面,而不是继续处理 PHP 的其余部分,这实际上会插入错误的数据。以上导致刷新后无限循环并且继续弹出警报。我怎样才能收到警报一次,刷新页面,然后继续?

编辑

完整代码:

<!DOCspecies html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-species" />
<title>Untitled 1</title>
</head>

<?php

$servername = "localhost";
$username = "kevin";
$password = "ally";
$database = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$getTableQuery = "SELECT tbf.Id, tbf.Name, tbf.Size, tbf.Color, tbs.Name as Species, tbs.Description
FROM tbl_fish as tbf INNER JOIN
tbl_species as tbs ON tbf.Species = tbs.Id
ORDER BY tbf.Id";

$table = $conn->query($getTableQuery);

if ($table->num_rows > 0) {
echo "<table border='1'><tr><th>Name</th><th>Size</th><th>Color</th><th>Species</th></tr>";
// output data of each row
while($row = $table->fetch_assoc()) {
echo "<tr><td>".$row["Name"]."</td><td>".$row["Size"]."</td><td>".$row["Color"]."</td><td>".$row["Species"]."</td></tr>";
}
echo "</table>";
echo "</br>";
} else {
echo "0 results";
}

if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert"))
{
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txtSpecies'], $_POST['txtDescription']);

$Dog->InsertDog($conn);
}

class Dog
{
private $name = "Dog Name";
private $size = 0;
private $color = "255:255:255";
private $speciesName = "Species Name";
private $speciesDescription = "Species Description";

public function Dog($name, $size, $color, $species, $description){
$this->name = $name;
$this->size = $size;
$this->color = $color;
$this->speciesName = $species;
$this->speciesDescription = $description;
}

private function ColorCheck($color){
if($color > 256 || $color < 0)
return false;
else
return true;
}

public function InsertDog($conn){
$this->speciesName = mysqli_real_escape_string($conn, $this->speciesName);
$this->speciesDescription = mysqli_real_escape_string($conn, $this->speciesName);
$this->name = mysqli_real_escape_string($conn, $this->name);
$this->size = mysqli_real_escape_string($conn, $this->size);
$this->color = mysqli_real_escape_string($conn, $this->color);
$_SESSION['reloaded'] = false;
$color = explode(':', $this->color);

if (strpos($this->color,':') !== false && !isset($_SESSION['reloaded'])) {
echo '<script type="text/javascript">alert("Please use the RBG format of 255:255:255 for color.");</script>';
echo '<script type="text/javascript">location.reload();</script>';
die($conn);
}

if(!$this->ColorCheck($color[0]) || !$this->ColorCheck($color[1]) ||!$this->ColorCheck($color[2]) && !isset($_SESSION['reloaded'])){
echo '<script type="text/javascript">alert("Please use the RBG format of 255:255:255 for color.");</script>';
echo '<script type="text/javascript">location.reload();</script>';
die($conn);
}

$speciesId = "SELECT Id from tbl_species WHERE Name = '$this->speciesDescription'";
$speciesInsert = "INSERT IGNORE INTO tbl_species (Name, Description)
VALUES ('$this->speciesName', '$this->speciesDescription')";

$result = mysqli_query($conn, $speciesInsert) or die("Query fail: " . mysqli_error($conn));

if($id = $conn->query($speciesId)){
$row = $id->fetch_assoc();
$intId = $row['Id'];
}

$DogInsert = "INSERT INTO tbl_fish (Name, Size, Color, Species)
VALUES ('$this->name', $this->size, '$this->color', $intId)";
$result2 = mysqli_query($conn, $DogInsert) or die("Query fail: " . mysqli_error($conn));

unset($this);
}

public function UpdateDog(){
}
}
$conn->close();
?>

<body>

<form action="index.php" method="post">
Dog Name:<br />
<input name="txtName" type="text" /><br />
<br />
Size:<br />
<input name="txtSize" type="text" /><br />
<br />
Color:<br />
<input name="txtColor" type="text" /><br />
<br />
Species Name:<br />
<input name="txtSpecies" type="text" /><br />
<br />
Species Description:<br />
<input name="txtDescription" style="width: 419px; height: 125px" type="text" /><br />
<br />
<input name="btnInsert" type="submit" value="Insert" />
<input name="btnUpdate" type="button" value="Update" />
</form>

</body>

</html>

最佳答案

location.reload 就像点击浏览器中的“刷新”按钮一样,这意味着表单将重新发布,并且您每次都会直接返回到此 block :

if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert"))
{
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txtSpecies'], $_POST['txtDescription']);

$Dog->InsertDog($conn);
}

使用它,因为它更像是一个干净的页面加载:

window.location = window.location.href;

关于javascript - 接受错误后重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28286510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com