paulm Posted April 30, 2007 Share Posted April 30, 2007 How do I write the php code for a form when I only want what the user enters in the form to be displayed?? 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:", I only want that info displayed. For example: Full Name: John Smith Primary Phone: 518-555-1234 Primary Email: 1234@5678.com Secondary Email: 2345@6789.com Comments: Hello I'm very new at PHP so I may not even make sense half the time. :-) Thank you!! Paul :( Quote Link to comment Share on other sites More sharing options...
Gmunky Posted April 30, 2007 Share Posted April 30, 2007 if($secondphone != null) echo "Secondary Phone: $secondphone"; Quote Link to comment Share on other sites More sharing options...
paulm Posted April 30, 2007 Author Share Posted April 30, 2007 Hi, Thanks for the help!! This is my code: <?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 />"; if($full_name!= null) echo "Full Name: $full_name"; 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 added what you suggested up above: echo "$pieces[0]<br />"; if($full_name!= null) echo "Full Name: $full_name"; But what I'm still getting is: Full Name: Primary Phone: 518-555-5555 Secondary Phone: 518-555-6789 Primary Email: 123@4567.com Secondary Email: 234@5678.com Comments: hi there The 'Full Name:' is still there, even if the info (name) is not entered. Any suggestions? Thanks again! Paul Quote Link to comment Share on other sites More sharing options...
Gmunky Posted April 30, 2007 Share Posted April 30, 2007 try if($_POST["full_name"] != null) echo "Full Name:<br> ". $_POST["full_name"]. "<br>"; if($_POST["primary_phone"] != null) echo "Primary Phone:<br> ". $_POST["primary_phone"]. "<br>"; Do this for all the posts. I hope this helps Quote Link to comment Share on other sites More sharing options...
paulm Posted May 1, 2007 Author Share Posted May 1, 2007 Thank you!! I will give it a try! Quote Link to comment Share on other sites More sharing options...
paulm Posted May 2, 2007 Author Share Posted May 2, 2007 Hello again! I actually tried your suggestions but I'm still not getting it to work right. May I be putting things in the wrong place? This is what I have now: <?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($pieces[1]){ echo 'Full Name:'".$full_name."; } else{ echo ""; } echo "$pieces[2]<br />"; echo "$pieces[3]<br />"; if($pieces[3]){ echo "Primary Phone:.$primary_phone."; } else{ echo "";} echo "$pieces[4]<br />"; echo "$pieces[5]<br />"; if($pieces[5]) { echo "Secondary Phone:.$secondary_phone."; } else{ echo "";} echo "$pieces[6]<br />"; echo "$pieces[7]<br />"; if($pieces[7]){ echo "Primary Email:.$primary_email."; } else{ echo "";} echo "$pieces[8]<br />"; echo "$pieces[9]<br />"; if($pieces[9]){ echo "Secondary Email:.$secondary_email."; } else{ echo "";} echo "$pieces[10]<br />"; echo "$pieces[11]<br /><br /><br />"; if($pieces[11]){ echo "Comments:.$form_comments."; } else{ echo "";} } } fclose($fp); ?> Any other suggestions? This is very frustrating. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
corbin Posted May 2, 2007 Share Posted May 2, 2007 Is it writing into the file correctly? It may not be the most efficient thing ever, but sometimes when comparing $_POST or $_GET things, if the variable exists, but it isn't set it will not == null.... Meaning $var != null returns true. You could try something like if(isset($var)) or if that doesn't work you could try something like if(strlen($var) > 0). Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 I'm very new at this. I'm taking php in school and I'm having a rough time. I've been working with my instructor on and off, but I feel like the course itself was more advanced from the start...as if I already knew alot of the stuff, which isn't the case. I'm starting from scratch. I have changed the code to this: <?php else{ echo ""; } echo "$pieces[2]<br />"; echo "$pieces[3]<br />"; if($pieces[3]){ echo "$primary_phone"; } else{ echo "";} echo "$pieces[4]<br />"; echo "$pieces[5]<br />"; if($pieces[5]) { echo "$secondary_phone"; } else{ echo "";} echo "$pieces[6]<br />"; echo "$pieces[7]<br />"; if($pieces[7]){ echo "$primary_email"; } else{ echo "";} echo "$pieces[8]<br />"; echo "$pieces[9]<br />"; if($pieces[9]){ echo "$secondary_email"; } else{ echo "";} echo "$pieces[10]<br />"; echo "$pieces[11]<br /><br /><br />"; if($pieces[11]){ echo "$form_comments"; } else{ echo "";} } } fclose($fp); ?> I'm getting this result: (leaving the entries "Secondary Phone:" and "Secondary Email:" blank...but still displaying the "Secondary Phone:" and the "Secondary Phone:"...which I'm trying not to display the info not entered) Full Name: John Smith Primary Phone: 518-555-5555 Secondary Phone: Primary Email: 123@4567.com Secondary Email: Comments: hi there Ultimately, the result I want displayed is only the info entered : Full Name: John Smith Primary Phone: 518-555-5555 Primary Email: 123@4567.com Comments: hi there I understand that this way is probably not the best way to do a guestbook, but unfortunately this is what I have to build off because it has been built up piece by piece in previous school assignments. Sorry this is so much info in this post. Any help is very much appreciated!! Thanks! Paul Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 if($pieces[3]){ echo "$primary_phone"; } else{ echo "";} $pieces=explode("~",$items); If the field exists in the text file, which it does the way it's being written, the array will just have an empty value for the corresponding field. if you did something like $var = ""; and then did if($var) it would evaluate to true because the variable is set, it's just blank. So I would try something like: if(!empty($variable)) { //do what ever you want to do if the variable isn't blank ;p } As for your echoing of "", that's not needed because that will just output nothing. Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 I'm getting the same result. However, does it look as if I wrote it correctly? Or put the code in the correct spot? This is what I have now: while (!feof($fp)){ $items = fgets($fp); { $pieces=explode("~",$items); echo "$pieces[0]<br />"; echo "$pieces[1]<br />"; if(!empty($full_name)){ echo "$full_name"; } echo "$pieces[2]<br />"; echo "$pieces[3]<br />"; if(!empty($primary_phone)){ echo "$primary_phone"; } echo "$pieces[4]<br />"; echo "$pieces[5]<br />"; if(!empty($secondary_phone)) { echo "$secondary_phone"; } echo "$pieces[6]<br />"; echo "$pieces[7]<br />"; if(!empty($primary_email)){ echo "$primary_email"; } echo "$pieces[8]<br />"; echo "$pieces[9]<br />"; if(!empty($secondary_email)){ echo "$secondary_email"; } echo "$pieces[10]<br />"; echo "$pieces[11]<br /><br /><br />"; if(!empty($form_comments)){ echo "$form_comments"; } } } fclose($fp); ?> Thanks again! Paul Quote Link to comment Share on other sites More sharing options...
Ravo Posted May 3, 2007 Share Posted May 3, 2007 [pre]while (!feof($fp)){ $items = fgets($fp); { $pieces=explode("~",$items); echo "$pieces[0] "; echo "$pieces[1] "; if(!empty($full_name)){ echo "$full_name"; } echo "$pieces[2] "; echo "$pieces[3] "; if(!empty($primary_phone)){ echo "$primary_phone"; } echo "$pieces[4] "; echo "$pieces[5] "; if(!empty($secondary_phone)) { echo "$secondary_phone"; } echo "$pieces[6] "; echo "$pieces[7] "; if(!empty($primary_email)){ echo "$primary_email"; } echo "$pieces[8] "; echo "$pieces[9] "; if(!empty($secondary_email)){ echo "$secondary_email"; } echo "$pieces[10] "; echo "$pieces[11] "; if(!empty($form_comments)){ echo "$form_comments"; } } } fclose($fp); ?>[/pre] Except for the red brackets, I see no reason why the code shouldn't work. An alternative to their given examples is this: $fullname = isset($fullname) ? "Full Name: $fullname<br />\n" : NULL ; Do the same for $primary_phone, $secondary_phone and the others before you start echo'ing stuff, and use them like the data existed anyway. [pre] while(!feof($fp)){ $items=fgets($fp); $pieces=explode("~",$items); # Validate for empty variables $fullname = isset($fullname) ? "Full Name: $fullname\n" : NULL ; $primary_phone = isset($primary_phone) ? "Primary phone: $primary_phone\n" : NULL ; $secondary_phone = isset($secondary_phone) ? "Secondary phone: $secondary_phone\n" : NULL ; #etc #etc echo $pieces[0]. $pieces[1]. $fullname. $pieces[2]. $pieces[3]. $primary_phone. $pieces[4]. $pieces[5]. $secondary_phone; #etc etc [/pre] I hope that helps. EDIT: HTML is on. Didn't notice. That totally screws with adding line break html. =p Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 You could also not write the variables that aren't put in the form to the file.... Then when you exploded you could either count the array or check if keys existed to know whether or not to echo.... Quote Link to comment Share on other sites More sharing options...
Moon-Man.net Posted May 3, 2007 Share Posted May 3, 2007 echo the value on the variables between |'s to see what they actualy are, Because they might be set, just blanks. Just a stab int he dark. -- Nathan Quote Link to comment Share on other sites More sharing options...
Ravo Posted May 3, 2007 Share Posted May 3, 2007 If I may take a completely fresh stab at this, I'd write it this way: <?php $fp=fopen("/studfs2/02441040/homepage/guestbook.txt","a"); # Note the funky bracket system is just a cheap trick to avoid the quote and dot chaos fwrite($fp, "~{$_POST['full_name']}"); fwrite($fp, "~{$_POST['primary_phone']}"); fwrite($fp, "~{$_POST['secondary_phone']}"); fwrite($fp, "~{$_POST['primary_email']}"); fwrite($fp, "~{$_POST['secondary_email']}"); fwrite($fp, "~{$_POST['form_comments']}\n"); fclose($fp); $fp=fopen("/studfs2/02441040/homepage/guestbook.txt","r"); while (!feof($fp)){ $items = fgets($fp); $pieces=explode("~",$items); # We have 6 variables in $pieces # 0 = full name # 1 = primary phone # 2 = secondary phone # 3 = primary email # 4 = secondary email # 5 = comments # IM RUSTY IN USING EXPLODE() STUFF SO IF I'M A DIGIT OFF DON'T SUE ME $fullname = isset($pieces[0]) ? "Full Name: {$pieces[0]}<br />" : NULL; $primaryphone = isset($pieces[1]) ? "Primary Phone: {$pieces[1]}<br />" : NULL; $secondaryphone = isset($pieces[2]) ? "Secondary Phone: {$pieces[2]}<br />" : NULL; $primaryemail = isset($pieces[3]) ? "Primary Email: {$pieces[3]}<br />" : NULL; $secondaryemail = isset($pieces[4]) ? "Secondary Email: {$pieces[4]}<br />" : NULL; $comments = isset($pieces[5]) ? "Comments: {$pieces[5]}<br />" : NULL; echo "(Insert random HTML formatting here)". $fullname.$primaryphone.$secondaryphone.$primaryemail.$secondaryemail.$comments. "(More random HTML formatting)"; } fclose($fp); ?> I didn't test the code, so syntax errors might be hiding. But this does away with all the excess formatting in the database itself. Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 Okay, this is what I have now: (but I'm still getting the same result) Am I writing your examples correctly and placing them in the correct places? <?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); $fullname = isset($fullname) ? "Full Name: {$pieces[0]}<br />" : NULL ; $primary_phone = isset($primary_phone) ? "Primary Phone: {$pieces[1]}<br />" : NULL ; $secondary_phone = isset($secondary_phone) ? "Secondary Phone: {$pieces[2]}<br />" : NULL ; $primary_email = isset ($primary_email) ? "Primary Email: {$pieces[3]}<br />" : NULL ; $secondary_email = isset($secondary_email) ? "Secondary Email: {$pieces[4]}<br />" : NULL ; $form_comments = isset($form_comments) ? "Comments: {$pieces[5]}<br />" : NULL ; echo "$pieces[0]<br />"; echo "$pieces[1]<br />"; echo "$full_name"; echo "$pieces[2]<br />"; echo "$pieces[3]<br />"; echo "$primary_phone"; echo "$pieces[4]<br />"; echo "$pieces[5]<br />"; echo "$secondary_phone"; echo "$pieces[6]<br />"; echo "$pieces[7]<br />"; echo "$primary_email"; echo "$pieces[8]<br />"; echo "$pieces[9]<br />"; echo "$secondary_email"; echo "$pieces[10]<br />"; echo "$pieces[11]<br /><br /><br />"; echo "$form_comments"; } } fclose($fp); ?> Thanks again! Paul Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 $pieces=explode("~",$items); $fullname = isset($fullname) ? "Full Name: {$pieces[0]} " : NULL ; $primary_phone = isset($primary_phone) ? "Primary Phone: {$pieces[1]} " : NULL ; $secondary_phone = isset($secondary_phone) ? "Secondary Phone: {$pieces[2]} " : NULL ; $primary_email = isset ($primary_email) ? "Primary Email: {$pieces[3]} " : NULL ; $secondary_email = isset($secondary_email) ? "Secondary Email: {$pieces[4]} " : NULL ; $form_comments = isset($form_comments) ? "Comments: {$pieces[5]} " : NULL ; Where is fullname, primary_phone etcetera being set? If they aren't set pre these lines, then isset($var) will always return false, causing them to be set to null.... (PS I could be wrong but I think it would be $var = (isset($var1)) ? yes : no; instead of it without the outside parenthesis... I could be wrong though ;p.) Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 Is this the way you mean? $full_name = (isset($full_name))? "Full Name: {$pieces[0]}" : NULL ; Paul Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 Yeah, but that's not the main problem (I think the syntax used works fine).... What I was saying is that if the variables aren't set already, that will always evaluate to false, causing the variables to be set to null. Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 I'm sorry I don't understand too much about what you mean when you ask about the variables being set. What exactly do you mean? Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 Well, either you didn't post your entire code for that page, or you there's a problem.... If you check if a variable is set, and the variable hasn't been assigned, it'll always return false.... So setting a variable for the first time based on whether the variable makes no sense.... It'll always make the variables false.... Quote Link to comment Share on other sites More sharing options...
paulm Posted May 3, 2007 Author Share Posted May 3, 2007 I'm not sure if this will help. This is my entry form page which I call 'entry_form.htm' : <body> <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> Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 is $items = fgets($fp); { short for a while loop? I've never seen that before... haha And, yeah I was completely loopy about the variables not being set.... Just put the script on my local computer and it works fine.... Weird, I have no idea what's setting those variables haha.... Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 OOOHHH! I figured it out.... Those variables I was asking about aren't out putting anything.... The field names are stored in the pieces array with the values.... Haha, I have my slow moments.... Lemme think for a sec, and I'll come up with a check for if they're empty. Quote Link to comment Share on other sites More sharing options...
Ravo Posted May 3, 2007 Share Posted May 3, 2007 I just ran your latest code myself with an example guestbook.txt that I ran up myself and it works just fine when all the fields are filled. In fact, I pulled some variables and it *still* ran fine, so I'm not sure there even *is* an error. Here's a copy of the datafile I used to test it: Full Name:~fullnamevar~Primary Phone:~primaryphonevar~Secondary Phone:~secondaryphonevar~Primary email:~primaryemailvar~Secondary email:~secondaryemailvar~Comments:~commentvar Full Name:~fullnamevar2~Primary Phone:~primaryphonevar2~Secondary Phone:~~Primary email:~primaryemailvar2~Secondary email:~secondaryemailvar2~Comments:~commentvar2 Full Name:~fullnamevar3~Primary Phone:~primaryphonevar3~Secondary Phone:~secondaryphonevar3~Primary email:~~Secondary email:~secondaryemailvar3~Comments:~commentvar3 Full Name:~fullnamevar4~Primary Phone:~primaryphonevar4~Secondary Phone:~secondaryphonevar4~Primary email:~primaryemailvar4~Secondary email:~~Comments:~commentvar4 Full Name:~fullnamevar5~Primary Phone:~~Secondary Phone:~secondaryphonevar5~Primary email:~primaryemailvar5~Secondary email:~secondaryemailvar5~Comments:~commentvar5 Quote Link to comment Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 There isn't an error.... There's some unnecessary code, and it shows fields when they're blank, which I believe is the problem.... Recoding part of it now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.