这是一个五星评级表,我正在尝试使它工作。
<div class="stars">
<form>
<input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
<label class="star star-1" for="star-1"></label>
<button class="button" type="submit" name="submit" value="submit">Submit</button>
</form>
</div>
当我按下提交按钮时没有任何反应。代码不会移动到if语句中,但是因为submit应该不再为null。
<?php
if(isset($_POST['submit'])){
$rating = $_POST['star'];
echo "hello";
echo $rating;
}
?>
答案 0 :(得分:2)
设置表单method=post
如果同一页面中的html和php代码使用
设置表单action=""
否则使用
设置表单action=".php file"
,例如:<form method="post" action=" or .php file">
答案 1 :(得分:1)
您只是错过了表单中的方法和操作形式。
使用此
<form method="post" action="" >
并在PHP代码上使用
if(isset($_POST['star']))
它会起作用
答案 2 :(得分:0)
要使用超全局变量$ _POST,您需要使用提交按钮为表单标记指定方法属性。 将您的表单标记和提交按钮标记更改为:
<div class="stars">
<form method="POST" name="starRatingForm" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
<label class="star star-1" for="star-1"></label>
<input type="submit" name="submit" value="submit"/>
</form>
</div>
答案 3 :(得分:0)
isset()
检查变量的值是否包含( False , 0 , or empty string)
,但不是NULL。如果变量存在则返回TRUE,否则返回FALSE
。
另一方面,empty()
函数检查变量是否有empty value, empty string , 0, NULL ,or False
。如果变量具有FALSE
和non-empty
值,则返回non-zero
。
if(!empty($_POST['star'])){
....
}
答案 4 :(得分:-1)
您必须在表单标记中设置方法和操作属性。
<form method='post' action='page.php'>