Thursday, February 2, 2012

PHP

I tried my luck at PHP today. The problem was to be able to create a form and have it post to a blog. Thus allowing any viewer to post to that blog. So the concept was similar to that of mlib, fml, etc. But how can someone do that for free? Or without designing a whole website in ruby on rails? Simpler than I thought.
Here's what I did:
Create an html form:
 <html>
<body>
<form method="post" action="mailer.php">
<p>Title:<input type="text" id="title" name="title" size="19"></p>
<p>Message:</p>
<textarea rows="9" name="message" cols="30" id="message"</textarea><br>
<input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset">
</form></body></html>
Which then links to a PHP file that handles it:

 <?php
$to = "to_email@site.com";
$subject = $_POST['title'];
$message = $_POST['message'];
echo "submitting... \n";
$headers = 'From: default <from@email.com>' . "\r\n";
mail($to, $subject, $message, $headers) or die("failure");
echo "done\n";
echo "redirecting in 5 seconds, otherwise click";
sleep(5);
echo "<a href=\"http://mlipro.blogspot.com\">here</a>\n";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://mlipro.blogspot.com">';
?>

For the TO address, change your blog mobile/email settings to accept posts from a "secret" email, and use that address.  
Next, upload both files to the same directory on a PHP enabled server, and link to the form on your blog.
The website: mlipro.blogspot.com

No comments:

Post a Comment