Jump to content

Registration - Check for empty textboxs


PF2G

Recommended Posts

Hi, PHP Freaks

 

I'm doing a registration system and as you can see when i verify the fields, if it's empty it says that the FIELD_1 misses. And what i want is if the field is empty says that and then goes back, but i don't wanto the info already written disappears.

 

registration

 

<?PHP

include 'topo.php';

?>


<!-- content -->

   <div class="indent">

        <h2> Inscrição </h2>


<form action="envia.php" method="post" onsubmit="return validar()" >
  <fieldset>
   <legend> ALUNO </legend>
       
          <br/>
          <div class="field"><label>Username*: </label><input type="text" size="30" name = "username"/></div>
      <br/>      
          <div class="field"><label>Password*: </label><input type="password" size="25" name = "pass"/></div>
      <br/>      
          <div class="field"><label>Confirma Password*: </label><input type="password" size="25" name = "conf_pass"/></div>
      <br/>      
          <div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome"/></div>
      <br/>      
      <div class="field"><label>Data de Nascimento*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "dia"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "mes"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "ano"/>  (DD/MM/AAAA)</div>
      <br/>
      <div class="field"><label>Morada: </label><input type="text" onkeypress="return onlyNumbers();" size="39" name = "morada"/></div>
      <br/>
      <div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2"/></div>
      <br/>
      <div class="field"><label>Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele"/></div>
      <br/>
          Curso*: <select name="curso">
          <option value = 1> Piano    </option>
          <option value = 2> Orgão    </option>
          <option value = 3> Guitarra Elétrica </option>
          <option value = 4> Guitarra Acústica </option>
          <option value = 5 Violino  </option>
          <option value = 6> Canto    </option>
          <option value = 7> Bateria  </option>
          <option value = 8> Saxofone </option>
          <option value = 9> Flauta   </option>
          <option value = 10> Baixo </option>
           <option value = 11> Violoncelo </option>
          </select>
          <br/>
          <br/>
      <div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email" size = "25"/></div>
          <br/>
          Observações:
          <br/>
      <textarea cols="30" rows="7" name="obs"></textarea>
  </fieldset>
  <br/>
  <fieldset>
    <legend> ENCARREGADO DE EDUCAÇÃO  </legend>
         
          <br/>
      <div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome_ee"/></div>
      <br/>
      <div class="field"><label>Morada: </label><input type="text" size="39" name = "morada_ee"/></div>
      <br/>
          <div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1_ee"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2_ee"/></div>
          <br/>
      <div class="field">Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele_ee"/></div>
      <br/>
      <div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email_ee" size = "25"/></div>
         
  </fieldset>
      <br/>
      * Campos Obrigatórios
          <br/>
          <input type="submit" value="Enviar">
      <input type="Reset" value="Apagar">

</form>
       
   </div>
</div>


<?PHP

include 'rodape.php';

?>

 

send

 

<?PHP

//ligar base de dados

include "db_connect.php";

/////////// ALUNO ///////////////
$username = $_POST ['username'];
$pass = $_POST ['pass'];
$conf_pass = $_POST ['conf_pass'];
$nome = $_POST ['nome'];
$dia = $_POST ['dia'];
$mes = $_POST ['mes'];
$ano = $_POST ['ano'];
$data_nascimento = $dia .'-'. $mes .'-'. $ano;
$morada = $_POST ['morada'];
$cp1 = $_POST ['cp1'];
$cp2 = $_POST ['cp2'];
$cp_aluno = $cp1 .'-'. $cp2;
$curso = $_POST ['curso'];
$telemovel = $_POST ['tele'];
$email = $_POST ['email'];
$observacao = $_POST ['obs'];

///////////ENC.EDU.///////////////
$nome_ee = $_POST ['nome_ee'];
$morada_ee = $_POST ['morada_ee'];
$cp1_ee = $_POST ['cp1_ee'];
$cp2_ee = $_POST ['cp2_ee'];
$cp_ee = $cp1_ee .'-'. $cp2_ee;
$tele_ee = $_POST ['tele_ee'];
$email_ee = $_POST ['email_ee'];

include 'veref_aluno.php';
include 'veref_ee.php';

//inserir na base de dados

$sql = "INSERT INTO alunos (username, password, nome_aluno, data_nascimento, morada_aluno, cp_aluno, telemovel_aluno, email_aluno, observacoes, nome_ee, morada_ee, cp_ee, telemovel_ee, email_ee)
                VALUES('". $username. "', '" . $pass ."','" . $nome ."', '". $data_nascimento ."', '". $morada ."', '". $cp_aluno ."', '". $telemovel ."', '". $email ."','" .  $observacao . "','". $nome_ee ."', '". $morada_ee ."', '". $cp_ee ."', '". $tele_ee ."', '". $email_ee ."')";

if(mysql_query($sql))
{
echo "Inscrição feita com sucesso. Em breve receberá um email para confirmar os dados. Obrigado!";
//header ('Location: index.php');
}

?>

 

veref_reg student

<?PHP

$back = $_SERVER['HTTP_REFERER'];

