Help - Search - Members - Calendar
Full Version: Phpmail() Help
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Brandon
I am trying to do a script like the one on this site ...
http://www.bbc.co.uk/food/recipes/database...rka_83462.shtml
The "Email This Recipes" script is what i need can someone help me please?
Valency
Have you considered Viewing the source for the website, Might give some clues there.. smile.gif
Jetteh22
Well I'm assuming you know a bit about PHP.
What I would imagine you would need to do is give each recipe it's own row in a database table called "recipes"... give them all an id. So how you would do this is...

CODE
//list the recipes in order of newest to oldest

include 'connect.php'; // this is assuming you have a connect file. if not put your connect info there

//select all recipes
$sql = "SELECT * FROM recipes order by id desc";
$query = mysql_query();

//get the information from the rows for each recipe and display it
while($row = mysql_fetch_array($query))
{
$id = $row['id'];
$title = $row['title'];

// display a link for reach recipe. notice it goes to viewrecipe.php?id=$id
// what this means is that now you will know the id on the next page
echo "<a href='http://www.yoursite.com/recipes/viewrecipe.php?id=".$id."'>".$title."</a><br>";


Syntax might be wrong i'm not testing this, but i'm almost positive it will work.

That basicaly sends the user to the next page where they will actually view the recipe. Now on the next page you want to have a form (or give them a link to a form, but do the same thing with the " ?id=$id " in php, so that you'll still have the ID when they get to the email form.

the email form:

CODE
<?php
$id = $_GET['id']; // get variable id
?>
<form action="emailrecipe.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
Email: <input type="text" name="email"><br>
<input type="submit" value="submit"></form>



Just a simple little form. It gives the form a value called "id" so that we can bring ID up in the form action:

CODE
$id = $_POST['id']; // get id from the form
$email = $_POST['emal']; // get email from the form

include 'connect.php'; // include connecting to mysql database info

$sql = "SELECT * FROM recipes WHERE id=\"$id\""; // select the recipe from recipes
$query = mysql_query();

while($row = mysql_fetch_array($query)) // get all the info from the recipe
{
$title = $row['title']; // title or recipe
$info = $row['info']; // all the information, assuming all of the information is in one column of the mysql db
}

$subject = $title . " recipe information requested."; // gives the email a subject
$message = $title . "<br><br>" .  $info; // puts the title and information together to form the email body


mail($email, $subject, $message);

echo "mail sent";


I hope that helped at least a little bit.. It took me like 45 minutes lol
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.