isset and empty

This is a discussion on "isset and empty" within the PHP Forum section. This forum, and the thread "isset and empty are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Jan 26th, 2008, 20:07
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
isset and empty

I'm doing some form validation and I have a series of radio buttons with profiles taken from the db to display but I also have one for "Other" with a textbox beside it for people to enter their own profile if it's not in the list.

So ... I want to make sure that if people checked the radio button (with name="autre"), that they have entered something in the textbox beside it (with name="autre_secteur").

So I put this

PHP: Select all

if ((isset($autre)) && (empty($autre_secteur))) {
        echo 
'You forgot to bla bla bla';
        
regForm(); // displays the form
    


However, if they didn't check ANY of the radio buttons (with name="pID"), I want to display a different message ... I tried

PHP: Select all

if ( (!$pID) || (!$autre) ) {
echo 
'You forgot to to choose a profile';
        
regForm(); // displays the form
    

But it's not working ... it's always showing the first error message.

Any idea what I'm doing wrong? If you guys need more code, let me know.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 27th, 2008, 00:42
JustinStudios's Avatar
Reputable Member
Join Date: Mar 2007
Location: USA
Posts: 404
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

I'd like to see more code if I could before I claim that I know what is going on. What it seems like is going on is its saying "If you checked add more and didn't put anything in the field then excerpt Blah Blah Blah". I'm sure you did set the variable = to the post but just need to see that if possible.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 27th, 2008, 02:00
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

Alright ... here's the whole thing...

PHP: Select all

<?php session_start(); ?>

<?php include('../inc/header.html'); ?>

<?php include('../inc/sidebar.html'); ?>

<div id="main">

<h1>Formulaire d'adh&eacute;sion</h1>


<?
if ($_POST['submit']) {
    
$spam addslashes($_POST['spam']);
    
$prenom addslashes($_POST['prenom']);
    
$nom addslashes($_POST['nom']);
    
$entreprise addslashes($_POST['entreprise']);
    
$adresse addslashes($_POST['adresse']);
    
$ville addslashes($_POST['ville']);
    
$codepostal addslashes($_POST['codepostal']);
    
$telephone addslashes($_POST['telephone']);
    
$telecopieur addslashes($_POST['telecopieur']);
    
$cellulaire addslashes($_POST['cellulaire']);
    
$siteweb addslashes($_POST['siteweb']);
    
$courriel addslashes($_POST['courriel']);
    
$ddn addslashes($_POST['ddn']);
    
$pID addslashes($_POST['pID']);
    
$autre_secteur addslashes($_POST['autre_secteur']);
    
$employe_plein addslashes($_POST['employe_plein']);
    
$employe_partiel addslashes($_POST['employe_partiel']);
    
$employe_saisonnier addslashes($_POST['employe_saisonnier']);
    
$superficie_globale addslashes($_POST['superficie_globale']);
    
$superficie_vente addslashes($_POST['superficie_vente']);
    
$description addslashes($_POST['description']);
    
$username addslashes($_POST['username']);
    
$password addslashes($_POST['password']);
    
    
$autre addslashes($_POST['autre']);
    
$autre_secteur addslashes($_POST['autre_secteur']);
    
    
$date_adhesion date("Y-m-d");
    
$acces '2';
    
    
    
$_SESSION['prenom'] = $prenom;
    
$_SESSION['nom'] = $nom;
    
$_SESSION['entreprise'] = $entreprise;
    
$_SESSION['adresse'] = $adresse;
    
$_SESSION['ville'] = $ville;
    
$_SESSION['codepostal'] = $codepostal;
    
$_SESSION['telephone'] = $telephone;
    
$_SESSION['telecopieur'] = $telecopieur;
    
$_SESSION['cellulaire'] = $cellulaire;
    
$_SESSION['siteweb'] = $siteweb;
    
$_SESSION['courriel']= $courriel;
    
$_SESSION['ddn'] = $ddn;
    
$_SESSION['autre_secteur'] = $autre_secteur;
    
$_SESSION['employe_plein'] = $employe_plein;
    
$_SESSION['employe_partiel'] = $employe_partiel;
    
$_SESSION['employe_saisonnier'] = $employe_saisonnier;
    
$_SESSION['superficie_globale'] = $superficie_globale;
    
$_SESSION['superficie_vente'] = $superficie_vente;
    
$_SESSION['description'] = $description;
    
$_SESSION['username'] = $username;
    
$_SESSION['password'] = $password;
    
    
    
    if ((isset(
$autre)) && (empty($autre_secteur))) {
        echo 
'<div id="errors">';
        echo 
"<p>Votre secteur ne figure pas dans notre liste, mais vous avez oubli&eacute; de l'inscrire.</p>";
        echo 
'</div>';
        
regForm();
    } elseif ( (
$spam != 'chanson') || (!$prenom) || (!$nom) || (!$entreprise) || (!$adresse) || (!$ville) || (!$codepostal) || (!$telephone) || (!ddn) || (!$pID) || (!$autre) || (!$_POST['iID']) || (!$username) || (!$password) ){
        echo 
'<div id="errors">';
        if (
$spam != 'chanson') { echo "<p>S'il-vous-pla&icirc;t, bien &eacute;crire le mot de pr&eacute;vention contre le spam!</p>"; }
        if (!
$prenom) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>pr&eacute;nom</strong></p>"; }
        if (!
$nom) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>nom</strong></p>"; }
        if (!
$entreprise) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>nom d'entreprise</strong></p>"; }
        if (!
$adresse) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>adresse</strong></p>"; }
        if (!
$ville) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>nom de ville</strong></p>"; }
        if (!
$codepostal) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>code postal</strong></p>"; }
        if (!
$telephone) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>num&eacute;de t&eacute;l&eacute;phone</strong></p>"; }
        if (!
$ddn) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>date de naissance</strong></p>"; }
        if (!
$pID && !$autre) { echo "<p>Vous avez oubli&eacute; de <strong>choisir un profil d'entreprise</strong></p>"; }
        if (!
$_POST['iID']) { echo "<p>Vous avez oubli&eacute; de <strong>choisir un int&eacute;r&ecirc;t</strong></p>"; }
        if (!
$username) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>nom d'usager</strong></p>"; }
        if (!
$password) { echo "<p>Vous avez oubli&eacute; d'inscrire votre <strong>mot de passe</strong></p>"; }
        echo 
'</div>';
        
regForm();
    } else {
        echo 
'<div id="success">';
        echo 
"<p>Merci d'avoir rempli le formulaire.</p>";
        echo 
"<p>Afin que vous puissiez garder vos informations &agrave; jour, l'adresse de la section administrative est http://www.apica.ca/dashboard/ .</p>";
        echo 
"<p>Une personne d&eacute;sign&eacute; de l'APICA sera en contact avec vous.</p>";
        echo 
'</div>';
        
        if (isset(
$_POST['iID'])) {
            
// Now use the PHP implode function which is exatly like explode except you use the second argument as the glue for the string and it joins the array together
            
$iID implode($_POST['iID'], '-'); // Join on the hyphen
            //echo $variableString;
        

        
        
        
// save information
        //$insertMembre = mysql_query("insert into membres (prenom, nom, entreprise, adresse, ville, codepostal, telephone, telecopieur, cellulaire, siteweb, courriel, ddn, pID, employe_plein, employe_partiel, employe_saisonnier, superficie_globale, superficie_vente, description, iID, date_dadhesion) values ('$prenom', '$nom', '$entreprise', '$adresse', '$ville', '$codepostal', '$telephone', '$telecopieur', '$cellulaire', '$siteweb', '$courriel', '$ddn', '$pID', '$employe_plein', '$employe_partiel', '$employe_saisonnier', '$superficie_globale', '$superficie_vente', '$description', '$iID', '$date_dadhesion')");
        
        //$getMemberInfo = mysql_query("select mID from membres where nom='$nom', prenom='$prenom', entreprise='$entreprise'");
        //$row = mysql_fetch_array($getMemberInfo);
        //$mID = $row['mID'];
        
        //insertLogin = mysql_query("insert into login (mID, username, password, aID) values ('$mID', '$username', '$password', '$aID')");
        
        // send email to new member with url of dashboard
        
        // send email to APICA to let them know of the new member
    
}
        

// missing interets
    
    //$insert = mysql_query("insert into profil (profil, cID) values ('$profil', '$cID')");
} else {
    
regForm();
}

function 
regForm() {
?>

<form method="post" action="" id="formulaire">
<fieldset>
     <legend>Information sur l'entreprise</legend>
    <p><label for="prenom"><strong>Pr&eacute;nom</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="prenom" value="<?=$_SESSION['prenom']?>" id="prenom" size="75" /></p>
    <p><label for="nom"><strong>Nom</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="nom" value="<?=$_SESSION['nom']?>" id="nom" size="75" /></p>
    <p><label for="entreprise"><strong>Entreprise</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="entreprise" value="<?=$_SESSION['entreprise']?>" id="entreprise" size="75" /></p>
    <p><label for="adresse"><strong>Adresse</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="adresse" value="<?=$_SESSION['adresse']?>" id="adresse" size="75" /></p>
    <p><label for="ville"><strong>Ville</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="ville" value="<?=$_SESSION['ville']?>" id="ville" size="75" /></p>
    <p><label for="codepostal"><strong>Code Postal</strong></label> : <strong>X0X 0X0</strong> <span class="required">(requis)</span><br /><input type="text" name="codepostal" value="<?=$_SESSION['codepostal']?>" id="codepostal" size="75" /></p>
    <p><label for="telephone"><strong>T&eacute;l&eacute;phone</strong></label> : <strong>123-456-7890</strong> <span class="required">(requis)</span><br /><input type="text" name="telephone" value="<?=$_SESSION['telephone']?>" id="telephone" size="75" /></p>
    <p><label for="telecopieur">T&eacute;l&eacute;copieur</label> : <strong>123-456-7890</strong> <br /><input type="text" name="telecopieur" value="<?=$_SESSION['telecopieur']?>" id="telecopieur" size="75" /></p>
    <p><label for="cellulaire">Cellulaire</label> : <strong>123-456-7890</strong> <br /><input type="text" name="cellulaire" value="<?=$_SESSION['cellulaire']?>" id="cellulaire" size="75" /></p>
    <p><label for="siteweb">Site Web</label> :<br /><input type="text" name="siteweb" value="<?=$_SESSION['siteweb']?>" id="siteweb" size="75" /></p>
    <p><label for="courriel">Courriel</label> :<br /><input type="text" name="courriel" value="<?=$_SESSION['courriel']?>" id="courriel" size="75" /></p>
    <p><label for="ddn"><strong>Date de naissance (aaaa-mm-jj)</strong></label> : <span class="required">(requis)</span><br /><input type="text" name="ddn" value="<?=$_SESSION['ddn']?>" id="ddn" size="75" /></p>
</fieldset>

<fieldset>
    <legend>Profil d’entreprise</legend>
    <p><label for="secteur"><strong>Secteur d’activité : (cocher une case seulement)</strong></label> : <span class="required">(requis)</span></p>
    <?
    $selectcat 
mysql_query("select * from categories order by categorie desc"); 
    while (
$rowcat mysql_fetch_array($selectcat)) {
        
$categorie stripslashes($rowcat['categorie']);
        
$cID $rowcat['cID'];
        
        
$selectprofil mysql_query("select * from profil where cID='$cID' order by profil asc");
    
?>
    <h2><?=$categorie?></h2>
       <?
        
while ($rowprofil mysql_fetch_array($selectprofil)) {
            
$profil stripslashes($rowprofil['profil']);
            
$pID $rowprofil['pID'];
            
?>
            <input type="radio" name="pID" id="secteur" value="<?=$pID?>" /> <?=$profil?><br />
    <?     ?>
    <p><input type="radio" name="autre" value="<?=$cID?>" id="secteur" value="0" /> Autre : <input type="text" name="autre_secteur" id="autre_secteur" size="45" /></p>
    <? ?>
    
    <h2>Nombre d’employé</h2>
    <p><label for="plein">Temps plein</label> : <br /><input type="text" name="employe_plein" value="<?=$_SESSION['employe_plein']?>" id="plein" size="75" /></p>
    <p><label for="partiel">Temps partiel</label> : <br /><input type="text" name="employe_partiel" value="<?=$_SESSION['employe_partiel']?>" id="partiel" size="75" /></p>
    <p><label for="saisonnier">Saisonnier</label> : <br /><input type="text" name="employe_saisonnier" value="<?=$_SESSION['employe_saisonnier']?>" id="saisonnier" size="75" /></p>
    
    <p><label for="globale">Superficie globale de votre entreprise</label> : <br /><input type="text" name="superficie_globale" value="<?=$_SESSION['superficie_globale']?>" id="globale" size="75" /></p>
    <p><label for="vente">Superficie de vente</label> : <br /><input type="text" name="superficie_vente" value="<?=$_SESSION['superficie_vente']?>" id="vente" size="75" /></p>
</fieldset>

<fieldset>
    <legend>Description</legend>
    <p><label for="description"><strong>Brève description de vos produits et services</strong></label> : <span class="required">(requis)</span><br />
    <textarea name="description" id="description" cols="50" rows="5"><?=$_SESSION['description']?></textarea></p>
</fieldset>

<fieldset>
    <legend>Int&eacute;r&ecirc;t</legend>
    <p><label for="interets"><strong>Intérêt des membres</strong></label> : <span class="required">(requis)</span></p>
    <p>Afin de mieux connaître les intérêts de nos membres, nous vous demandons d’identifier les activités qui sont susceptibles de vous intéresser à court terme. Cette information sera utilisée à des fins statistiques, promotionnelles et de développement d’activités.</p>
    <?
    $selectint 
mysql_query("select * from interets order by interet asc"); 
    while (
$rowint mysql_fetch_array($selectint)) {
        
$interet stripslashes($rowint['interet']);
        
$iID $rowint['iID'];
    
?>
        <input type="checkbox" name="iID[]" id="interets" value="<?=$iID?>" /> <?=$interet?><br />
    <?
    
?>
</fieldset>

<fieldset>
    <legend>Tarifs et modalités de paiement</legend>
<p>Frais d’adhésion : 150 $ par année ou selon les mois restants à l’année en cours. Notre facturation est toujours faite en fonction de janvier à décembre.</p>
<p>Si vous adhérer en :</p>
<table>
<tr>
    <td width="33%">Janvier</td>
    <td width="33%">12 mois</td>
    <td width="33%">$150</td>
</tr>
<tr>
    <td>Février</td>
    <td>11 mois</td>
    <td>$137.50</td>
</tr>
<tr>
    <td>Mars</td>
    <td>10 mois</td>
    <td>$125</td>
</tr>
<tr>
    <td>Avril</td>
    <td>9 mois</td>
    <td>$112.50</td>
</tr>
<tr>
    <td>Mai</td>
    <td>8 mois</td>
    <td>$100</td>
</tr>
<tr>
    <td>Juin</td>
    <td>7 mois</td>
    <td>$87.50</td>
</tr>
<tr>
    <td>Juillet</td>
    <td>6 mois</td>
    <td>$75</td>
</tr>
<tr>
    <td>Août</td>
    <td>5 mois</td>
    <td>$62.50</td>
</tr>
<tr>
    <td>Septembre</td>
    <td>4 mois</td>
    <td>$50 + 150$ pour 2008</td>
</tr>
<tr>
    <td>Octobre</td>
    <td>3 mois</td>
    <td>$37.50 + 150$ pour 2008</td>
</tr>
<tr>
    <td>Novembre</td>
    <td>2 mois</td>
    <td>$25 + 150$ pour 2008</td>
</tr>
<tr>
    <td>Décembre</td>
    <td>1 mois</td>
    <td>$12.50 + 150$ pour 2008</td>
</tr>
</table>
</fieldset>

<fieldset>
    <legend>Acc&egrave;s &agrave; votre compte</legend>
    <p>Afin de pouvoir garder l'information de votre compagnie &agrave; jour, veuillez vous cr&eacute;er un compte qui vous donnera acc&eacute;s &agrave; une section administrative du site de l'APICA o&ugrave; vous pourrez effectuer vous-m&ecirc;me les changements n&eacute;cessaire</p>
    
    <p><label for="username"><strong>Nom d'usager</strong></label> : <br /><input type="text" name="username" value="<?=$_SESSION['username']?>" id="username" size="75" /></p>
    <p><label for="password"><strong>Mot de passe</strong></label> : <br /><input type="password" name="password" value="<?=$_SESSION['password']?>" id="password" size="75" /></p>
</fieldset>

<p>Pour la pr&eacute;vention du spam, &eacute;crivez le mot "<strong>chanson</strong>" dans la bo&icirc;te ci-dessous <br /><input type="text" name="spam" id="spam" size="25" /></p>

<p><input type="submit" name="submit" value=" SOUMETTRE L'INSCRIPTION " class="submit" /> <input type="reset" value=" EFFACER " /></p>

</form>

<? ?>

</div>
<div class="clear"></div>
</div>

<?php include('../inc/footer.html'); ?>
I know it's messy ... I'm just rushing to get this thing done!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 27th, 2008, 03:31
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

The reason it is always failing in the first one is the way you are building your form together.

You are assiging every variable regardless of whether it came with the POST request. This means that $autre will always be set because you are creating the variable even if it wasn't checked.

If you are going to assign the variables even if they weren't sent to PHP, you would need to use some ternary operators to set the variable as false if it wasn't sent.

PHP: Select all

$autre = isset($_POST['autre']) ? addslashes($_POST['autre']) : false
If you do the above for all checkboxes/radios it will mean anything that wasn't checked will end up as false.

Then when checking if they were set you can use

PHP: Select all

if ($autre === false && $piID === false)

   echo 
'Oh no! you didn\'t select anything'
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 27th, 2008, 04:09
JustinStudios's Avatar
Reputable Member
Join Date: Mar 2007
Location: USA
Posts: 404
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

Grrr... Rakuli, this was mine to work on... :P

Rakuli has shown the short hand if statment. Personally I steer clear of it as I can never remember how to use it, but the idea behind it is wise. It puts all of your code into your variable then you can just see if it's false... Great thinking Rakuli! I don't speak French (I think that its French) but I don't really see a need for all of those session variables. Maybe you have a reason for doing it, but that is a TON of session variables.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 27th, 2008, 14:28
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

Quote:
Originally Posted by Rakuli View Post
You are assiging every variable regardless of whether it came with the POST request. This means that $autre will always be set because you are creating the variable even if it wasn't checked.
That's one problem when learning to do things on your own. You tend to copy code never knowing if it's the right way to do it or not

So ... How should I be doing it then? That's how I've always done it

Quote:
Originally Posted by JustinStudios View Post
I don't speak French (I think that its French) but I don't really see a need for all of those session variables. Maybe you have a reason for doing it, but that is a TON of session variables.
The reason for the session variables is that I want to retain their answers when they get an error... I don't want them to have to fill that big form more than once.

If that's not the way it should be done, how else should I be doing it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jan 27th, 2008, 14:50
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: isset and empty

Quote:
Originally Posted by karinne View Post
The reason for the session variables is that I want to retain their answers when they get an error... I don't want them to have to fill that big form more than once.

If that's not the way it should be done, how else should I be doing it?
Use the $_POST array.....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jan 27th, 2008, 15:09
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,759
Thanks: 0
Thanked 13 Times in 13 Posts
Re: isset and empty

Yup, what I do is if they have an error, print the $_POST for that field e.g
HTML: Select all
  <input type="text" name="title" id="title" value="<?php print $_POST['title']; ?>" />
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jan 27th, 2008, 15:20
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts