Click to See Complete Forum and Search --> : Form to submit data and images?


Sky Captain
10-19-2004, 12:10 PM
Hey there. Some time ago I started a thread over in the Javascript forum looking for a way for visitors to my website to send me data and images using an email form. I got some great PHP info from Pittiman:

http://www.webdeveloper.com/forum/showthread.php?threadid=29742&perpage=15&pagenumber=1

I've got new problems, but rather than add to that thread, I'm starting a new one here in the appropriate forum.

So now I'm getting around to putting this site online. The images come through fine on email. But I want a whole bunch of text, too. Name, address, phone, email, etc.

But when I receive test emails, they contain the image attachment and the "message" text area, nothing else. How do I get the other text areas to show up? I've tried altering the PHP code, but it never comes out the way I like.


My email form (HTML) looks like this:



<form action="buycustomcode.php" method="POST" enctype="multipart/form-data" name="imgForm" onSubmit="return formCheck()">

<input type="hidden" name="subject" value="CUSTOM CARD ORDER with IMAGE">

<p>
<h4>Contact Information</h4>

<P>

<pre>
First Name <INPUT NAME="FIRST_NAME" value="" SIZE="15" TYPE="text"> Last Name <INPUT NAME="LAST_NAME" value="" SIZE="15" TYPE="text">
Address <INPUT NAME="ADDRESS" value="" SIZE="40" TYPE="text">
City <INPUT NAME="CITY" value="" SIZE="15" TYPE="text"> State <INPUT NAME="STATE" value="" SIZE="2" TYPE="text"> Zip Code <INPUT NAME="ZIP" value="" SIZE="5" TYPE="text">
Phone Number (Optional) <INPUT NAME="PHONE" value="" SIZE="15" TYPE="text">
Email (Mandatory) <input type="text" name="EMAILaddress" value="" SIZE="40">
</pre>


<FONT SIZE="-1" FACE="VERDANA, ARIAL, HELVETICA">



<P>
Please pick out the type of card for your template.

<P>
<table width=100%>
<tr>
<td align=center>
<img src="images/customezX250-143.jpg" width=250 height=143 border=1 alt="EZ X"><br>Choose EZ X here: <input type="radio" name="custom_choice" value="EZ X" CHECKED>
</td>
<td align=center>
<img src="images/customY250-143.jpg" width=250 height=143 border=1 alt="Y"><br>Choose Y here: <input type="radio" name="custom_choice" value="Y">
</td>
</tr>
<tr><td colspan=2> </td></tr>
<tr>
<td align=center>
<img src="images/customezZ250-143.jpg" width=250 height=143 border=1 alt="EZ Z"><br>Choose EZ Z here: <input type="radio" name="custom_choice" value="EZ Z">
</td>
<td align=center>
<img src="images/customZZ250-143.jpg" width=250 height=143 border=1 alt="ZZ"><br>Choose ZZ here: <input type="radio" name="custom_choice" value="ZZ">
</td>
</tr>
</table>



<P>
Provide your personalized text here:

<P ALIGN=CENTER>
<textarea cols="50" rows="5" name="message"></textarea><br>
</p>


Please select an image from your hard disk:<br>

<P ALIGN=CENTER>
<input type="file" name="attachedImg"><br><br>


<p align=center>
<input type="submit">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset">
</p>

</form>



It then forwards the user to the PHP code page (buycustomcode.php) here:



<?php
$to= 'test@test.com';//your email address has to go here and
$from= 'test@test.com';//here (second time to be sure, that mail will arrive
$maxSize=102400;//allowed file size in bytes
/*If you want to add more filetypes here, you will need to know the mime type; see this sample for a few types:
$extArray = array('image/gif','image/bmp','image/pjpeg','image/x-png');*/
$extArray = array('image/pjpeg');//valid type now
$from2= $_POST['from2'];
$subject = $_POST['subject'];
$message = stripslashes($_POST['message']);
$attachedImg= $_FILES['attachedImg']['tmp_name'];
$attachedImg_type = $_FILES['attachedImg']['type'];
$attachedImg_name = $_FILES['attachedImg']['name'];
if (filesize($attachedImg)>$maxSize){
echo 'The file is bigger than allowed: '.filesize($attachedImg). ' bytes;<br>';
echo 'The allowed maximum size is: '.$maxSize. ' bytes.<br>';
echo 'Click <a href="buycustom4.html" title="&nbsp;try again&nbsp;">here</a> to get back to the form...';
}
if ($attachedImg_type!='image/x-png'&&!in_array($attachedImg_type,$extArray)){
echo $attachedImg_type.' is not permitted!<br>';
echo 'Click <a href="buycustom4.html" title="&nbsp;try again&nbsp;">here</a> to get back to the form...';
}
if (filesize($attachedImg)<=$maxSize&&in_array($attachedImg_type,$extArray)){
$headers = "From: $from";
if (is_uploaded_file($attachedImg)) {
$file = fopen($attachedImg,'rb');
$data = fread($file,filesize($attachedImg));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message2 = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message2 . $message."\n\n";
$data = chunk_split(base64_encode($data));
$message2 .= "--{$mime_boundary}\n" .
"Content-Type: {$attachedImg_type};\n" .
" name=\"{$attachedImg_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n" .
"--{$mime_boundary}--\n";
}
$ok = @mail($to, $subject, $message2, $headers);
if ($ok) {
echo "<p>Order sent successfully! Please wait for Paypal instructions...</p><br>";
}
else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
}
?>



