2012-12-08 11 views
5

lasciatemi innanzitutto dire che ho cercato di tagliare il codice qui sotto il più piccolo possibile:Come fermare menu a discesa la visualizzazione "Selezionare" quando viene visualizzato il messaggio di errore

 <?php 
     // required variables (make them explciit no need for foreach loop) 

     $getyear  = (isset($_POST['year'])) ? $_POST['year'] : ''; 
     $getpass  = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : ''; 
     $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : ''; 
     $errormsg  = (isset($errormsg)) ? $errormsg : ''; 

     $validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass']; 

     $min_year = 1; 
     $max_year = 10; 
     $years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012 
     $yearHTML = ''; 
     $yearHTML .= '<select name="year" id="yearDrop">' . PHP_EOL; 
     $yearHTML .= '<option value="">Please Select</option>' . PHP_EOL; 
     foreach ($years as $year) { 
      if (!$validSubmission && isset($_POST['year']) && $year == $_POST['year']) { 
       $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
      } else { 
       $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
      } 
     } 

     $yearHTML .= '</select>'; 


     if ((isset($_POST['registerbtn']))) { 
      if (in_array($_POST['year'], $years) === true) { 
       $getyear = (int) $_POST['year']; 
      } 
      $getpass  = $_POST['studentpass']; 
      $getretypepass = $_POST['retypepass']; 


      if ($getyear) { 
       if ($getpass) { 
        if (strlen($getpass) <= 5) { 
         $errormsg = "The Password must be a minimum of 6 characters or more"; 
        } else { 
         if ($getretypepass) { 
          if ($getpass === $getretypepass) { 
           //perform 2 queries, one query contains $aliasnumrows and other contains $numrows 

           if ($aliasnumrows == 0) { 
            if ($numrows == 0) { 
             //perform query which contains $numrows 


             if ($numrows == 1) { 
              $errormsg = "<span style='color: green'>Student has been Registered</span>"; 

              $getyear = ""; 


             } else { 
              $errormsg = "An error has occured, Student has not been Registered"; 

             } 

            } 

            else { 
             $errormsg = "There is already a Student with that Username"; 
            } 
           } 

           else { 
            $errormsg = "There is already a Student with that Alias"; 
           } 
          } 

          else { 
           $errormsg = "Your Passwords did not match"; 
          } 
         } 

         else { 
          $errormsg = "You must retype your Password to Register"; 
         } 
        } 
       } else { 
        $errormsg = "You must enter in a Password to Register"; 
       } 

      } 

     else{ 
    $errormsg = "You must enter in Student's current Academic Year to Register"; 
    } 

    } 

$form = " 
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'> 
    <table> 
    <tr> 
    <td></td> 
    <td id='errormsg'>$errormsg</td> 
</tr> 
    <tr> 
    <td>Year:</td> 
    <td>{$yearHTML}</td> 
    </tr> 
    <tr> 
    <td>Password:</td> 
    <td><input type='password' name='studentpass' value='' /></td> 
    </tr> 
    <tr> 
    <td>Retype Password:</td> 
    <td><input type='password' name='retypepass' value='' /></td> 
    </tr> 
    <tr> 
    <tr> 
    <td></td> 
    <td><input type='submit' value='Register' name='registerbtn' /></td> 
    </tr> 
    </table> 
    </form>"; 

    echo $form; 

Ok questo è il problema che sto avendo. Ho un menu a discesa per anni e due input di testo per digitare la password e digitare la password. Ora, se l'utente seleziona un anno dal menu a discesa e invia il modulo, visualizza il messaggio di errore che indica che è necessario inserire una password e che nel menu a discesa dell'anno viene comunque visualizzata l'opzione che l'utente ha selezionato dal menu a discesa dell'anno.

Il problema che sto avendo è che se appare uno di questi messaggi sotto, l'opzione a tendina torna alla visualizzazione di "Please Select". La mia domanda è: come posso interrompere il menu a discesa tornando all'opzione "Seleziona" quando vengono visualizzati i seguenti messaggi di errore?

$errormsg = "There is already a Student with that Username"; 
$errormsg = "There is already a Student with that Alias"; 
$errormsg = "Your Passwords did not match"; 
$errormsg = "You must retype your Password to Register"; 

La riga di codice Credo mutate esigenze è qui:

$validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass']; 
+0

Il codice contiene un sacco di logica che è estremamente difficile da seguire (per non parlare scomodo quando qualcuno ha più di un errore dal momento che mostrerà solo uno di loro e si dovrà presentare il modulo di nuovo). Quando convalido i moduli, aggiungo tutti i miei errori a un array, quindi li passo in ciclo per visualizzarli all'utente. – Mike

+0

@ Mike Sì, questo è un miglioramento che posso apportare. Ad essere onesto, sto facendo il back-end, quindi spetta a me determinare quello o fino alla persona che fa il front-end? – user1881090

risposta

1

Io penso quello che stai cercando è questo:

// This is just a suggestion, but I would set these to null instead of an empty string: 
    $getyear  = (isset($_POST['year'])) ? $_POST['year'] : null; 
    $getpass  = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : null; 
    $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : null; 
    $errormsg  = (isset($errormsg)) ? $errormsg : null; 
// You have already defined these, so no need to use $_POST here 
// Also, I like to compare it to an actual value rather than just doing if ($variable) 
// to avoid unforseen circumstances. Note, isset() returns false if $var == null. 
$validSubmission = (isset($_POST['registerbtn']) && isset($getyear) && isset($getpass) && isset($getretypepass)); 

allora penso che hai la tua logica indietro in seguito:

foreach ($years as $year) { 
     if ($validSubmission && $year == $getyear) { 
      $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
     } else { 
      $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
     } 
    } 
+0

Credo che stia usando $ validSubmission per convalidare che i valori non sono "falsi", non se sono impostati. –

+0

Vero, penso che forse la sua logica è al contrario e sta invertendo il suo assegno su '$ validSubmission 'più tardi. – Mike

+0

Devo sbarazzarmi di isset() nella parte superiore dello script che ho già e includere l'isset in '$ validSubmiision' come hai menzionato? – user1881090

0

Il problema è che stai scrivendo al server, e il server è nuovamente il rendering della pagina. Per fare questo correttamente e in un modo che funzioni su tutti i browser, è necessario ripetere i risultati $ _POST (dopo averli correttamente richiamati) negli elementi di input.

Analogamente, è necessario utilizzare il valore $ _POST per la casella di selezione per determinare quale tag option deve avere il set di attributi selected="selected".

1

Quando ripopolare la discesa Anno, probabilmente non ti interessa se qualche parte della presentazione è valida o meno, vuoi solo ripopolarla a prescindere. Semplicemente abbandonare la logica per quello.

foreach ($years as $year) { 
    if (isset($_POST['year']) && $year == $_POST['year']) { 
     $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL; 
    } else { 
     $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL; 
    } 
} 
+0

Sono d'accordo. Questa è probabilmente una risposta migliore della mia. – Mike

Problemi correlati