2015-07-11 49 views
1

我正在從html調用PHP函數,我想確保當相應的輸入框爲空時發生一件事,當沒有時發生其他事情。 由於某種原因,PHP認爲它總是不爲空。無法弄清楚爲什麼。PHP獲取空參數

HTML片段:

<input type="button" 
value="checkout" 
onClick="self.location='testphp.php?checkout=true&folderName=\'' + document.folderForm.folderLocation.value + '\''"> 

</button> 
<form enctype="multipart/form-data" method="post" name="folderForm"> 
<input type=file name="myfile"> 
<input type=text name="folderLocation" id="folderLocation"> 
<input type=hidden name="folderName"> 
<input type=button value="Get Folder" onclick="javascript:GetFolder();"> 
<input type=hidden name=submitted value=1> 
<input type="submit" value="Create Repository" /> 
</form> 

PHP代碼片段:

if($_GET['checkout']){ 
    if(isset($_GET['folderName']) && !empty($_GET['folderName'])) { 
     echo $_GET["folderName"]; 
     echo exec("/var/www/html/checkout.sh ".$_GET["folderName"]); 
    } else { 
     echo "<script type='text/javascript'>alert('Please choose repository from the list first');</script>"; 
    } 
} 

什麼想法?

回答

0

嘗試更改!isempty($_GET['folderName'])strlen($_GET['folderName']) > 0

編輯在這種情況下,問題是由'在當folderName是空白導致$_GET仍然包含兩個引號的folderName參數引起的。

+0

謝謝,但它沒有幫助。它總是達到「真正的」部分。 – user2880391

+0

當您將它添加到查詢字符串中的'folderName'參數時,您不需要轉義該URL或將其放入引號中。相反,您應該使用類似於[詳細說明](http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript)的方法,通過JavaScript將其編碼爲HTML實體。 –

+0

逃脫解決它。我猜這總是發送。不確定HTML編碼。謝謝!順便說一句,我仍然在使用'!empty'。 – user2880391