sted999 Posted April 3, 2009 Share Posted April 3, 2009 Hiya. Im really stuck on this problem, ive ages on it and its driving me mad! Ive only really just started using PHP so im struggling. What i am trying to do is query the database to find the email address, which i have managed to do. Then i want to use the values in the variable to use in a form that will email them. This is what I have got so far, any help would be amazing because I am so stuck with it. The error I get is just the white screen/page. Thanks. <?php $username=($_POST['username']); echo "User selected = $username"; include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = ("SELECT email FROM Member WHERE loginName='$username'"); $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["email"]; } //mysql_free_result($result); FROM PERVIOUS QUERY $sysmail = $row["email"]; if ($_POST['sendmessage']) { if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } elseif(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } elseif(mail($sysmail, stripslashes(trim($_POST['subject'])), stripslashes(trim($_POST['message'])), "From: " . $_POST['email'] . "\r\n")) { $messages = "Please wait for a response from the user you messaged.<br>"; } else { $messages = "Error. Please contact the administrator.<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted April 3, 2009 Share Posted April 3, 2009 Im sorry, but what exactly is the problem? Are you wanting to email the person once everything has been validated? Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 3, 2009 Author Share Posted April 3, 2009 Sorry it wasnt very clear was it. Here is a break down of what i want to achieve User enters a login name(of another user) Code then querys the MySQL database (using the login name from before), finds the email address of the login name specified The result (email address) is then stored a varible and used in the contact form I have done it this way so they is form of security, I am not wanting to display email addresses. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted April 3, 2009 Share Posted April 3, 2009 Try this <?php $username=($_POST['username']); echo "User selected = $username"; include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = ("SELECT email FROM Member WHERE loginName='$username'"); $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } $row = mysql_fetch_assoc($result)) $sysmail = $row["email"]; if ($_POST['sendmessage']) { if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } elseif(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } elseif(mail($sysmail, stripslashes(trim($_POST['subject'])), stripslashes(trim($_POST['message'])), "From: " . $_POST['email'] . "\r\n")) { $messages = "Please wait for a response from the user you messaged.<br>"; } else { $messages = "Error. Please contact the administrator.<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
Benmcfc Posted April 3, 2009 Share Posted April 3, 2009 Are you getting an error message or is it simply not sending the email? If so, does changing your final if statement to this work: if ($_POST['sendmessage']) { if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } elseif(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } else { //separating the variables here is unnecessary but helpful for readability $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; mail($sysmail, $subject, $message, $headers); $messages = "Please wait for a response from the user you messaged.<br>"; } } Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 4, 2009 Author Share Posted April 4, 2009 Thanks for the suggestions guys but none of the answers seemed to work. Im starting to think im going about this completly the wrong way. Here is all the code ive got so far - <?php $username=($_POST['username']); echo "User selected = $username"; include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = ("SELECT email FROM Member WHERE loginName='$username'"); $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } $row = mysql_fetch_assoc($result)) $sysmail = $row["email"]; if ($_POST['sendmessage']) { if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } elseif(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } else { //separating the variables here is unnecessary but helpful for readability $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; mail($sysmail, $subject, $message, $headers); $messages = "Please wait for a response from the user you messaged.<br>"; } } ?> <form action="" method="post"> <span style="color:#FF0000; font-weight:bold;"><?=$messages ?></span> <table width="50%" border="0"> <tr> <td width="50%" valign="baseline"><div id="cf-email" align="right">Your email Address: </div></td> <td width="50%" valign="baseline"><input type="ext" id="email" name="email" size="50"/></td> </tr> <tr> <td valign="baseline"><div id="cf-subject" align="right">Subject: </div></td> <td valign="baseline"><input type="ext" id="subject" name="subject" size="50"/></td> </tr> <tr> <td valign="top"><div id="cf-message" align="right">Message: </div></td> <td valign="top"><textarea name="message" cols="50" rows="10" id="message"></textarea></td> </tr> <tr> <td colspan="2" valign="top"><div align="center"> <input type="submit" value="Send Message"> </div> </td> </tr> </table> <input type="hidden" name="sendmessage" value="1"></form> The error im getting is just the white page. Thanks. Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 4, 2009 Share Posted April 4, 2009 If you are getting a blank page odds are your error reporting is off, you need to add a semicolon to the end of the line: $row = mysql_fetch_assoc($result)) That should of generated and error by itself. Tray adding a semicolon at the end there and see if it helps. Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 5, 2009 Author Share Posted April 5, 2009 Nah, im still getting the white page. Do I have to enter a form action in the form following the php code, or is the php script in completly the wrong place? Also it appears that the 3rd if statement is not working, although I cant see why? Here is the latest code: <?php $username=($_POST['username']); echo "User selected = $username"; include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = ("SELECT email FROM Member WHERE loginName='$username'"); $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } $row = mysql_fetch_assoc($result); $sysmail = $row["email"]; if ($_POST['sendmessage']) { if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } else if(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } else { $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; mail($sysmail, $subject, $message, $headers); $messages = "Please wait for a response from the user you messaged.<br>"; } } ?> <form action="" method="post"> <span style="color:#FF0000; font-weight:bold;"><?php = $messages ?></span> <table width="50%" border="0"> <tr> <td width="50%" valign="baseline"><div id="cf-email" align="right">Your email Address: </div></td> <td width="50%" valign="baseline"><input type="ext" id="email" name="email" size="50"/></td> </tr> <tr> <td valign="baseline"><div id="cf-subject" align="right">Subject: </div></td> <td valign="baseline"><input type="ext" id="subject" name="subject" size="50"/></td> </tr> <tr> <td valign="top"><div id="cf-message" align="right">Message: </div></td> <td valign="top"><textarea name="message" cols="50" rows="10" id="message"></textarea></td> </tr> <tr> <td colspan="2" valign="top"><div align="center"> <input type="submit" value="Send Message"> </div> </td> </tr> </table> <input type="hidden" name="sendmessage" value="1"></form> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 when in test mode, you should really make you you have some else statements for your if's, so if there's a problem, you know where it is. make sure you have: error_reporting(E_ALL); placed at the top of your script(s) when testing. lose the brackets around your $_POST. $username=($_POST['username']); use isset when testing a passed string... if (isset($_POST['sendmessage'])) { //give this guy an 'else' so you can see what's going on... if(!isset($_POST['message'])) { $messages = "You have not entered a message. Please enter a message in the comments box below and submit.<br>"; } else if(!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages = "You have not entered a valid email address. It must be in the format name@provider.com.<br>"; } else { $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; mail($sysmail, $subject, $message, $headers); $messages = "Please wait for a response from the user you messaged.<br>"; } } else { die('no go'); } ?> change this to: <span style="color:#FF0000; font-weight:bold;"><?php echo $messages ?></span> using the '=' sign is not good practice. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 consider this... <?php include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = mysql_query(sprintf("SELECT email FROM Member WHERE loginName='%s'", mysql_real_escape_string($_POST['username']))) or die (mysql_error()); if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_assoc($result); $sysmail = $row["email"]; if (isset ($_POST['sendmessage'])) { if (!isset ($_POST['message'])) { $messages[] = "You have not entered a message. Please enter a message in the comments box below and submit."; } if (!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages[] = "You have not entered a valid email address. It must be in the format name@provider.com."; } if (is_array($messages) { foreach ($messages as $value) { echo $value.'<br />'; } } else { $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; $mail = mail ($sysmail, $subject, $message, $headers) ? true : false; if ($mail) { $messages = "Mail sent!"; } else { $messages = "Please wait for a response from the user you messaged."; } } } else { header("Location: /path/to/form"); exit; } } else { header("Location: /path/to/login"); exit; } ?> <form action="" method="post"> <span style="color:#FF0000; font-weight:bold;"><?php if (isset ($messages)) echo $messages; ?></span> <table width="50%" border="0"> <tr> <td width="50%" valign="baseline"><div id="cf-email" align="right">Your email Address: </div></td> <td width="50%" valign="baseline"><input type="ext" id="email" name="email" size="50" /></td> </tr> <tr> <td valign="baseline"><div id="cf-subject" align="right">Subject: </div></td> <td valign="baseline"><input type="ext" id="subject" name="subject" size="50" /></td> </tr> <tr> <td valign="top"><div id="cf-message" align="right">Message: </div></td> <td valign="top"><textarea name="message" cols="50" rows="10" id="message"></textarea></td> </tr> <tr> <td colspan="2" valign="top"><div align="center"> <input type="submit" value="Send Message" /> </div> </td> </tr> </table> <input type="hidden" name="sendmessage" value="1" /> </form> Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks for the tips, all i seem to be getting though is white page again? Am i missing something completly obvious? Also Im a little confused as to the order of code. The php is above the form, does this create a problem? Do i need to refer to the php instead in the action? I have slightly changed the code so that you enter the person you want to contact user name on the same form. Thanks, Stephen. Code so far - <?php include ('connect.php'); mysql_select_db("a6188092") or die(mysql_error()); $sql = mysql_query(sprintf("SELECT email FROM Member WHERE loginName=loginName='%s'", mysql_real_escape_string($_POST['username']))) or die (mysql_error()); if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_assoc($result); $sysmail = $row["email"]; if (isset ($_POST['sendmessage'])) { if (!isset ($_POST['message'])) { $messages[] = "You have not entered a message. Please enter a message in the comments box below and submit."; } if (!preg_match("/^.*?@.*?$/", $_POST['email'])) { $messages[] = "You have not entered a valid email address. It must be in the format name@provider.com."; } if (is_array($messages) { foreach ($messages as $value) { echo $value.'<br />'; } } else { $subject = stripslashes(trim($_POST['subject'])); $message = stripslashes(trim($_POST['message'])); $headers = "From: " . $_POST['email']; $mail = mail ($sysmail, $subject, $message, $headers) ? true : false; if ($mail) { $messages = "Mail sent!"; } else { $messages = "Please wait for a response from the user you messaged."; } } } else { header("Location: error.php"); exit; } } else { header("Location: index.php"); exit; } ?> <form action="" method="post"> <span style="color:#FF0000; font-weight:bold;"><?php if (isset ($messages)) echo $messages; ?></span> <table width="50%" border="0"> [b] <tr> <td width="50%" valign="baseline"><div id="username" align="right">User you want to conatct: </div></td> <td width="50%" valign="baseline"><input type="ext" id="username" name="username" size="50" /></td> </tr> [/b] <tr> <td width="50%" valign="baseline"><div id="cf-email" align="right">Your email Address: </div></td> <td width="50%" valign="baseline"><input type="ext" id="email" name="email" size="50" /></td> </tr> <tr> <td valign="baseline"><div id="cf-subject" align="right">Subject: </div></td> <td valign="baseline"><input type="ext" id="subject" name="subject" size="50" /></td> </tr> <tr> <td valign="top"><div id="cf-message" align="right">Message: </div></td> <td valign="top"><textarea name="message" cols="50" rows="10" id="message"></textarea></td> </tr> <tr> <td colspan="2" valign="top"><div align="center"> <input type="submit" value="Send Message" /> </div> </td> </tr> </table> <input type="hidden" name="sendmessage" value="1" /> </form> Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 7, 2009 Share Posted April 7, 2009 Change: <?php if (isset ($messages)) echo $messages; ?> to <?php if (isset ($messages)) { echo $messages; } ?> and if (is_array($messages) to if (is_array($messages)) Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 7, 2009 Author Share Posted April 7, 2009 Still just the white page ??? Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 7, 2009 Share Posted April 7, 2009 $sql = mysql_query(sprintf("SELECT email FROM Member WHERE loginName=loginName='%s'", mysql_real_escape_string($_POST['username']))) or die (mysql_error()); if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_assoc($sql); $sysmail = $row["email"]; i changed $row = mysql_fetch_assoc($result); to $row = mysql_fetch_assoc($sql); Your query would have been empty otherwise. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 sorry, i don't test everything i put out here .. i do what i can to keep everything uniformed, but it's up to you to make sure you have error_reporting on and such to pick up any syntax errors. in your query you have: loginName=loginName='%s' lose one of the loginName= Quote Link to comment Share on other sites More sharing options...
sted999 Posted April 7, 2009 Author Share Posted April 7, 2009 I thought that might be the problem, I was going to ask someone about it but you beat me too it. Thanks for all your help guys, this problem is now solved your all legends. Thanks again, Stephen. 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.