我正在使用 url 发送一个 id。但是,我只能在页面加载时访问 $GET['id'] 变量。当我继续提交表单时,变量未定义。可能是什么问题呢
if(isset($_GET['id'])) {
$tripId = htmlspecialchars($_GET['id']);
}
if (isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['package']); $i++) {
//check if there are any errors before proceeding
if(array_filter($errors)) {
print_r($errors);
} else {
$package = mysqli_real_escape_string($con, $_POST['package'][$i]);
$price = mysqli_real_escape_string($con, $_POST['price'][$i]);
$sql = "INSERT INTO packages(tripId,packageName,packagePrice) VALUES('$tripId','$package','$price')";
}
//check and redirect
if(mysqli_query($con, $sql)) {
header('location: admin_home.php');
} else {
echo 'error' . mysqli_error($con);
}
}
}
构建表单时,目标也应包含 GET 参数。这就是 GET 参数的传递方式。$_POST
将填充表单字段,$_GET
将填充 url 中的任何内容。
<?php
$url = '/process.php?' . http_build_query(['id' => $yourIdHere]);
?>
<form action="<?=$url;?>" method="POST">
<label for="package">Package:</label>
<input type="text" id="package" name="package"><br><br>
<label for="price">Price:</label>
<input type="text" id="price" name="price"><br><br>
<input type="submit" value="Submit" name="submit">
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句