php - 当使用带有 HTML 下拉框选择器的 PHP 时,它给了我一个未定义的错误?
问题描述
我正在做一个 php 表单验证程序,但是我遇到了一个我似乎无法解决的奇怪问题,即使我声明了所有变量,它仍然给我一个通知:未定义索引:C:\Users\andrew\Desktop 中的餐馆\CPSC 2030\Xampp\htdocs\Assignment3\VisitForm.php 第 28 行错误,如果我忽略错误,它根本不会填写该字段,下拉框不起作用....这是我的主文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width-device-width, intial-scale-1.0">
<link rel="stylesheet" href="styles/main.css">
<title>Find rest - add Visit</title>
</head>
<nav>
<a href="index.html">Home</a>
</nav>
<div class="left"></div>
<div class="right"></div>
<div class="wrapper">
<body>
<?php
//php invalid.
if($_SERVER["REQUEST_METHOD"] == "POST"){
$error_rest = "";
echo "There is invalid data on your form \r\n";
if(!$validName){
echo "Restaurant must be selected!! \r\n";
$error_rest='border: red 5px solid';
}
?>
<form method="post" action="VisitForm.php">
<table border="1px">
<tr class="repeat_example">
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr class="repeat_example">
<td>restaurant <br>
<select id="restaurants" value="<?php echoIfExists($_POST,"restaurants","")?>"
style="<?php echo $error_rest; ?>">
<option value="556">556(Burger King)</option>
<option value="778">778(A&W)</option>
<option value="666">666(McDonalds)</option>
</select>
</td>
<td>Drop down</td>
<td>Contains restaurant ID as the value and name of the
restaurant is displayed on the page.</td>
</tr>
</table>
</table>
<br><br>
<label> Write new Form!</label><input type="submit">
</form>
<img src="img/Sitemap.PNG" alt="Map of site">
</body>
</div>
</html>
这是我的另一个文件,请注意我试图制作一个最小的示例,所以如果缺少信息,请询问....
<?php
//constants
define("rest", "restaurants");
//Helper function
function valueIfExists($array, $key, $default){
if(array_key_exists($key,$array)){
return $array[$key];
} else {
return $default;
}
}
//validation logic
$isValid = true;
$validName = true;
if($_SERVER['REQUEST_METHOD'] == "POST"){
if($_POST[rest] == ""){
$isValid = false;
$validName = false;
}
}else {
$isValid = false;
}
如果有人可以帮助我,我将不胜感激...
谢谢,
解决方案
您在主文件的标签中缺少name
属性。
所以那行代码将是 -select
<select name="restaurants" id="restaurants" value="<?php echoIfExists($_POST,"restaurants","")?>
所以这不是一个奇怪的问题;)
推荐阅读
- python - 无法通过带有 webdriver 的 xpath 为 Python 上的标签找到元素
- javascript - 使用 Selenium 滚动对网页进行不完整的抓取
- c# - C# .NET Framework 4.8 API 调用不适用于新 URI
- python - 我无法在 Ubuntu 上运行 Python 文件
- r - 为什么我的 ggplot 图没有显示在闪亮上?
- php - 在pdo中查询后查询?绑定问题
- ruby-on-rails - Rails - 网址中的两个或多个点
- ruby-on-rails - Ruby on Rails 中的自引用关联
- google-analytics - 按日期按人口统计的 Google 分析报告
- apache - .htaccess RewriteRule 提供正确的链接,但也提供 404