2010-07-06 11 views

risposta

58

Qualcosa di simile ($ login || $ password!!):

// Required field names 
$required = array('login', 'password', 'confirm', 'name', 'phone', 'email'); 

// Loop over field names, make sure each one exists and is not empty 
$error = false; 
foreach($required as $field) { 
    if (empty($_POST[$field])) { 
    $error = true; 
    } 
} 

if ($error) { 
    echo "All fields are required."; 
} else { 
    echo "Proceed..."; 
} 
+3

Ancora una volta, raccomando un isSet ($ _ POST [$ campo]). Questa è una buona soluzione, però. – Borealid

+0

Grazie Harold, era quello che stavo cercando ... – FFish

+8

vuoto() controlla sia i valori di esistenza che quelli non falsi (null, false, 0, stringa vuota). –

1

empty e isset dovrebbe farlo.

if(!isset($_POST['submit'])) exit(); 

$vars = array('login', 'password','confirm', 'name', 'email', 'phone'); 
$verified = TRUE; 
foreach($vars as $v) { 
    if(!isset($_POST[$v]) || empty($_POST[$v])) { 
     $verified = FALSE; 
    } 
} 
if(!$verified) { 
    //error here... 
    exit(); 
} 
//process here... 
+0

È necessario anche un isSet, penso, altrimenti si otterrà un errore se il valore non è stato pubblicato affatto. – Borealid

0

Personalmente estrarre la matrice POST e quindi avere se then echo compilare il modulo :)

+2

Awww, sempre una pratica pericolosa, perché potrebbe essere possibile introdurre le variabili globali nel tuo script tramite '$ _POST'. –

+0

Ho già sentito qualcosa in proposito, e probabilmente non è il modo migliore per andare :) soprattutto se è importante qualcosa –

3

Io uso la mia funzione personalizzata ...

public function areNull() { 
    if (func_num_args() == 0) return false; 
    $arguments = func_get_args(); 
    foreach ($arguments as $argument): 
     if (is_null($argument)) return true; 
    endforeach; 
    return false; 
} 
$var = areNull("username", "password", "etc"); 

Sono sicuro che può essere facilmente modificato per lo scenario. Fondamentalmente restituisce true se qualcuno dei valori è NULL, quindi puoi cambiarlo in vuoto o altro.

1
if(isset($_POST['login']) && strlen($_POST['login'])) 
{ 
    // valid $_POST['login'] is set and its value is greater than zero 
} 
else 
{ 
    //error either $_POST['login'] is not set or $_POST['login'] is empty form field 
} 
+0

se hai un campo vuoto inviato, allora lo strlen() restituisce 0 che è falso in PHP. –

0

ho fatto in questo modo:

$missing = array(); 
foreach ($_POST as $key => $value) { if ($value == "") { array_push($missing, $key);}} 
if (count($missing) > 0) { 
    echo "Required fields found empty: "; 
    foreach ($missing as $k => $v) { echo $v." ";} 
    } else { 
    unset($missing); 
    // do your stuff here with the $_POST 
    } 
0

ho appena scritto una funzione veloce per fare questo. Ne avevo bisogno per gestire molti moduli così l'ho fatto in modo che accetti una stringa separata da ",".

//function to make sure that all of the required fields of a post are sent. Returns True for error and False for NO error 
//accepts a string that is then parsed by "," into an array. The array is then checked for empty values. 
function errorPOSTEmpty($stringOfFields) { 
     $error = false; 
      if(!empty($stringOfFields)) { 
       // Required field names 
       $required = explode(',',$stringOfFields); 
       // Loop over field names 
       foreach($required as $field) { 
        // Make sure each one exists and is not empty 
        if (empty($_POST[$field])) { 
        $error = true; 
        // No need to continue loop if 1 is found. 
        break; 
        } 
       } 
      } 
    return $error; 
} 

Quindi è possibile inserire questa funzione nel codice e gestire gli errori per ogni pagina.

$postError = errorPOSTEmpty('login,password,confirm,name,phone,email'); 

if ($postError === true) { 
    ...error code... 
} else { 
    ...vars set goto POSTing code... 
} 
0

Nota: fare attenzione se 0 è un valore accettabile per un campo richiesto. Come @ Harold1983- menzionato, questi sono trattati come vuoti in PHP. Per questo tipo di cose dovremmo usare isset anziché vuoto.

$requestArr = $_POST['data']// Requested data 
$requiredFields = ['emailType', 'emailSubtype']; 
$missigFields = $this->checkRequiredFields($requiredFields, $requestArr); 

if ($missigFields) { 
    $errorMsg = 'Following parmeters are mandatory: ' . $missigFields; 
    return $errorMsg; 
} 

// Function to check whether the required params is exists in the array or not. 
private function checkRequiredFields($requiredFields, $requestArr) { 
    $missigFields = []; 
    // Loop over the required fields and check whether the value is exist or not in the request params. 
    foreach ($requiredFields as $field) {`enter code here` 
     if (empty($requestArr[$field])) { 
      array_push($missigFields, $field); 
     } 
    } 
    $missigFields = implode(', ', $missigFields); 
    return $missigFields; 
} 
Problemi correlati