Forum Moderators: coopster

Message Too Old, No Replies

Form to Email code problems

form to email

         

zorro

1:26 am on Sep 18, 2010 (gmt 0)

10+ Year Member



Please could some php expert look at this code and let me know where I have gone wrong? Been at it for 2 days but can't achieve what I want!

The form part is ok and displays all items in database for a certain user. Even the form send function works and the returning thankyou page.

THE PROBLEM IS...
On clicking the 'form send button' a seperate email is sent to the user and me (as I get a copy) for every item the user has in the database.
Also a thank you is displayed for every item after sending.

Basically I want to recall all items a user has in the database on the form (this bit ok) then when clicking the send button only 1 email is sent to user and 1 email sent to me with a list of all the items.
Same on thank you page - One thank you page showing list of items.

The resulting Email/thank you page should look something along the lines of:
TITLE 1 - TITLE 2 - TITLE 3 - TITLE 4 - TITLE 5
first 1 - first 2 - first 3 - first 4 - first 5
secnd 1 - secnd 2 - secnd 3 - secnd 4 - secnd 5

HERE IS THE CODE:
<table><tr>
<td>TITLE 1</td> <td>TITLE 2</td> <td>TITLE 3</td> <td>TITLE 4</td> <td>TITLE 5</td>
</tr>

