Jump to content

Check if a form field has been completed


anarchoi

Recommended Posts

Hi,

 

I made a little script that basically just adds 3 different informations to the database. Only one is typed by the user in a textbox, others are select fields...

 

The problem is that i just noticed users can still submit the form and add an empty entry to the database...

 

how can i prevent this? I want to show an error if an user forgot to fill that textbox (the name of it is "TITLE" btw)

 

 

here is my code on add.php

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Ajouter un groupe</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<?

if($_POST['submit']) //If submit is hit

{

  //then connect as user

  //change user and password to your mySQL name and password

  mysql_connect("localhost","anarchoi1","****");

 

  //select which database you want to edit

  mysql_select_db("anarchoi1_phpb1");

 

  //convert all the posts to variables:

  $title = $_POST['title'];

  $infos = $_POST['infos'];

  $localisation = $_POST['localisation'];

  $date = $_POST['date'];

  $time = $_POST['time'];

  $actif = $_POST['actif'];

  $strSQL = "SELECT * FROM  news2 WHERE title='".$title."'";     

    $rs=mysql_query($strSQL); //execute the query

  if(mysql_num_rows($rs)==1)

  {

  $as = rand(0,9);

  echo "<b>Ce groupe est déja répertorié.</b>";

  }

  else

  {

  $result=MYSQL_QUERY("INSERT INTO news2 (id,title,infos,localisation,date,time,actif)".

      "VALUES ('NULL', '$title', '$infos', '$localisation', '$date', '$time', '$actif')");

 

    //confirm

  echo "<b>Groupe Ajouté.</b><br>Note: le groupe s'affichera dans la liste seulement après validation par un modérateur (-24h). Par contre, vous pouvez publier dès maintenant des concerts pour ce groupe.<br><b><a href=\"modifier2.php?groupe=$title\">Cliquez ici pour modifier les informations additionnelles (biographie, site web, activité, etc..)</a></b>";

  }

 

 

  //Insert the values into the correct database with the right fields

  //mysql table = news2

  //table columns = id, title, infos, localisation, date, time

  //post variables = $title, $infos, '$localisation, $date, $time

 

}

?>

<b>Ajouter un groupe au répertoire:</b>

<form method="post" action="add.php">

<TABLE>

<TR>

  <TD>Nom du Groupe:</TD>

  <TD><INPUT TYPE='TEXT' NAME='title' VALUE='' size=60></TD>

</TR>

<TR>

  <TD>Localisation:</TD>

  <TD>

      <SELECT NAME='localisation'>

        <OPTION selected VALUE=''>Région Inconnue

        <OPTION VALUE='MTL'>Montréal

        <OPTION VALUE='QC'>Québec

        <OPTION VALUE='T-R'>Trois-Rivières

        <OPTION VALUE='Laval'>Laval

        <OPTION VALUE='Saguenay'>Saguenay

        <OPTION VALUE='Bas-St-Laurent'>Bas-Saint-Laurent

        <OPTION VALUE='Estrie'>Estrie

        <OPTION VALUE='Outaouais'>Outaouais

        <OPTION VALUE='Abitibi'>Abitibi-Témiscamingue

        <OPTION VALUE='Côte-Nord'>Côte-Nord

        <OPTION VALUE='Gaspésie'>Gaspésie

        <OPTION VALUE='Lanaudière'>Lanaudière

        <OPTION VALUE='Laurentides'>Laurentides

 

 

      </SELECT>

  </TD>

</TR>

<TR>

  <TD>Groupe actif?</TD>

  <TD>

      <SELECT NAME='actif'>

        <OPTION selected VALUE=''>Statut inconnu

        <OPTION VALUE='1'>Ce groupe est présentement ACTIF

        <OPTION VALUE='0'>Ce groupe est INACTIF - les membres sont séparés

      </SELECT>

  </TD>

</TR>

 

<!-- <TR>

  <TD>Infos/Biographie:</TD>

  <TD><INPUT TYPE='TEXT' NAME='infos' VALUE='' size=60></TD>

</TR> -->

<TR>

  <TD></TD>

  <TD>

      <!-- You can use PHP functions to automatically get the value of date -->

      <INPUT TYPE='hidden' NAME='date' VALUE='<? echo date("j.m.y"); ?>' size=60>

  </TD>

</TR>

<TR>

  <TD></TD>

  <TD>

      <!-- You can use PHP functions to automatically get the value of time -->

      <INPUT TYPE='hidden' NAME='time' VALUE='<? echo date("g:i a"); ?>' size=60>

  </TD>

</TR>

 

 

<TR>

  <TD></TD><br>

  <TD><INPUT TYPE="submit" name="submit" value="Ajouter"></TD>

</TR>

</TABLE>

</form>

 

 

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/98589-check-if-a-form-field-has-been-completed/
Share on other sites

redarrow, was showing you an example of variables , yours would be the variables above:

 

<?php
   $title = $_POST['title'];
   $infos = $_POST['infos'];
   $localisation = $_POST['localisation'];
   $date = $_POST['date'];
   $time = $_POST['time'];
   $actif = $_POST['actif'];
?>

 

Graham

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.