我正在构建一个表单,以便用户可以添加自己的产品在网站上销售。 javascript应该检查是否所有字段都填写完毕,但它不起作用。为什么javascript不运行?
PHP有点复杂。该网站以不同的包装销售产品以允许批发和零售价格,因此可以为不同数量的单位提供多种定价选项。但是,如果填写了其中一个可选的定价选项或单位编号,但相应的值没有填写,我认为这会导致问题,并且除了用户错误之外没有理由发生这种情况。 / p>
为了避免这种情况,如果设置了price2,并且units2为空,我希望拒绝该表单,并且“需要为每个包选项填写单位和价格”以进行回显。对于逆和price3以及unit3也是如此。
这就是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>
答案 0 :(得分: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.
答案 1 :(得分: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.');
}