PHP不适用于空
PHP isset not working with empty
我正在构建一个表单,以便用户可以添加自己的产品在网站上销售。javascript应该检查是否填写了所有字段,尽管它不起作用。为什么javascript没有运行?
PHP稍微复杂一些。该网站以不同的包装销售产品,以允许批发和零售定价,因此不同数量的单元有多种定价选项。然而,如果填写了其中一个可选的定价选项或单位编号,但没有填写相应的值,我认为这会导致后续问题,除了用户打字错误之外,没有其他原因导致这种情况发生。
为了避免这种情况,如果设置了price2,而units2为空,我希望表单被拒绝,并回复"您需要为每个套餐选项填写单位和价格"。倒数、price3和units3也是如此。
这就是PHP应该做的。然而,第一个"if"每次都会触发,所以"你需要为每个包填写单位和价格option1"会出现回声。为什么运行不正确?
<script>
function validproform()
{
var a=document.forms["productentry"]["pname"].value;
var b=document.forms["productentry"]["pword"].value;
var c=document.forms["productentry"]["about"].value;
var d=document.forms["productentry"]["unitabout"].value;
var e=document.forms["productentry"]["price1"].value;
var f=document.forms["productentry"]["units1"].value;
if (a==null || a=="" || b==null || b=="" || c==null || c=="" || d==null || d=="" || e==null || e=="" || f==null || f=="")
{
alert("Required fields are not all filled out.");
return false;
}
</script>
<?php
if (isset($_POST['psubmit'])) {
if(isset($_POST['price2']) and empty($_POST['units2'])){
echo('You need to fill out both units and prices for each package option1');
}
elseif(isset($_POST['units2']) and empty($_POST['price2'])){
echo('You need to fill out both units and prices for each package option2');
}
elseif(isset($_POST['price3']) and empty($_POST['units3'])){
echo('You need to fill out both units and prices for each package option3');
}
elseif(isset($_POST['units3']) and empty($_POST['price3'])) {
echo('You need to fill out both units and prices for each package option4');
}
}
?>
<form name="productentry" action="" method="post" onsubmit="return validproform()">
Product Name: <input type="text" name="pname" maxlength="30"><br>
About: <input type="text" name="about" maxlength="250"><br>
Describe your product. What is unique about it, what does it do, what sets it apart?<br>
What each unit contains: <input type="text" name="unitabout" maxlength="250"><br>
For example: Are you selling, "A bag of chips" or "A box w/ 30 bags of chips" or "a carton of 10 boxes with 30 bags of chips each." Describe what one unit contains.<br>
Price: <input type="text" name="price1" maxlength="30"><br>
Number of units: <input type="text" name="units1" maxlength="30"><br>
----
Price 2(optional): <input type="text" name="price2" maxlength="30"><br>
Number of units(optional): <input type="text" name="units2" maxlength="30"><br>
Price 3(optional): <input type="text" name="price3" maxlength="30"><br>
Number of units(optional): <input type="text" name="units3" maxlength="30"><br>
Password: <input type="password" name="pword" maxlength="30"><br>
<input type="submit" name="psubmit">
</form>
Try this code:
<?php
if (isset($_POST['psubmit']))
{
if(!$_POST['price2'])
{
echo('You need to fill out both units and prices for each package option1');
}
elseif(!$_POST['units2'])
{
echo('You need to fill out both units and prices for each package option2');
}
elseif(!$_POST['price3'])
{
echo('You need to fill out both units and prices for each package option3');
}
elseif(!$_POST['price3'])
{
echo('You need to fill out both units and prices for each package option4');
}
}
?>
The !$_POST will the return if the value is FALSE.
再次更新:
好吧,我做了一些实验。只需单击提交按钮即可"设置"$_POST
变量。因此,即使字段是空的,服务器仍然认为它是"设置的"
因此,检查变量是否有值是正确的方法。
谁会想到我回答的第一个问题会是我自己的哈哈?
下面的代码比我原来的答案工作得更好,也更干净:
elseif($_POST['price2'] != 0 and empty($_POST['units2'])){
echo('You need to fill out the number of units corresponding to price 2.');
}
elseif($_POST['units2'] != 0 and empty($_POST['price2'])){
echo('You need to fill out the price corresponding to your second unit option.');
}
elseif($_POST['price3'] != 0 and empty($_POST['units3'])){
echo('You need to fill out the number of units corresponding to price 3.');
}
elseif($_POST['units3'] != 0 and empty($_POST['price3'])) {
echo('You need to fill out the price corresponding to your third unit option.');
}
相关文章:
- HTML5 Canvas toDataURL(“image/png”)适用于一些绘画,而有些则不然,不确定原因
- JavaScript不显示剩余字符,但适用于其他页面
- 我如何完善这个Javascript代码,使其只适用于来自图像的链接(而不是来自文本的链接)
- 为什么这只适用于身体部分而不是头部
- 为什么不't此正则表达式适用于重复出现的对
- document.getElementById 对内部 html 的调用适用于 $_POST 和 $_GET 变量,但不
- JavaScript 位置适用于本地主机而不是服务器
- Rally App SDK 2.0:Rallymultiobjectpicker,适用于不显示displayField的
- jQuery事件在Mozilla上不起作用,并且适用于其他浏览器
- Angular.js - 控制器不为索引执行,适用于其余模板
- D3 .on 更改适用于文本而不是图表
- Zillow 数据 - json_encode不起作用 - 适用于常规变量
- 弹出模式仅适用于表格中的第一个元素,第二次单击后不会再次打开
- Phonegap Android项目适用于模拟器,而不是实际的手机
- ajax jonp 不应该适用于 请求的上不存在“访问控制-允许源”标头
- AngularJS:ng-单击不起作用,适用于<按钮>
- Javascript不会在移动设备上加载.适用于桌面
- 为什么我的代码适用于高度:100px,而不是高度:100%
- 拖放区.js + Laravel-多文件上传不起作用(仅适用于一个文件)
- 哪些限制适用于不透明响应