required and hide

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

required and hide

Post by ex247 »

well... i have 2 questions

how do you make fields required to be filled out, like how to veridy there is actually something in the text box...

and, how do you hide a bit of info from someone that is not admin, i am pretty shure its if statement, but i want to hide some html, how would i go about doing that?


(sorry, i tried searching but found nothing of use for these 2 examples)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Might give you more ideas.

Code: Select all

<?php
    if (!empty($_POST['foo'])) {
        echo 'Input with name "foo" had text.<br />';
        if ($_POST['foo'] == 'bar') {
            echo 'Input with name "foo" is also "bar".<br />';
        }
    } else {
        echo 'Input with name "foo" is empty.<br />';
    }
?>
<form method="post">
 <input type="text" name="foo" />
 <input type="submit" />
</form>

Edited: Removed isset(), thanks Mac. Old habit.
Last edited by JAM on Tue Oct 28, 2003 4:04 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can condense

Code: Select all

if (isset($_POST['foo']) and !empty($_POST['foo'])) {
down to

Code: Select all

if (!empty($_POST['foo'])) {
as empty() checks whether a variable is set or not on top of checking to see if it has an empty value.

Mac
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Is there a problem with using

Code: Select all

if($var!="")
{
  echo "thank you!";
}
else
{
  echo "please fill everything out..";
}
??
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Yes, because if $var has not been set then you will get an undefined variable warning as you are trying to test whether something that does not exist is equal to an empty string.

If you are testing to see whether a particular variable has been set, then you really should use something like empty() or isset(). If you know that $var has definitely been set, for example if you did:

Code: Select all

$var = (!empty($_POST['var'])) ? $_POST['var'] : '';
beforehand, then you could do:

Code: Select all

if($var != '') {
  echo 'thank you!';
} else {
  echo 'please fill everything out..';
}
Mac
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

thanks everyone... but when i put that code, it displays the error even before i submit/post

why is this?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What is the error?

Mac
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

no error, it just prints the 'Input with name "foo" is empty'

but i didnt even post yet
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

You must have a form... and make sure when you click submit it will foward to yourscript.php so then the script will be activated.

Code: Select all

<form method="POST" action="yourscript.php">
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

ex247 wrote:no error, it just prints the 'Input with name "foo" is empty'

but i didnt even post yet
That's because the code JAM posted is an example to show you how isset() and empty() work. It will say that foo is empty because it doesn't exist yet nor does it contain a value so the code is working as expected.

You just need to adjust the implementation of the code so that it only runs when the form has been submitted. I tend to do this with a hidden form, so to elaborate on JAM's example:

Code: Select all

<?php
if (!empty($_POST['action']) && $_POST['action'] == 'post') {
    if (!empty($_POST['foo'])) {
        echo 'Input with name "foo" had text.<br />';
        if ($_POST['foo'] == 'bar') {
            echo 'Input with name "foo" is also "bar".<br />';
        }
    } else {
        echo 'Input with name "foo" is empty.<br />';
    }
} else {
    echo 'Form has not been submitted yet.';
}
?>
<form method="post">
<input type="text" name="foo" />
<input type="hidden" name="action" value="post" />
<input type="submit" />
</form>
Mac
ex247
Forum Commoner
Posts: 34
Joined: Tue Oct 28, 2003 1:35 am
Location: Boston, MA, USA
Contact:

Post by ex247 »

Code: Select all

<?php 
    if (!empty($_POST['frm_affected'])) { 
        echo 'Input with name "frm_affected" had text.<br />'; 
    } else { 
        echo 'Input with name "frm_affected" is empty.<br />'; 
    } 
	 if (!empty($_POST['frm_catagory'])) { 
        echo 'Input with name "frm_catagory" had text.<br />'; 
		 } else { 
        echo 'Input with name "frm_catagory" is empty.<br />'; 
    } 
	if (!empty($_POST['frm_descrition'])) { 
        echo 'Input with name "frm_description" had text.<br />'; 
		 } else { 
        echo 'Input with name "frm_description" is empty.<br />'; 
    } 
?> 

<FORM METHOD="post" ACTION="add.php?add=true"><TABLE BORDER="0">
<TR>
      <TD width="220" CLASS="td_label">Issue:</TD>
      <TD width="337"><INPUT SIZE="48" TYPE="text" NAME="frm_affected"></TD></TR>
<TR> 
      <td witch="196" class="td_label">Select issue Catagory:</td>
	 <td witch="360"><select name=frm_catagory>
	 <option value="Password">Password</option>
	 <option value="Frontpage">Frontpage</option>
	 <option value="Email">Email</option>
	 <option value="Web Control">Web Control</option>
	 <option value="FTP">FTP</option>
	 <option value="MySQL">MySQL</option>
	 <option value="Cgi We Provided">Cgi We Provided</option>
	 <option value="Domain Registration">Domain Registration</option>
	 <option value="PHP">PHP</option>
	 <option value="Sitebuilder">Sitebuilder</option>
	 <option value="Server Appears Down">Server Appears Down</option>
	 <option value="Other">Other</option></select></td></TR>
<TR>
TR>
      <TD CLASS="td_label">Description:</TD>
      <TD><TEXTAREA NAME="frm_description" COLS="35" ROWS="8"></TEXTAREA></TD></TR>
<INPUT TYPE="submit" VALUE="Submit"></P></TD></TR>
</TABLE></FORM>
that what i have in there for the php/html is that correct?
Post Reply