2013-08-05 65 views
3

我正在構建表單,以便用戶可以添加自己的產品在網站上銷售。 JavaScript應檢查是否所有字段都填寫完畢,儘管它不起作用。爲什麼不運行JavaScript?PHP isset不能用於空

PHP稍微複雜一些。該網站以不同包裝銷售產品以允許批發和零售定價,因此可爲不同數量的單位提供多種定價選項。但是,如果填寫了一個可選的定價選項或單位數字,但相應的值不是這樣,我認爲它會導致問題,並且除了用戶輸入錯誤外沒有理由出現這種情況。

爲了避免這種情況,如果設置price2,並且units2爲空,我希望表單被拒絕,並且「您需要填寫每個包選項的單位和價格」以得到迴應。對於逆向和價格3和單位3也是如此。

這就是PHP應該做的事情。但是,每次都會觸發第一個「if」,所以「您需要填寫每個包裝選項的單位和價格」。爲什麼這不正確運行?

<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()'缺少結束括號' –

+0

爲什麼這會被低估? – pattyd

回答

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.'); 
    } 
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. 
+0

我不知道如何使問題更清楚......這是行不通的,因爲我希望這些字段是可選的。但是,如果填寫了(價格2),則也必須填寫相應的字段(單位2)。 – EveyPortman

+0

您是否可以使您的問題更清晰?因爲我與選項1,2,3,4混淆,因爲在你的HTML它是可選的。它與選項 –

+0

有所不同。這些數字只是讓我可以確定一個人是否正在清理,但另一個人卻不是。對不起,我想出瞭如何解決我的問題,如果您有興趣,請參閱我的答案。 – EveyPortman