PHP中isset与empty一起使用无效

3
我正在创建一个表单,以便用户可以在网站上添加自己的产品进行销售。虽然javascript应该检查所有字段是否都填写了,但它却没有工作。为什么javascript没有运行?
PHP有点复杂。这个网站以不同的包装方式销售产品,以允许批发和零售价格,因此针对不同数量的单位提供多种定价选项。然而,如果其中一个可选的定价选项或单位号码被填写,但相应的值没有填写,我认为这将在以后引起问题,而且除了用户输错之外,没有任何原因会出现这种情况。
为了避免这种情况,如果设置了price2,并且units2为空,则我希望拒绝该表单,并回显“您需要填写每个包装选项的单位和价格”。对于逆向操作和price3和units3也是一样的。
这就是PHP应该执行的任务。然而,第一个“if”每次都会触发,因此会回显“您需要填写每个包装选项的单位和价格1”。为什么这个不能正确运行?
<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>

1
尝试使用单引号来表示变量A、B、C、D等... 我看到你使用了双引号。 - pattyd
4
你忘记了 function validproform() 的结尾括号。 - Marcel Gwerder
1
你可以将那个 if 语句简化为:if (!a||!b||!c||!d||!e||!f) - elclanrs
我加了一个闭括号,现在 JS 可以工作了。谢谢! - EveyPortman
你的JS可能正常工作,但是使用这段代码时你的PHP永远不会按预期工作。请查看下面的答案。 - garrettmurray
2个回答

1
   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.

我不确定如何让问题更清晰...这样做行不通,因为我希望那些字段是可选的。但是,如果其中一个被填写了(price2),相应的字段也必须被填写(units2)。 - EveyPortman
你能让你的问题更清晰一些吗?因为我对选项1、2、3、4感到困惑,因为在你的HTML中它是可选的,这与选项不同。 - Dante Ditan
这些数字只是为了让我能够确定一个是否清除,而另一个是否没有。抱歉,我已经找到解决问题的方法了,如果你感兴趣可以看看我的答案。 - EveyPortman
做得好。你必须了解程序的流程才能自己解决它。做得好! - Dante Ditan

0

再次更新:

好的,我做了一些实验。简单地点击提交按钮似乎会“设置”$_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.');
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接