Nerede bu validateForm yöntemini çalıştırmak istiyorsunuz?

4 Cevap php

İşte ne var:

<html>
<head>
    <?php
    $validForm = false;

    function getValue($field){
        if(isset($_GET[$field])){
            return $_GET[$field];
        }
        else{
            return "";
        }
    }

    function validateForm(){
     //magic goes here.   
    }        
    ?>
</head>

<body>
    <?php if($validForm == false){ ?>
    <form action="class2.php" method="get">
        <dl>
            <dt>First Name:</dt>
            <dd><input type="text" value="<?php echo htmlspecialchars(getValue('name')) ?>" name="name" />                    
            </dd>                

            <dt>Last Name:</dt>
            <dd><input type="text" value="<?php echo htmlspecialchars(getValue('lastname')) ?>" name="lastname" />                    
            </dd>

            <br />                
            <dt>
                <input type="submit" value="enviar" />
            </dt>                
        </dl>
    </form>
    <?php
    } else {
    ?>

    <h1>Congratulations, you succesfully filled out the form!</h1>

    <?php }
    ?>
</body>

Where would I put the validateForm() call? I'm not sure. What I want is to continue showing the form until the $validForm variable is true. :)

Teşekkür ederim SO her zaman bana yardım.

4 Cevap

function validateForm(){
 if ( ) // Your form conditions go here
 {
    return true;
 } else {
    return false;
 }
} 
if ( $_GET ) // Only validate the form if it was submitted
{
   $validForm = validateForm();
} else {
   $validForm = false;
}

Bazı durumlar için örnekler:

if ( !empty ( $_GET[ 'name' ] ) and !empty ( $_GET[ 'lastname' ] ) ) // Check if the user entered something in both fields

I can only recommend you to change your getValue fonksiyonu çok.

Bu değiştirmek için iyi bir fikir olurdu

return $_GET[$field];

gibi bir şey

return htmlspecialchars ( trim ( $_GET[$field] ) );

Bu gereksiz boşluk kaldırmak ve bir kerede çıktı kaçacaktır.

Almanız gereken bir sonraki ya da muhtemelen ilk adım, çoğu durumda, {[aracılığıyla form verilerini göndermek için kötü bir fikirdir (olduğu gibi POST için GET formunuzu değiştirmek olacaktır 0)]}.

Size göndermek düğmeye bir isim eklemek zorunda:

  <input type="submit" value="enviar" name="validate" />

Ve validateForm ilanından sonra

Eğer koymak

if(getValue("validate") != "")
validateForm();
  1. Eğer (sizin veri URL yoluyla gönderilen $ _GET-değişkenleri kontrol) form verileri doğrulamak için çalışıyoruz olarak ben getValue fonksiyonda $ _POST-değişkenler için kontrol ediyorum, siz de "trim" eklemek isteyebilirsiniz
  2. teslim-düğmeye bir id vermek ve formu beed bu şekilde teslim olup olmadığını kontrol

örneğin <input type="submit" value="enviar" id="submitButton" />

("$ false validForm =;" yerine) $validForm = validateForm();: $ validForm ayar bu olmazdı

Lütfen validateForm-fonksiyonu daha sonra kontrol edilmelidir "$ _POST [" submitButton "]" ayarlanır ve veri geçerli (seti, sağ tip, vs vs) ise bütün bunlar tamam olup olmadığını, aksi halde, "gerçek" return olsun "false"

<html>
  <head>
  <?php
    $validForm = validateForm();

C.