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!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello,
I'm very new at PHP and I have this code written for school that I have to keep building off of. I'm almost complete with it, however I need to hide the empty fields when a user leaves a field blank when filling out form.
How do I write the php code when I want to hide the empty fields in this form and when using the present code???
For example (Here's the form:):
Full Name:
Primary Phone:
Secondary Phone:
Primary Email:
Secondary Email:
Comments:
Now, if the user enters info for everything except "Secondary Phone:" and "Secondary Email:, I only want that info displayed. For example: [b][u]IN OTHER WORDS, I DON'T WANT THE EMPTY FIELDS TO BE DISPLAYED[/u].[/b]
Full Name:
John Smith
Primary Phone:
518-555-1234
Primary Email:
1234@5678.com
Comments:
Hello
I'm very new at PHP so I may not even make sense half the time.
Here is my code: [syntax="php"]<?php
$fp=fopen("/studfs2/02441040/homepage/guestbook.txt","a");
fwrite($fp, "Full Name:~ ".$_POST["full_name"]);
fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]);
fwrite($fp, "~Secondary Phone:~ ".$_POST["secondary_phone"]);
fwrite($fp, "~Primary Email:~ ".$_POST["primary_email"]);
fwrite($fp, "~Secondary Email:~ ".$_POST["secondary_email"]);
fwrite($fp,"~Comments:~ ".$_POST["form_comments" ]."\n");
fclose($fp);
$fp=fopen("/studfs2/02441040/homepage/guestbook.txt","r");
while (!feof($fp)){
$items = fgets($fp); {
$pieces=explode("~",$items);
echo "$pieces[0]<br />";
echo "$pieces[1]<br />";
echo "$pieces[2]<br />";
echo "$pieces[3]<br />";
echo "$pieces[4]<br />";
echo "$pieces[5]<br />";
echo "$pieces[6]<br />";
echo "$pieces[7]<br />";
echo "$pieces[8]<br />";
echo "$pieces[9]<br />";
echo "$pieces[10]<br />";
echo "$pieces[11]<br /><br /><br />";
}
}
fclose($fp);
?>
The result that I get, when not filling in the "Secondary Phone" and "Secondary Email" , is:
Full Name:
John Smith
Primary Phone:
518-555-5555
Secondary Phone:
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I tried that but I'm still getting the same thing. How does this look?
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
tags when posting to these forums
Second, please indent your code
Third, duplication in code is the root of all evil. If you find yourself copy-pasting stuff, try using a loop or a function
The PHP Manual wrote:empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
This checks isset() and whether the variable is empty. Just be careful as some values may return (boolean) true from empty() even when they have a value:
The PHP Manual wrote: The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)