<?php
$sql = "SELECT * FROM ".$TABLES["table1"]." WHERE user_id='".$_SESSION["useraccount"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

while ($TABLE1 = mysql_fetch_assoc($sql_result)) {

$sql = "SELECT * FROM ".$TABLES["table2"]." WHERE id='".$TABLE1["user_id"]."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

$USER=mysql_fetch_assoc($sql_resultT);

?>

<?

if (isset($action)) {


$to = $USER["email"];
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg =
"
Dear ".stripslashes($USER["name"])."<br><br>

some text here

TITLE1: ".$TABLE1["name"].", - TITLE2. ".$TABLE1["something"]." etc etc for TITLE 3 4 5<br><br>

";

mail($to, "email subject", $msg, $mailheader) or die ("Failure");
mail("to-me@my-email.com", "email subject", $msg, $mailheader) or die ("Failure");

echo
"<br /><br /><font size=2><strong><center>Thank you!</center><br />
Your form has been sent:</center><br />
";

};

?>

<form action="page_this-code_is_on" method="post">
<input type="hidden" name="action" value="0">
<input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">


<tr>
<td><?php echo stripslashes($TABLE1["TITLE1"]); ?></td>
<td><?php echo stripslashes($TABLE1["TITLE2"]); ?></td>
<td><?php echo stripslashes($TABLE1["TITLE3"]); ?></td>
<td><?php echo stripslashes($TABLE1["TITLE4"]); ?></td>
<td><?php echo stripslashes($TABLE1["TITLE5"]); ?></td>
</tr>

<?php } ?>
</tr>
</table>
<table width="100%"><tr>
<td align="center" valign="top">&nbsp;<input type="submit" name="Submit" value="Send"></td>
</tr><tr><td></td></tr></table>

</form>
</table>

I HAVE TRIED CHANGING THE 'while' STATEMENT TO 'if' IN THE ORIGINAL CALL TO THE DATABASE BUT THIS ONLY RETURNS ONE ITEM RESULT.
REMOVE/ADDED BITS OF CODE CURLY BRACES ETC ETC - PUZZLED ?

enigma1

10:00 am on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to move the mail lines outside the while loop. And you need to fix the forms because the while loop encloses some fields of the form and then closes. So you end up with the html broken pretty much.

So move the while loop closing bracket up before the if statement. Use arrays to collect all rows from the DB and finally send a single mail to the user.

$data_array = array();
while ($TABLE1 = mysql_fetch_assoc($sql_result)) {
.......
$data_array[] = mysql_fetch_assoc($sql_resultT);
}

Also from your code seems like a single row holds the data for the user not multiple ones. If it's really the case, you only need to do a single fetch.

See the mysql_num_rows function which should be used before the row fetch.

zorro

2:50 pm on Sep 18, 2010 (gmt 0)

10+ Year Member



Thanks ever so much for your reply enigma1 but I am banging my head on the wall with this one! I know little about php (still learning), you would believe how long it took me to get this far!
I'm not sure where to put the code?

I moved the while loop closing bracket up before the if statement but don't know what to do with the data array?

So far the call to the database looks like this...

<?php
$sql = "SELECT * FROM ".$TABLES["table1"]." WHERE user_id='".$_SESSION["useraccount"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

$data_array = array(Title1,Title2,Title3,Title4,Title5);
while ($TABLE1 = mysql_fetch_assoc($sql_result)) {

$sql = "SELECT * FROM ".$TABLES["table2"]." WHERE id='".$TABLE1["user_id"]."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

$data_array[]=mysql_fetch_assoc($sql_resultT);

?>

enigma1

3:57 pm on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The data_array variable holds the results of your $TABLES query so just initialize it as empty.

$data_array = array();
while ($TABLE1 = mysql_fetch_assoc($sql_result)) {

$sql = "SELECT * FROM ".$TABLES["table2"]." WHERE id='".$TABLE1["user_id"]."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$data_array[]=mysql_fetch_assoc($sql_resultT);
.............
} // closing the while loop

then you process the data array to construct the body of the email
so after you setup the initial text in the $msg variable you could do:

for($i=0, $j=count($data_array); $i<$j; $i++ ) {
$msg .= $data_array[$i]['field1'];
$msg .= $data_array[$i]['field2'];
.....etc
}

where field1, field2 etc are the columns of the table you called using $TABLES["table2"] as I don't know what fields you have. In the end, you send the mail as you did:

mail($to, "email subject", $msg, $mailheader) or die ("Failure");
mail("to-me@my-email.com", "email subject", $msg, $mailheader) or die ("Failure");

and the $msg will contain the data of the fields you want to sent.

And move the following code:

<table><tr>
<td>TITLE 1</td> <td>TITLE 2</td> <td>TITLE 3</td> <td>TITLE 4</td> <td>TITLE 5</td>
</tr>

right after the mail is sent it's easier to read the code (right before the <form> tag

zorro

1:31 am on Sep 19, 2010 (gmt 0)

10+ Year Member



Hi enigma and thanks again for all your help but it just isn't working!
I read through your last piece several times and implemented it to the word but still no joy.

CODE IS AS FOLLOWS:(simplified)
<?php
$sql = "SELECT * FROM ".$TABLES["one"]." WHERE user_id='".$_SESSION["useraccount"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

$data_array = array();
while ($ONE = mysql_fetch_assoc($sql_result)) {

$sql = "SELECT * FROM ".$TABLES["two"]." WHERE id='".$ONE["user_id"]."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$data_array[]=mysql_fetch_assoc($sql_resultT);

} // closing the while loop

?>

<?

if (isset($action)) {

$to = $data_array["email"]; // GET FAILURE IF THIS LINE LEFT IN // WAS PREVIOUSLY $to = $USER["email"]; // NEED EMAIL FROM TABLE TWO

$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg =
"
Dear

";
for($i=0, $j=count($data_array); $i<$j; $i++ ) {
$msg .= $data_array[$i]['name']; //THIS OK
$msg .= $data_array[$i]['ref']; //THIS IS NOT PULLING REF FROM TABLE ONE BUT SHOWING name FROM TABLE TWO
}

mail($to, "subject", $msg, $mailheader) or die ("Failure"); // WON'T WORK WITH THIS LINE IN
mail("me@my-email.co.uk", "email subject", $msg, $mailheader) or die ("Failure");

echo
"<br /><br /><font size=2><strong><center>Thank you!</center><br />
Your form has been sent:</center><br />
";

};

?>

<table><tr>
<td>TITLE 1</td> <td>TITLE 2</td> <td>TITLE 3</td> <td>TITLE 4</td> <td>TITLE 5</td>
</tr>

<form action="page code is on" method="post">
<input type="hidden" name="action" value="0">
<input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">

// NOTHING BEEN SHOWN ON FORM PAGE
<tr>
<td><?php echo stripslashes($TWO["name"]); ?></td> //NEED NAME FROM TABLE TWO
<td><?php echo stripslashes($ONE["ref"]); ?></td> // NEED REF No FROM TABLE ONE
<td><?php echo stripslashes($ONE["expiry"]); ?></td> // NEED EXPIRY DATE FROM TABLE ONE
<td><?php echo stripslashes($ONE["type"]); ?></td> // NEED TYPE FROM TABLE ONE
<td><?php echo stripslashes($ONE["plan"]); ?></td> // NEED PLAN FROM TABLE ONE
</tr>


</tr>
</table>
<table width="100%"><tr>
<td align="center" valign="top">&nbsp;<input type="submit" name="Submit" value="Send"></td>
</tr><tr><td></td></tr></table>

</form>
</table>

Just to reiterate:
I have a form (sort of) on a page which is accessed when a user logs into their account.
The form dynamically pulls bits of the users info from 2 tables in the database, Tables ONE and TWO.
Table TWO holds the users info while Table ONE may hold several items for the user.
When the form page is accessed all the users items from Table ONE are dynamically shown on the form with several checkboxes at the side of each item.
The user can tick certain boxes at the side of each item and click the send button.
TRYING TO ACHIEVE...
After form is sent a thank you message appears on same page with details of the items and what checkboxes were ticked.
Email is sent to user with same info of the items and what checkboxes were ticked.
Email is also sent to me with same info of the items and what checkboxes were ticked.
I need to access Table ONE for the items and Table TWO for users name and email address!

I really appreciate your help but trying your method I could only get one, i.e the form, or the other, i.e the email, to work, even then it was only an email that was physically entered i.e me@my-email.com that would work it would not work with the $to email.

please help it's taking over my life!

enigma1

11:03 am on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand the problem now is with the mail sent. So now you need to setup the mail to be sent to each customer. So the 2nd loop could be used to send the mail retrieving at the same time the mail from the table2. Also the msg variable holds the current body of the message you need to reset it before sending the next mail.

// Check if its time to sent mail
if (isset($action)) {
// Initialize mail headers
// From the code I can't see know where the $email var was initialized. Assume it's ok.
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
// Data loop
for($i=0, $j=count($data_array); $i<$j; $i++ ) {
// I assume the email field is held in table-2
$to = $data_array[$i]["email"];
$msg =
"
Dear

";
// $data_array holds the fields of table2 only. Where the ref field comes from? If its from table-1 then $ONE has to be multi-dimensional array like the data_array and/or merge the 2 arrays
$msg .= $data_array[$i]['name'];
$msg .= $data_array[$i]['ref'];

mail($to, "subject", $msg, $mailheader) or die ("Failure");
mail("me@my-email.co.uk", "email subject", $msg, $mailheader) or die ("Failure");

} // End of for loop

} // End of if condition

zorro

12:46 am on Sep 20, 2010 (gmt 0)

10+ Year Member



Hi enigma, again thanks for all you help I think I may have to outsource this to someone else as I can not get it working.

I am not getting anything showing in the form at all now, presumably because the stuff I want in the form is in table ONE and the array is only looking at table TWO? Same goes for:
$msg .= $data_array[$i]['ref'];
as this is also in table ONE - This is where multi-dimensional array would come in I expect but this is way above my head!

Using the original code i posted still gives a better output than what I can get at present. At least the old code showed:
All user items on form
All user items on returned thank you page
The only fault was that it would send the user and me a seperate email for each item the user had (which, all i wanted was to include in one email).
If user had 4 items in database they would get 4 emails - would have preferred 1 email detailing the 4 items.

I can't spend anymore time on this as it's my 4th day and it has worn me out and I'm totally fed up with it. Thanks for all your effort though it is much appreciated!

This was the final code:

<?php
$sql = "SELECT * FROM ".$TABLES["items"]." WHERE user_id='".$_SESSION["UserAccount"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

$data_array = array();
while ($ITEMS = mysql_fetch_assoc($sql_result)) {

$sql = "SELECT * FROM ".$TABLES["users"]." WHERE id='".$ITEMS["user_id"]."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$data_array[]=mysql_fetch_assoc($sql_resultT);

} // closing the while loop

?>


<?

if (isset($action)) {


for($i=0, $j=count($data_array); $i<$j; $i++ ) {
$to = $data_array[$i]["email"];
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg =
"
Dear

";

$msg .= $data_array[$i]['name'];
$msg .= $data_array[$i]['ref']; //NOT WORKING
}

mail($to, "subject", $msg, $mailheader) or die ("Failure");
mail("me@me.co.uk", "subject", $msg, $mailheader) or die ("Failure");

echo
"<br /><br /><font size=2><strong><center>Thank you!</center><br />
Your form has been sent:</center><br />
";

}

?>

<table><tr>
<td>TITLE 1</td> <td>TITLE 2</td> <td>TITLE 3</td> <td>TITLE 4</td> <td>TITLE 5</td>
</tr>

//NOT SHOWING
<form action="this-page" method="post">
<input type="hidden" name="action" value="0">
<input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">


<tr>
<td>$data_array['name']; </td>
<td>$data_array['ref']; </td>
<td>$data_array["expiry"]; </td>
<td>$data_array["type"]; </td>
<td>$data_array["plan"]; </td>
</tr>



</tr>
</table>
<table width="100%"><tr>
<td align="center" valign="top">&nbsp;<input type="submit" name="Submit" value="Send"></td>
</tr><tr><td></td></tr></table>


</form>
</table>

enigma1

9:07 am on Sep 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The form is not showing because it needs to be within an inline element.

<table><tr>
<td>TITLE 1</td> <td>TITLE 2</td> <td>TITLE 3</td> <td>TITLE 4</td> <td>TITLE 5</td>
</tr>
<tr>
<td colspan="5">
<form action="this-page" method="post">
<input type="hidden" name="action" value="0">
<input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>">
</td>
</tr>
..................
and ideally the form should open close within the table definition.

For the database tables can you post the structure of each so we can see where each column belongs to?

zorro

3:52 pm on Sep 21, 2010 (gmt 0)

10+ Year Member



MANY THANKS NOW SOLVED!

Matthew1980

6:17 pm on Sep 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Sorry to butt in at this point but I think as it is worth the mention since I seem to be advocating this quite a lot recently;

Case in point

<? ?> This isn't a preferred method of declaring a php coded document

<?php ?> This IS the preferred method of declaring a php document.

Although servers have the OPTION of having the short tags enabled, this is a server OPTION, so say you come to change servers one day (migrate) and the new host's don't have it enabled, this will STOP the script dead in it's tracks.

The latter of my examples is the best way to declare your document, as the parser reads the instruction as full tags REGARDLESS of what's set in the options on the server, for the sake of 3 extra chars this will and can save you headaches in the long run.

Anyway, glad your problem is sorted out now - I just thought as this was worth pointing out.

Cheers,
MRb

System

3:13 pm on Sep 25, 2010 (gmt 0)

redhat



The following 2 messages were cut out to new thread by coopster. New thread at: php/4211551.htm [webmasterworld.com]
5:04 am on Oct 5, 2010 (utc -6)