我试图回显一个javascript,看看(isset($_POST['action'))是否返回true。但每次我点击按钮时,它都不会违背它应该做的事情。原因是什么?
编辑:我包含了整个代码。这里有什么问题吗?我几乎尝试了所有的建议,但仍然没有奏效。
<div class="container">
<form action="" method="post">
<div class="row">
<div class="input-field col">
<h5>Patient Details</h5>
<div class="row">
<div class="input-field col">
<i class="material-icons prefix">assignment_ind</i>
<input type="text" name="patientName" class="validate">
<label for="patientName">Patient Name</label>
</div>
<div class="input-field col s3">
<select name="bloodType" required>
<option value="" disabled selected>Select Blood Group</option>
<?php
$q = $db->query("select * from tblbloodtype where btStatus = '1' ORDER by bloodTypeName");
while($r = $q->fetch(PDO::FETCH_ASSOC)){
echo"<option value=".$r['bloodTypeID'].">".$r['bloodTypeName']."</option>";
}
?>
</select>
<label>Blood Group</label>
</div>
<div class="input-field col s3">
<select name="city" required>
<option value="" disabled selected>Select City</option>
<?php
$q = $db->query("select * from tblcity where status = '1' ORDER by cityName");
while($r = $q->fetch(PDO::FETCH_ASSOC)){
echo"<option value=".$r['cityID'].">".$r['cityName']."</option>";
}
?>
</select>
<label>City where it is needed</label>
</div>
</div><!--ROW-->
<div class="row">
<div class="input-field col s12 ">
<i class="material-icons prefix">business</i>
<textarea id="hospitalName" class="materialize-textarea"></textarea>
<label for="hospitalName">Hospital Name and Address</label>
</div>
</div>
<div class="row">
<div class="input-field col">
<i class="material-icons prefix">perm_identity</i>
<input type="text" name="doctorName" class="validate">
<label for="doctorName">Doctor's Name</label>
</div>
</div>
<h5>Contact Details</h5>
<div class="row">
<div class="input-field col">
<i class="material-icons prefix">perm_identity</i>
<input type="text" name="contactName" pattern="[a-zA-Z0-9']+" class="validate">
<label for="contactName">Contact Name</label>
</div>
<div class="input-field col">
<input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+'.[a-z]{2,3}$" class="validate">
<label for="email">Contact Email</label>
</div>
<div class="input-field col">
<input type="text" name="contactNum" pattern="[0-9+]{7,13}" class="validate">
<label for="contactNum">Contact Number</label>
</div>
<input class="btn waves-effect waves-light" type="submit" value="Send Request" name="action">
</form>
</div><!--CONTAINER-->
<?php
if(isset($_POST['action'])){
echo "<script>alert('Success! Redirecting to homepage..');</script>";
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
我终于找到了问题,首先感谢您的努力。
正如您在上面的代码中看到的,我在输入标记上放置了一个required
属性。因此,除非我选择其中有值的东西,否则我可以成功地回显警报。
我花了很长时间才注意到,因为它没有显示错误信息,所以我需要选择一些东西。
if(isset($_POST['action'])){
echo "<script>alert('Success! Redirecting to homepage..');</script>";
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
删除$_POST['action']
中的空间
单报价封闭'Success! Redirecting to homepage..'
回波echo "<meta http-equiv='refresh' content='0;url=index.php'>";
后给定的空间
更改
$_REQUEST['action ']
至
$_REQUEST['action']
(注意最后去掉的空格)
同时更改
<script>alert('Success! Redirecting to homepage..);</script>
至
<script>alert('Success! Redirecting to homepage..');</script>
(注意两点后的报价)
仅在标题部分中允许使用元标记。尝试删除此行:
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
您可以在正文部分使用meta,如下所示用于语义目的:
<meta itemprop="ratingValue" content="4" />
而且,正如这里所指出的http://schema.org/docs/gs.html#advanced_missing
应谨慎使用此技术。仅将meta与内容一起使用以获取无法标记的信息。
所以你必须避免这种情况。
编辑:
前3个div没有结束标记。关闭这些潜水器。不关闭它们,<script>
标签由php打印在标签内。
也替换此:
echo"<option value=".$r['bloodTypeID'].">".$r['bloodTypeName']."</option>";
这个:
echo "<option value='".$r['bloodTypeID']."'>".$r['bloodTypeName']."</option>";
(注意value属性中的单引号)
也是如此
echo"<option value=".$r['cityID'].">".$r['cityName']."</option>";
替换为:
echo "<option value='".$r['cityID']."'>".$r['cityName']."</option>";
(注意回波之间的空格)
删除$_POST['action ']
到$_POST['action']
中的空格。