Any help would be appreciated!

dreamcatcher
10-19-2004, 12:34 PM
It doesnt look like you are passing any form values to your PHP script. ie: FIRST_NAME, LAST_NAME, ADDRESS etc

I only see the message and subject fields actually passing data. Or are my eyes playing tricks on me?

Sky Captain
10-19-2004, 12:51 PM
Yes, I think you're right. That's why I need help.

Pretend I know nothing about PHP (which is almost true). Where do I put that info in the code?

I'm not sure why "message2" works in the PHP when I don't have a corresponding "message2" in the HTML. Just "message". But I guess that's where I'd put "first_name" and "last_name" etc. Would those be message 3, 4, 5...?

dreamcatcher
10-19-2004, 01:05 PM
message2 works because you have created a variable called 'message2'. This info is not passed from your form.

I`ll just use the first few fields as an example.

When your form is processed, it passes the data to the PHP page. To access these variables you should use the $_POST superglobal array. I see you have used this in your code already.

You can assign variables. ie:

$first_name = $_POST['FIRST_NAME'];
$last_name = $_POST['LAST_NAME'];
$address = $_POST['ADDRESS'];

which you would use as $first_name etc

or you can just use $_POST['FIRST_NAME'] etc

These go in your PHP code. You need to use these variables to build a message string for your message. Something like:

$message = "First Name:" . $first_name . "\n";
$message .= "Last Name:" . $last_name . "\n";
$message .= "Address:" . $address . "\n";

Note the use of the . which concatenates (joins together) the string. You can add as many lines as you wish. I see you are already familiar with this.

You need to do this with all your variables in your form. Your code is set, all you need is your form variables and you are good to go.

Hope that gets you started. :)

Sky Captain
10-19-2004, 02:54 PM
Thanks a ton! I'll be working on this tonight. I'll post back tomorrow with my status...

Sky Captain
10-19-2004, 04:06 PM
Well, I tried it. Still having problems. I probably didn't put things where they should go... I'm not making any changes to my HTML page, right?

Okay, here are my new lines of code for the PHP file:

$first_name = $_POST['FIRST_NAME'];
$last_name = $_POST['LAST_NAME'];
$address = $_POST['ADDRESS'];
$city = $_POST['CITY'];
$state = $_POST['STATE'];
$zip = $_POST['ZIP'];
$phone = $_POST['PHONE'];
$emailaddress = $_POST['EMAILaddress'];
$custom_choice = $_POST['custom_choice'];


$message = "First Name:" . $first_name . "\n";
$message .= "Last Name:" . $last_name . "\n";
$message .= "Address:" . $address . "\n";
$message .= "City:" . $city . "\n";
$message .= "State:" . $state . "\n";
$message .= "Zip:" . $zip . "\n";
$message .= "Phone:" . $phone . "\n";
$message .= "Email:" . $emailaddress . "\n";
$message .= "Custom Choice:" . $custom_choice . "\n";


I put the first batch near the top and the second batch near the bottom. However, I fear I slugged things into the wrong place. Do I need to worry about being case sensitive?

Here's the whole PHP file, with my awkward changes:


<?php
$to= 'test@test.com';//your email address has to go here and
$from= 'test@test.com';//here (second time to be sure, that mail will arrive
$maxSize=102400;//allowed file size in bytes
/*If you want to add more filetypes here, you will need to know the mime type; see this sample for a few types:
$extArray = array('image/gif','image/bmp','image/pjpeg','image/x-png');*/
$extArray = array('image/pjpeg');//valid type now
$from2= $_POST['from2'];
$subject = $_POST['subject'];
$first_name = $_POST['FIRST_NAME'];
$last_name = $_POST['LAST_NAME'];
$address = $_POST['ADDRESS'];
$city = $_POST['CITY'];
$state = $_POST['STATE'];
$zip = $_POST['ZIP'];
$phone = $_POST['PHONE'];
$emailaddress = $_POST['EMAILaddress'];
$custom_choice = $_POST['custom_choice'];
$message = stripslashes($_POST['message']);
$attachedImg= $_FILES['attachedImg']['tmp_name'];
$attachedImg_type = $_FILES['attachedImg']['type'];
$attachedImg_name = $_FILES['attachedImg']['name'];
if (filesize($attachedImg)>$maxSize){
echo 'The file is bigger than allowed: '.filesize($attachedImg). ' bytes;<br>';
echo 'The allowed maximum size is: '.$maxSize. ' bytes.<br>';
echo 'Click <a href="buycustom4.html" title="&nbsp;try again&nbsp;">here</a> to get back to the form...';
}
if ($attachedImg_type!='image/x-png'&&!in_array($attachedImg_type,$extArray)){
echo $attachedImg_type.' is not permitted!<br>';
echo 'Click <a href="buycustom4.html" title="&nbsp;try again&nbsp;">here</a> to get back to the form...';
}
if (filesize($attachedImg)<=$maxSize&&in_array($attachedImg_type,$extArray)){
$headers = "From: $from";
if (is_uploaded_file($attachedImg)) {
$file = fopen($attachedImg,'rb');
$data = fread($file,filesize($attachedImg));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message2 = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message2 . $message."\n\n";
$data = chunk_split(base64_encode($data));
$message2 .= "--{$mime_boundary}\n" .
"Content-Type: {$attachedImg_type};\n" .
" name=\"{$attachedImg_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$message = "First Name:" . $first_name . "\n";
$message .= "Last Name:" . $last_name . "\n";
$message .= "Address:" . $address . "\n";
$message .= "City:" . $city . "\n";
$message .= "State:" . $state . "\n";
$message .= "Zip:" . $zip . "\n";
$message .= "Phone:" . $phone . "\n";
$message .= "Email:" . $emailaddress . "\n";
$message .= "Custom Choice:" . $custom_choice . "\n";
}
$ok = @mail($to, $subject, $message2, $headers);
if ($ok) {
echo "<p>Order sent successfully! Please wait for Paypal instructions...</p><br>";
}
else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
}
?>

dreamcatcher
10-19-2004, 06:22 PM
The variable that is being sent to your message body is $message2. So, your $message variables have no functionality.

What I would suggest is you lose the following:

$message2 = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message2 . $message."\n\n";
$data = chunk_split(base64_encode($data));
$message2 .= "--{$mime_boundary}\n" .
"Content-Type: {$attachedImg_type};\n" .
" name=\"{$attachedImg_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

Then change your mail function message variable to just $message. See what happens. The above is header information and does not need to be in the message body.

Sky Captain
10-19-2004, 09:39 PM
The above is header information and does not need to be in the message body.

So I don't need that chunk at all?

Then change your mail function message variable to just $message. See what happens. [/B]

So I should change this:

$message = stripslashes($_POST['message']);

to this?

$message = $_POST['message'];

dreamcatcher
10-20-2004, 03:54 AM
Some of the stuff you have for the $message2 variable is header data and should be in the $header variable. Your stripslashes function is fine.

Try changing:


$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message2 = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message2 . $message."\n\n";
$data = chunk_split(base64_encode($data));
$message2 .= "--{$mime_boundary}\n" .
"Content-Type: {$attachedImg_type};\n" .
" name=\"{$attachedImg_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$message = "First Name:" . $first_name . "\n";
$message .= "Last Name:" . $last_name . "\n";
$message .= "Address:" . $address . "\n";
$message .= "City:" . $city . "\n";
$message .= "State:" . $state . "\n";
$message .= "Zip:" . $zip . "\n";
$message .= "Phone:" . $phone . "\n";
$message .= "Email:" . $emailaddress . "\n";
$message .= "Custom Choice:" . $custom_choice . "\n";
}
$ok = @mail($to, $subject, $message2, $headers);


to this:


$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$headers .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n";
$headers .= chunk_split(base64_encode($data));
$headers .= "--{$mime_boundary}\n" .
"Content-Type: {$attachedImg_type};\n" .
" name=\"{$attachedImg_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= "\n\n" .
"--{$mime_boundary}--\n";
$message = "First Name:" . $first_name . "\n";
$message .= "Last Name:" . $last_name . "\n";
$message .= "Address:" . $address . "\n";
$message .= "City:" . $city . "\n";
$message .= "State:" . $state . "\n";
$message .= "Zip:" . $zip . "\n";
$message .= "Phone:" . $phone . "\n";
$message .= "Email:" . $emailaddress . "\n";
$message .= "Custom Choice:" . $custom_choice . "\n";
}
$ok = @mail($to, $subject, $message, $headers);


See if that works. Make sure you make a copy of your original file just in case.

Sky Captain
10-27-2004, 12:49 PM
I stepped away from this problem for a few days, and now I'm back on it.

Ouch. That substitution didn't work for me. I ended up with gobbledygook emails, like the JPG file had been broken down into characters. But I also had the image attached, unreadable by my image viewer.

I'm thinking of going back to the beginning and starting from the ground up.