Page 1 of 1

I Need HELP!!! Hiding empty fields...

Posted: Thu May 03, 2007 3:30 pm
by paulm
pickle | Please use

Code: Select all

,

Code: Select all

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:

Primary Email:
123@4567.com
Secondary Email:

Comments:
hi there


As you can see, the empty fields are displayed. I want them to be hidden to look like this:

Full Name:
John Smith
Primary Phone:
518-555-5555
Primary Email:
123@4567.com
Comments:
hi there


Can anyone help me? The thing is, I have to use the current code. I appreciate it! Thank you!


Paul



pickle | Please use[/syntax]

Code: Select all

,

Code: Select all

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]

Posted: Thu May 03, 2007 3:35 pm
by arturm
use

Code: Select all

if (isset($_POST['xxxx'])) {
    fwrite(....);
}

Posted: Thu May 03, 2007 3:39 pm
by paulm
What goes in the fwrite parentheses?

Posted: Thu May 03, 2007 3:47 pm
by nickvd
It is unlikely that you will find help regarding school work on these forums...

Posted: Thu May 03, 2007 3:51 pm
by arturm
I will give you another example

Code: Select all

if (isset($_POST["primary_phone"])) { 
   fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]); 
}
this way if something is not set it wont be written to the file.

Posted: Thu May 03, 2007 4:11 pm
by paulm
Everah | Please use

Code: Select all

,

Code: Select all

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?

Code: Select all

<?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 />";
  if (isset($_POST["full_name"])) {
   fwrite($fp, "Full Name:~ ".$_POST["full_name"]);
} 
  
  echo "$pieces[2]<br />";
  echo "$pieces[3]<br />";
  if (isset($_POST["primary_phone"])) {
   fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]);
} 
  
  echo "$pieces[4]<br />";
  echo "$pieces[5]<br />";
  if (isset($_POST["secondary_phone"])) {
   fwrite($fp, "~Secondary Phone:~ ".$_POST["secondary_phone"]);
}   
  echo "$pieces[6]<br />";
  echo "$pieces[7]<br />";
  if (isset($_POST["primary_email"])) {
   fwrite($fp, "~Primary Email:~ ".$_POST["primary_email"]);
}   
  echo "$pieces[8]<br />";
  echo "$pieces[9]<br />";
  if (isset($_POST["secondary_email"])) {
    fwrite($fp, "~Secondary Email:~ ".$_POST["secondary_email"]);
}    
  echo "$pieces[10]<br />";
  echo "$pieces[11]<br /><br /><br />";             
  if (isset($_POST["form_comments"])) {                                               
	fwrite($fp,"~Comments:~ ".$_POST["form_comments"]);
}  
  
 }
   }  
 
  fclose($fp);
 
 ?>

Everah | Please use

Code: Select all

,

Code: Select all

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]

Posted: Thu May 03, 2007 4:34 pm
by arturm
how about

Code: Select all

if (isset($_POST["primary_phone"]) && $_POST["primary_phone"] != '') {
   fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]);
}
if var is set and is not empty

Posted: Thu May 03, 2007 4:50 pm
by paulm
I tried that:

Code: Select all

<?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 />";
  if (isset($_POST["full_name"]) && $_POST["full_name"] != '') {
   fwrite($fp, "Full Name:~ ".$_POST["full_name"]);
} 
  
  echo "$pieces[2]<br />";
  echo "$pieces[3]<br />";
  if (isset($_POST["primary_phone"]) && $_POST["primary_phone"] != '') {
   fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]);
} 
  
  echo "$pieces[4]<br />";
  echo "$pieces[5]<br />";
  if (isset($_POST["secondary_phone"]) && $_POST["secondary_phone"] != '') {
   fwrite($fp, "~Secondary Phone:~ ".$_POST["secondary_phone"]);
}   
  echo "$pieces[6]<br />";
  echo "$pieces[7]<br />";
  if (isset($_POST["primary_email"]) && $_POST["primary_email"] != '') {
   fwrite($fp, "~Primary Email:~ ".$_POST["primary_email"]);
}   
  echo "$pieces[8]<br />";
  echo "$pieces[9]<br />";
  if (isset($_POST["secondary_email"]) && $_POST["secondary_email"] != '') {
    fwrite($fp, "~Secondary Email:~ ".$_POST["secondary_email"]);
}    
  echo "$pieces[10]<br />";
  echo "$pieces[11]<br /><br /><br />";             
  if (isset($_POST["form_comments"]) && $_POST["form_comments"] != '') {                                               
	fwrite($fp,"~Comments:~ ".$_POST["form_comments"]);
}  
  
 }
   }  
 
  fclose($fp);
 
 ?>


As far as the variables being set, does this help...it's my html form page?

Code: Select all

<form action="form_process.php" method="post">

<table border="1">
<tr>
  <th>Name</th>
  <th align="center" style="color:blue"><input type="text" name="full_name" /></th>
</tr>  

<tr>
  <th>Primary Phone Number</th>
  <th align="center" style="color:blue"><input type="text" name="primary_phone" /></th>
</tr>

<tr>
  <th>Secondary Phone Number</th>
  <th align="center" style="color:blue"><input type="text" name="secondary_phone" /></th>
</tr>



<tr>
  <th>Primary Email Address</th>
  <th align="center" style="color:blue"><input type="text" name="primary_email" /></th>
</tr>

<tr>
  <th>Secondary Email Address</th>
  <th align="center" style="color:blue"><input type="text" name="secondary_email" /></th>
</tr>

<tr>
  <th>Comments</th>
  <th align="center" style="color:blue"><input type="text" name="form_comments" /></th>
</tr>

<tr>
  <td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

Thanks again for your time and patience!!
Paul

Posted: Thu May 03, 2007 5:13 pm
by Ambush Commander
I won't give you the answer, but I will give you some suggestions.

First, please use

Code: Select all

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

Posted: Thu May 03, 2007 6:30 pm
by RobertGonzalez
empty() is your friend.

Posted: Thu May 03, 2007 6:50 pm
by arturm
How about that ?

Code: Select all

<?php

$fp=fopen("/studfs2/02441040/homepage/guestbook.txt","a");


if (isset($_POST["full_name"]) && $_POST["full_name"] != '') {
   fwrite($fp, "Full Name:~ ".$_POST["full_name"]);
}

if (isset($_POST["primary_phone"]) && $_POST["primary_phone"] != '') {
   fwrite($fp, "~Primary Phone:~ ".$_POST["primary_phone"]);
}

if (isset($_POST["secondary_phone"]) && $_POST["secondary_phone"] != '') {
   fwrite($fp, "~Secondary Phone:~ ".$_POST["secondary_phone"]);
} 

if (isset($_POST["primary_email"]) && $_POST["primary_email"] != '') {
   fwrite($fp, "~Primary Email:~ ".$_POST["primary_email"]);
}   
 
if (isset($_POST["secondary_email"]) && $_POST["secondary_email"] != '') {
    fwrite($fp, "~Secondary Email:~ ".$_POST["secondary_email"]);
}  

if (isset($_POST["form_comments"]) && $_POST["form_comments"] != '') {                                               
        fwrite($fp,"~Comments:~ ".$_POST["form_comments"]);
} 

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);
 
 ?>
I hope that you can see the difference.
You have to check if field is not empty before you insert it into the file.

Posted: Thu May 03, 2007 7:01 pm
by RobertGonzalez
*cough*
empty()
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)

Posted: Thu May 03, 2007 7:24 pm
by arturm
Everah you are right it looks like empty() does the job better.
I have to start using it in my code :wink: