Page 1 of 1

Dynamically call data from db and passing into process form

Posted: Mon Jul 12, 2010 11:26 am
by johnny2005uk
Hi,

I'm very very new to PHP/Mysql, so forgive me if i'm asking really stupid basic questions.

I've sucessfully learned how to use a form to send user data into the db (in it, I've taken their e-mail address and created a dynamic ID)

What I then have is a profile for each user, which I am hoping to create an e-mail form where the user could be contacted through without displaying their email address - for obvious anti-spamming purposes.

I have the email form code as desired in another page, but am having difficulty getting my process form to dynamically pull the user's email from the database.

I've followed the line of thinking that you use the profile page to connect to the database, pull the user's information (and display it dynamically on the page)

Then I've used a hidden form to pull and store the email address, to be sent over to the process form.

I've then tried to ammend the process form, with what I thought was correct, by using a $_POST array to call on the sent element from the form.

However, when you click on the send button, I receive the following: -

Could not send email to: Resource id #2

Any help would be wonderful!

John

Code: Select all

<?php
mysql_connect("localhost", "username", "password");
mysql_select_db ("db_name");
$genmail = mysql_query("SELECT Email FROM tablename WHERE ID='6' ");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

echo "<form action='processform.php' method='POST'>
<input type='hidden' name='email' value='$genmail'>
<input type='submit' value='Next Page'>
<input name='good_url' type='hidden' id='good_url' value='thanks.php' />
</form>\n";

?>
</body>
</html>

Code: Select all

<?php
	$_email_recipient = $_POST[email];

	/* end fo config */
	
	
	
	
	
	
	
	$_referer = $_SERVER['HTTP_REFERER'];
	
	$_this_server = "http:\/\/" . $_SERVER['SERVER_NAME'];
	
	if (preg_match ("/^" . $_this_server . "/", $_referer)){
		
		foreach ($_POST as $_key => $_value){
			
			if ($_key == "good_url"){
				
				$_good_url = $_value;
				
			} else {
			
				$_message .= "$_key: $_value<br/>";
				
			}
			
		}
		
		$_subject = "Generated email from: $_referer";
		
		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		
		// Additional headers
		$headers .= "To: $_email_recipient <$_email_recipient>\r\n";
		$headers .= "From: Your Website <donotreply@mysite.com>\r\n";
				
		if (file_exists ($_good_url)){
		
			if (@mail ($_email_recipient, $_subject, $_message, $headers)){
			
				header ("location:$_good_url");
				
			} else {
			
				print ("Could not send email to: $_email_recipient");
				
			}
			
		} else {
				
			print ("Please specify a valid 'good_url'");
			
		}
		
	}
	
	exit;
	
?>

Re: Dynamically call data from db and passing into process f

Posted: Mon Jul 12, 2010 12:13 pm
by Jade
You need quotes around the field name in the $_POST function.

Code: Select all

<?php
        $_email_recipient = $_POST['email'];

        /* end fo config */
       
        $_referer = $_SERVER['HTTP_REFERER'];
       
        $_this_server = "http:\/\/" . $_SERVER['SERVER_NAME'];
       
        if (preg_match ("/^" . $_this_server . "/", $_referer)){
               
                foreach ($_POST as $_key => $_value){
                       
                        if ($_key == "good_url"){
                               
                                $_good_url = $_value;
                               
                        } else {
                       
                                $_message .= "$_key: $_value<br/>";
                               
                        }
                       
                }
               
                $_subject = "Generated email from: $_referer";
               
                // To send HTML mail, the Content-type header must be set
                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
               
                // Additional headers
                $headers .= "To: $_email_recipient <$_email_recipient>\r\n";
                $headers .= "From: Your Website <donotreply@mysite.com>\r\n";
                               
                if (file_exists ($_good_url)){
               
                        if (@mail ($_email_recipient, $_subject, $_message, $headers)){
                       
                                header ("location:$_good_url");
                               
                        } else {
                       
                                print ("Could not send email to: $_email_recipient");
                               
                        }
                       
                } else {
                               
                        print ("Please specify a valid 'good_url'");
                       
                }
               
        }
       
        exit;
       
?>


Re: Dynamically call data from db and passing into process f

Posted: Mon Jul 12, 2010 1:15 pm
by johnny2005uk
Thanks... I had a go at that too and it still seems to return the same message.

Should I be attacking this from a different direction? I couldn't think of any other way to pull the email address from the database and pass it to the process form dynamically.

Re: Dynamically call data from db and passing into process f

Posted: Mon Jul 12, 2010 2:03 pm
by Jade
You have another error in your first SQL statement.

Code: Select all

<?php
mysql_connect("localhost", "username", "password");
mysql_select_db ("db_name");
$genmail = mysql_query("SELECT Email FROM tablename WHERE ID='6' ");
$row = mysql_fetch_array($genmail); //your query is returned as a result resource that needs to be changed into an array or object so you can access the values inside
$email = $row['email']; //now we have an array of all the query results that we can pull directly from or iterate through
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

echo "<form action='processform.php' method='POST'>
<input type='hidden' name='email' value='$email'>
<input type='submit' value='Next Page'>
<input name='good_url' type='hidden' id='good_url' value='thanks.php' />
</form>\n";

?>
</body>
</html>
 


Re: Dynamically call data from db and passing into process f

Posted: Mon Jul 12, 2010 2:14 pm
by johnny2005uk
Thank you Jade. I've just had a quick play around with that and it works a treat. No doubt the setup of the syntax you've shown me will help on other things too!