// verifica username
if (empty($username))
{
die("<script>alert(\"Preencha o Username.\");
location.href='". $back ."';
</script>");

}

// verifica password
if (empty($pass))
{
die("Preencha a 'Password'.");
}
   
//verifica conf_pass


// verifica nome
if (empty($nome))
{
die("Preencha o 'Nome'.");
}

//verifica telemovel
if (empty($telemovel))
{
die("Preencha o 'Telemovel'.");
}

// verifica email
if (empty($email))
{
die("Preencha 'Email'.");
}
?>

 

Do you understand?

 

If you do, can you help me?

 

Thank you,

PF2G

Link to comment
Share on other sites

And what i want is if the field is empty says that and then goes back, but i don't wanto the info already written disappears.

Not a problem :) The easiest way to achieve it is to do all in one script.

 

I'll not write a code, just a logic (all in one form, "action" of the form is an empty string). I hope that you can create a code :). Mainly you need to combine a code parts that you have already.

 

1. Processing.

First you assign variables. I'd recommend to do it in such a way:

$username = isset($_POST ['username']) ? $_POST['username'] : '';

 

You may be sure that $username always a string, empty or not. But it never be a NULL!!!

 

Then you check if form was submited. If submited, check information. If it's correct, redirect to another script

header( 'Location: ....');

 

If something is wrong you a going to your form AUTOMATICALLY, because it's in the same script.

 

2. In a form.

Create an input

<div class="field"><label>Username*: </label><input type="text" size="30" name = "username" value="<?php echo $username ?>"/></div>

 

It doesn't matter if you have an empty string or not. In any case value will be created. Of course you may add any additional information, something like "Correct your input".

Also it's not important if it's a first time when you create a form or you like to correct input. It works for both cases.

 

And, finally, you may do the same for selects, textareas, checkboxes...

Link to comment
Share on other sites

Let me see if i understand:

 

checking the username field:

 

<?PHP
//ligar base de dados

include "db_connect.php";

/////////// ALUNO ///////////////
$username = isset($_POST ['username']) ? $_POST['username'] : '';

 

registration form:

<!-- content -->

   <div class="indent">

<h2> Inscrição </h2>


<form action="envia.php" method="post" onsubmit="return validar()" >
  <fieldset>
   <legend> ALUNO </legend>

  <br/>
  <div class="field"><label>Username*: </label><input type="text" size="30" name = "username" value="<?php echo $username ?>"/></div>

Link to comment
Share on other sites

Yes, you are right.

And I repeat once more - it might be in one script.

 

It erases everything anyway. I want to go to the form but with all other fields already written with the info the user filled previously.

 

Do you understand?

Link to comment
Share on other sites

Well.... Try this code, add nothing more inside body!!! And tell me what you get

 

<body>
<?php
$username=isset( $_POST['username'] ) ? htmlspecialchars($_POST['username']) : '';

if( $username != '' )
  echo "You entered username[$username]<br>";
?>
<form action="" method="post">
<input type="text" name="username" value="<?php echo $username ?>" />
<input type="submit" name="ok" />

</form>

</body>

Link to comment
Share on other sites

PF2G - did you try start my code with a form?

 

Look i don't think you understand my question. I already have an alert box saying that

if (empty($username))
{
die("<script>alert(\"Preencha o Username.\");
location.href='inscricao.php';
</script>");

 

What i want is give me that error in the registration page not opening other and goes to the page with the fields all empty. With that way the user forgots, for example, the name and then has to write everything over again.

 

Do you understand?

Link to comment
Share on other sites

What i want is give me that error in the registration page not opening other and goes to the page with the fields all empty. With that way the user forgots, for example, the name and then has to write everything over again.

 

Do you understand?

You still didn't answer if you try to start my code. I'm asking about it not for fun :) If you start it you'll see that you may go back to the form and don't lose any information. Your user will enter only the part of information that he forget.

 

Just try my to start my code... It's easy and it's not a virus ;)

Link to comment
Share on other sites

You still didn't answer if you try to start my code. I'm asking about it not for fun :) If you start it you'll see that you may go back to the form and don't lose any information. Your user will enter only the part of information that he forget.

 

Just try my to start my code... It's easy and it's not a virus ;)

 

 

Sorry, it erases everything.

 

$username=isset( $_POST['username'] ) ? htmlspecialchars($_POST['username']) : '';
if( $username != '' )
  echo "You entered username[$username]<br>";
?>

<!-- content -->

<div class="indent">

<h2> Inscrição </h2>

<form action="" method="post">
  <fieldset>
   <legend> ALUNO </legend>

  <br/>
  <div class="field"><label>Username*: </label><input type="text" name="username" value="<?php echo $username ?>" /></div>

Link to comment
Share on other sites

"Sorry, it erases everything" - I can't understand what you are talking about.... What is erased? Your code is working. When you enter a text and press Enter, it shows text again inside input.

What is erased? What do you like to get instead of this?

 

OK. Here is a printscreen of your code in my computer :) I entered any text and press Enter. The result of this action you see. Do you get the same or anything else?

I tried to add a picture but I fail... It was written that upload directory is full. But my picture has a volume 3KB only. Something is wrong in a forum?

Link to comment
Share on other sites

"Sorry, it erases everything" - I can't understand what you are talking about.... What is erased? Your code is working. When you enter a text and press Enter, it shows text again inside input.

What is erased? What do you like to get instead of this?

 

OK. Here is a printscreen of your code in my computer :) I entered any text and press Enter. The result of this action you see. Do you get the same or anything else?

I tried to add a picture but I fail... It was written that upload directory is full. But my picture has a volume 3KB only. Something is wrong in a forum?

 

It doesn't do anything...i didn't write a thing in username and i submited and it erased everything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.