Page 1 of 1

required and hide

Posted: Tue Oct 28, 2003 1:35 am
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)

Posted: Tue Oct 28, 2003 3:49 am
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.

Posted: Tue Oct 28, 2003 3:54 am
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

Posted: Tue Oct 28, 2003 5:23 am
by d3ad1ysp0rk
Is there a problem with using

Code: Select all

if($var!="")
{
  echo "thank you!";
}
else
{
  echo "please fill everything out..";
}
??

Posted: Tue Oct 28, 2003 5:49 am
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

Posted: Tue Oct 28, 2003 8:10 am
by ex247
thanks everyone... but when i put that code, it displays the error even before i submit/post

why is this?

Posted: Tue Oct 28, 2003 8:40 am
by twigletmac
What is the error?

Mac

Posted: Tue Oct 28, 2003 9:13 am
by ex247
no error, it just prints the 'Input with name "foo" is empty'

but i didnt even post yet

Posted: Tue Oct 28, 2003 9:16 am
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">

Posted: Tue Oct 28, 2003 9:46 am
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

Posted: Tue Oct 28, 2003 10:38 am
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?