required and hide
Moderator: General Moderators
required and hide
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)
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)
Might give you more ideas.
Edited: Removed isset(), thanks Mac. Old habit.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You can condense
down to
as empty() checks whether a variable is set or not on top of checking to see if it has an empty value.
Mac
Code: Select all
if (isset($_POST['foo']) and !empty($_POST['foo'])) {Code: Select all
if (!empty($_POST['foo'])) {Mac
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Is there a problem with using
??
Code: Select all
if($var!="")
{
echo "thank you!";
}
else
{
echo "please fill everything out..";
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
beforehand, then you could do:
Mac
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'] : '';Code: Select all
if($var != '') {
echo 'thank you!';
} else {
echo 'please fill everything out..';
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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">- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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.ex247 wrote:no error, it just prints the 'Input with name "foo" is empty'
but i didnt even post yet
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>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>