【发布时间】:2015-04-27 18:24:49
【问题描述】:
我的代码遇到问题
我正在使用materialize创建这个网页,但是按钮类型提交没有做任何事情,而输入类型提交运行PHP函数
在填写输入并按下输入类型提交后,它会运行 PHP 代码并打印“PHP 函数已执行”,这意味着我的变量设置为“”而不是其输入值
谁能帮帮我
<!DOCTYPE HTML>
<html>
<head>
<title>Mobile Phone</title>
<meta name="viewport" content="width=device-width, initil-scale=1.0">
<link href="css/materialize.min.css" rel="stylesheet">
</head>
<body class="green lighten-5">
<?php
// define variables and set to empty values
$MID = $MModel = $Color = $Price ="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$MID = test_input($_POST["MID"]);
$MModel = test_input($_POST["MModel"]);
$Color = test_input($_POST["Color"]);
$Price = test_input($_POST["Price"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/materialize.min.js"></script>
<div class="container">
<div class="row">
<form class="col s12" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="row">
<div class="input-field col s2">
<input placeholder="A11" id="MID" type="text" pattern="[A-Za-z]{1}[0-9]{2}" class="validate">
<label for="MID">Mobile ID</label>
</div>
<div class="input-field col s2">
<input id="MModel" type="text" class="validate">
<label for="MModel">Mobile Model</label>
</div>
<div class="input-field col s2">
<input id="Color" type="text" class="validate">
<label for="Color">Color</label>
</div>
<div class="input-field col s2">
<input id="Price" type="number" class="validate" min="0" max="5000">
<label for="Price">Price</label>
</div>
</div>
<!-- This do not works -->
<button class="waves-effect waves-light btn" type="submit" name="action">Add
<i class="mdi-content-send right"></i>
</button>
<!-- This works -->
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
<?php
if (isset($_POST['submit']))
{
echo "PHP function is executed<br>";
echo $MID;
echo $MModel;
echo $Color;
echo $Price;
}
?>
</body>
</html>
【问题讨论】:
-
A: - 您的 POST 数组超出范围,您的输入没有名称属性,只有您的提交有。所以,只有这个会在点击提交按钮时弹出
echo "PHP function is executed<br>";。 -
为了证明这一点 ^ - 在您的打开 PHP 标记之后添加错误报告到您的文件顶部,例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);然后是其余代码,您将在其中看到许多未定义索引... 通知,然后参考stackoverflow.com/questions/4261133/… 了解如何自己解决。没有更好的方法来做到这一点,然后“教一个人如何钓鱼......”;-) -
一旦您设法使用上面的 cmets 解决了这个问题,您就可以删除该问题。如果您仍然有困难,请告诉我们。
-
感谢 Fred 现在可以使用了。它缺少名称属性
-
不客气,很高兴听到它,干杯
标签: php html forms materialize