Hey everyone I'm new here. Just starting to learn PHP. Taking snippets of code I find and putting them together as a way of learning.
Right now I have this code (below). It's pretty much a guestbook that also emails me the results of the form whenever someone submits a post.
I'm having a couple issues with it though.
1.First whenever someone submits multiple paragraphs each one gets added to a new box. if you look at the last entry in this page you'll see what I mean. http://www.roxics.com/php/display.php
- Second I can't figure out how to reverse the display so that new posts show up on top.
Here is the code for the submit page:
<?php
if ($_POST['submit']) {
print "Thank you for your submission. <br><A HREF='display.php'>Click here to view your message.</A>";
}
?>
<?php
$data = fopen( "email.txt" , "a+" );
$fillin = $_POST['name']."|".$_POST['email']."|".$_POST['message'];
if ($_POST['submit']) {
$required_fields = explode(",", $_POST['required']);
$error = 0;
foreach($required_fields as $fieldname) {
if ($_POST[$fieldname] == "") {
$error++;
}
}
if ($error == 0) {
if (strstr($_POST['email'], "@") and strstr($_POST['email'], ".")) {
} else {
$errormessage = "<b>The email address you entered does not appear to be valid<br></b>";
}
if(!$data)
{
echo "Couldn't open the data file. Perhaps some other time.";
exit;
}
fwrite( $data, $fillin."\r\n" );
fclose( $data );
mail("mstesh@hotmail.com" , "Message from Web Form", $_POST['message'], "From: $_POST[name] <$_POST[email]>");
exit;
} else {
$errormessage = "<b>You have left some required fields in the form blank! Please fill in the form completely.";
}
}
?>
<br><br>
<?=$errormessage?>
<p>
<center>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="500" border="0" cellpadding="5" cellspacing="0">
<tr><td>Your name:</td><td><input type="text" name="name" value="<?=$_POST['name']?>"></td></tr>
<tr><td>Your email:</td><td><input type="text" name="email" value="<?=$_POST['email']?>"><td><tr>
<tr><td>Your message:</td><td><textarea name="message" ROWS=15 COLS=40><?=$_POST['message']?></textarea></td></tr></table>
<input type="hidden" name="required" value="name,email,message">
<input type="submit" name="submit" value="submit">
</center>
This page can be found here: http://www.roxics.com/php/contact2.php
Now here is the code for the display page:
<div align="center">
<center>
<table border="0" cellpadding="5" cellspacing="0" width="60%">
<tr>
<td width="100%">
<?php
$fp = file("email.txt");
for($i=0;$i<count($fp);$i++)
{
$array=explode("|",$fp[$i]);
echo "<div class='message'>".$array[0]."";
echo " (".$array[1].")<br><hr><p>";
echo "".$array[2]."</div><p>";
}
?>
</td>
</tr>
</table>
</center>
</div>
- Also what would be a good way to make the email address a link?
Thank you