2017-08-08 34 views
0

謝謝你的任何幫助。 如何向同一行添加另一個條件。我有2個要求,我需要加入又名我有這個加入兩個PHP請求到一個

if ([email protected]$_REQUEST['urEmail']) { $errorsAndAlerts .= "No email entered!<br/>\n"; } 

但我還需要它添加到它

if ([email protected]$_REQUEST['g-recaptcha-response']) 

我已經試過

if ([email protected]$_REQUEST['urEmail']) || ([email protected]$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; } 

if ([email protected]$_REQUEST['urEmail']) && ([email protected]$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; } 

但它工作得更加輕鬆。 我很感激任何幫助。

謝謝

+0

驗證您是否正確發送請求之前。 – KubiRoazhon

+0

不要使用'@'來壓制警告,修復腳本以避免錯誤發生。使用'empty()'函數確保它被填充。您可以將它與'trim()'結合使用來刪除空格,如果是和email,則使用'filter_var($ email,FILTER_VALIDATE_EMAIL)'來驗證它是否是有效的電子郵件模式。 – Rasclatt

+0

您嘗試工作的主要原因是它們都包含語法錯誤,這些錯誤會阻止您的腳本執行。在開發應用程序時,您需要[啓用錯誤報告](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display),以便您可以瞭解這些情況。 –

回答

0

你的條件都需要是if括號內,因爲這樣的:

if ([email protected]$_REQUEST['urEmail'] || [email protected]$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; } 

if ([email protected]$_REQUEST['urEmail'] && [email protected]$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "No email entered!<br/>\n"; } 

一個if構建PHP的結構如下(取自PHP documentation):

if (expr) 
    statement 

而在你的情況下,expr是你的條件,因此你需要把它們都括在括號中。

+0

非常感謝。這非常有幫助 –

0

重申我的意見,不要使用@來壓制警告,修復腳本以避免錯誤發生。使用empty()函數來確保它已填充。您可以將它與trim()結合使用以刪除空格,如果是,請使用filter_var($email,FILTER_VALIDATE_EMAIL)驗證它是否爲有效的電子郵件模式。

例子:

# Check the email is set and trim it 
$email = (isset($_REQUEST['urEmail']))? trim($_REQUEST['urEmail']) : false; 
# Check the recaptcha is set and trim it 
$recap = (isset($_REQUEST['g-recaptcha-response']))? trim($_REQUEST['g-recaptcha-response']) : false; 
# If either are empty 
if(empty($email) || empty($recap)) { 
    $errorsAndAlerts .= "No email entered!<br/>\n"; 
} 
# If both filled but invalid email 
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)) { 
    $errorsAndAlerts .= "Email invalid!<br/>\n"; 
} 
//etc... 

無論如何,作爲@ Don'tPanic提到,要確保你有錯誤的ini_set('display_errors',1); error_reporting(E_ALL);報告,但我懷疑你,因爲你是抑制錯誤/警告與@

最後一個音符,以緩解一些重複,我想也許想保存錯誤的數組,並在年底爆他們:

# Save all errors using the push 
# (mine are in a line, but yours would be throughout your script) 
$error[] = "No email entered"; 
$error[] = "Invalid request"; 
$error[] = "Invalid email"; 
$error[] = "Poor penmanship"; 
# Implode with the glue when you want to output them 
echo implode('!<br />'.PHP_EOL,$error).'!';