我正在构建一个表单,以便用户可以添加自己的产品以在网站上销售。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>