...

View Full Version : Passing the variable for an e-mail address through the URL for use with PHP mail.



Yay
02-22-2010, 07:00 PM
Hi there,

I've came across a problem. My mail script works fine, just that I need to know how to get the value for an e-mail from the URL (passed on via a different page). Here's my code so far for both pages. Certsend is the one which does the main mail work. Basically, I'd like the value of $to to be what the value of mail is in the URL.

Certsend

<?php

$to = 'yourmail@k.com';
$subject = 'You have receieved a certificate from the Pyongyang Pair!';
$from = 'Pyongyang Pair';
$name = $_GET['name'];


//begin of HTML message
$message = '<!DOCTYPE html>
<html>
<head>
<title>Pyongyang Pair Certificate</title>

<!-- CSS -->

<style type="text/css">
body{
text-align: center;
}

#cert{
width: 1648px;
height: 821px;
margin: 0 auto;
background: url(\'http://kwivia.co.uk/images/certjpg.jpg\');
margin-left: -10px;
margin-top: -10px;
margin-bottom: -10px;
}

#input{
padding-top: 40em;
float: left;
padding-left: 9em;
}
.input{
width: 900px;
height: 80px;
background: none;
font-family: georgia;
color: #fff;
font-size: 50px;
text-align: center;
border: 0;
}
</style>
</head>
<body>

<div id="cert">

<div id="input">
$name
</div>

</div>

';
?>

<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';


if(mail($to, $subject, $message, "$headers From: $from")){
echo "Mail sent";
}else{
echo "Mail send failure - message not sent"; }
?>

Home.php

<?php


$db ='pp';

require("connect.php");


// select db
mysql_select_db($db) or die ("Unable to select database!");

// pagination of sql results
// find number of rows in table
$sql= "SELECT COUNT(*) FROM members";
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

//number of rows per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;


// query
$query = "SELECT
members.Name
, members.Member_ID
, members.dob
, members.Mail
, COUNT(gigs.name_of_gig) AS gig_count
FROM members AS members
LEFT JOIN gigs AS gigs
ON members.Member_ID=gigs.member_id
GROUP BY members.Name, members.Member_ID, members.dob, members.Mail
ORDER BY Member_ID DESC LIMIT $offset, $rowsperpage";



// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());


// return rows check
if (mysql_num_rows($result) > 0) {
// yes
// print rows
echo "<table cellpadding=10 border=0><strong><td><strong>Name</strong></td><td><strong>Gigs Attended</strong></td><td><strong>Date of Birth</strong></td><td><strong>Email</strong></td><td><strong>Add new Gig</strong></td><td><strong>Send Certificate</strong>";

while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['gig_count']."</td>";
echo "<td>" .$row['dob']."</td>";
echo "<td>".$row['Mail']."</td>";
echo "<td><a href=\"/records/add_gigs.php?id=".$row['Member_ID']."&?name=".$row['Name']."\"><button class=\"submit\">Add new Gig</button></a></td>";
if($row['gig_count']> 2) {
echo "<td><a href=\"/certsend.php?name=".$row['Name']."&?Mail=".$row['Mail']."\"><button class=\"submit\">Make Certificate!</button></a></td>";


} echo "</tr>";
}
echo "</table>";
}
else {
// if no
// print message
echo "no data found";
}


// range of num links to show
$range = 3;

echo "<span class=\"pag\"><p style=\"font-size: 16px;\" align=\"center\">Page ";

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo "<button class=\"pagination\" style=\"font-size: 15px; font-weight: bold;'\">&nbsp;$x&nbsp;</button>";
// if not current page...
} else {
// make it a link
echo "&nbsp;<button class=\"pagination\" style=\"font-size: 15px;'\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a></button>";
} // end else
} // end if
} // end for


echo "&nbsp;out of $totalpages</h2></span>";

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($con);

?>


Anything else, ask!
Thanks,
Jack

Fou-Lu
02-22-2010, 07:17 PM
$to would simply become $_GET['Mail'].
You'll want to validate it though to ensure that its at least just an email address. Look into the filter_var() (http://php.net/manual/en/function.filter-var.php) to see the validation filter for emails. Works not too bad, the filter is FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL.


Hmm, wait a minute. Can you even wrap a button element with an anchor element?

Yay
02-22-2010, 07:31 PM
$to would simply become $_GET['Mail'].
You'll want to validate it though to ensure that its at least just an email address. Look into the filter_var() (http://php.net/manual/en/function.filter-var.php) to see the validation filter for emails. Works not too bad, the filter is FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL.


Hmm, wait a minute. Can you even wrap a button element with an anchor element?


I'll look into the aforementioned function, and I the button seems to work fine with the anchor attached to it—sends me to the correct page.

Yay
02-22-2010, 07:35 PM
Sorry for the double post:

When I try
$to = $_GET['Mail'];
$subject = 'You have receieved a certificate from the Pyongyang Pair!';
$from = 'Pyongyang Pair';
$name = $_GET['name'];

I get a PHP error which relates to the line with $to on it, saying that there's an undefined index. Is there something I'm missing in home.php?

MattF
02-22-2010, 07:44 PM
Get rid of the ? preceding Mail on this line:



echo "<td><a href=\"/certsend.php?name=".$row['Name']."&?Mail=".$row['Mail']."\"><button class=\"submit\">Make Certificate!</button></a></td>";

Fou-Lu
02-22-2010, 08:44 PM
Get rid of the ? preceding Mail on this line:



echo "<td><a href=\"/certsend.php?name=".$row['Name']."&?Mail=".$row['Mail']."\"><button class=\"submit\">Make Certificate!</button></a></td>";


Hah, yeah that will do it. Too much vb methinks O.o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum