PHP HTML çıkışı ile sunucu kodu Karıştırma

3 Cevap php

Ben form verilerini almak ve onunla bir şeyler yapacağım küçük bir script kadar kodlama ediyorum. Başarısız olursa, tekrar hata ve form gösterir. Eğer başarılı olursa, bu bir başarı mesajı göstereceğim.

Backend kodu form verilerini bir hata varsa istisnalar atarım sınıflarını kullanıyor. Sınıflar için çağrılar bir try {} catch {} sarılmış olacaktır.

Ne formu iki kez göstermek için en iyi yol olacağını?

Gibi bir şey gerekir:

If form submitted: 

   Try:
      Add the account via class
   Catch:
      Echo the error
      Show the form 

   Echo succcess

Else:
   Show the form

3 Cevap

Basit formlar için, böyle yazdı:

<?php

   if (isset($_POST['submit']))
   {
   $errorFound=0;

   if (empty($_POST['form_variable']))
   { 
     $errorFound=1; 
     $error_str="empty string";
   }

   if (!$errorFound) // if no errors, process the form!
   { 
   //process whatever you do with it - redirect to another page
   }

  }
 ?> 
 <form action="name.php">
 <div> <input name='form_variable'> 
 <?php if ($errorFound) {echo $error_str} ?>
 </div> 
 <input type=submit name=submit>
 </form> 

Ne olur o teslim asla, eğer html form ve ekran düz gidecek ama gönderilen ve bir hata varsa, bu php giriş etiketinden sonra echo'd error_st ile tekrar formunu görüntülemek için neden olacaktır.

Bir değişken formu için HTML kodlamak ve her iki yerde de değişken echo.

$form = "<form>...</form";

if ($_POST) {
  if (addAccount($fname,$email)) {
    print "Success";
  } else {
    print "Errors";
    print $form;
  }
} else {
  print $form;
}

Sen şablon motoru gibi smarty ile çok kolay bir şekilde tüm bu şeyler yapabilirsiniz. Sorunuza olarak sadece tam olarak aynı

{$ Form eq sunulması durumunda)}

{if $success} Add the account via class {else} Echo the error Show the form

Echo succcess {/if} {else} Show the form {/if}

Sadece php bunu yapmak istiyorsanız ya da tek başına sadece "netrox" yöntemi yapmak anlamına gelir. Onun iyi.

ve bu php başka bir kavramdır çağrı heredoc olduğunu. çok kolay bir şekilde php Html'nizi içerebilir.