php - 控件不进入 if(isset()))
问题描述
下面是我的表单的 html 代码和我用来将数据传递给类方法的 php 代码。现在我遇到的问题是控件似乎没有进入我通过测试得出的 if 循环,你可以见。“test0”被打印,但“test1”和其他后续“测试”没有被打印。
<form action="" method="post" enctype=multipart/form-data>
<div class="form-group">
<label for="job name">Job name:</label>
<input type="text" class="form-control" id="jobnm" value="<?php echo $_GET['jobnm'];?>" disabled>
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="mail" required>
</div>
<div class="form-group">
<label for="phone">Enter a phone number:</label><br><br>
<input type="tel" id="phone" name="phone" placeholder="+91-1234567890" pattern="[0-9]{10}" required><br><br>
<small>Format: 1234567890</small><br><br>
</div>
<label >Gender</label>
<div class="radio">
<label><input type="radio" name="optradio" value="m">Male</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio" value="f">Female</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio" value="o">Other</label>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="cvFile" required>
<label class="custom-file-label" for="customFile">Upload resume</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-danger" >Reset</button>
</form>
<?php
require_once 'db-config.php';
require_once 'classCandi.php';
echo "test0";
if(isset($_POST['submit']))
{
echo "test1";
$jobID = $_GET['jobid'];
echo "test2";
$canName = $_POST['name'];
$canEmail = $_POST['mail'];
$canPhone = $_POST['phone'];
$canRadio = $_POST['optradio'];
echo "test3";
//Upload file
$fnm = "cv/";
$cvDst = $fnm . basename($_FILES["cvFile"]["name"]);
move_uploaded_file($_FILES["cvFile"]["tmp_name"],$cvDst);
echo "test4";
$obj = new Candi($conn);
$obj->storeInfo($jobID,$canName,$canEmail,$canPhone,$canRadio,$cvDst);
echo "test5";
echo '<script language="javascript">';
echo 'alert("Submitted");';
echo '</script>';
echo "test6";
}
解决方案
下面的代码在任何时候都不会是真的!那是因为你不明白它是如何$_POST
工作的。
if(isset($_POST['submit']))
您的前端中没有输入元素具有name="submit"
. 并且看到,根本没有任何输入具有name
属性。
相反,更好的方法是了解其工作原理并更改您的代码,使其包括:
- 所有输入和表单元素的
name
属性。 - 检查值而不是
$_POST['submit']
最后...
- 不要在不了解代码的情况下复制和粘贴。
- 不要检查
$_POST['submit']
真实性。
例如,$canName = $_POST['name'];
要工作,您需要:
<input type="text" name="name" id="name" value="<?php echo $something; ?>" />
// ^^^^^^^^^^^
并将您的属性和值用引号引起来:
enctype="multipart/form-data"
// ^ ^
推荐阅读
- thymeleaf - thymeleaf 属性“?” 在元素中出现多次
- python - 如何在远程 shell 的上下文中进行 pdb 的 stdin/stdout 重定向?
- javascript - 有没有办法在 Nuxt 中为缩小的 JS 脚本标签添加属性?
- javascript - 无限循环页面
- reactjs - 如何触发深度嵌套子组件的刷新
- java - 为什么 com.fasterxml.jackson.core.type.TypeReference 实现 Comparable 接口?
- python - 如何使它只有某些人才能发消息?
- mongodb - 在多个字段聚合中排序
- capl - CAPL:动态改变对ECU的CAN ID请求
- c - C语言中的异或运算