Forum Moderators: coopster

Message Too Old, No Replies

PHP Form Email Handler Problem

I want to: add to database, upload file 2 server, & send email notification

         

alcheme

1:32 am on Sep 17, 2004 (gmt 0)

10+ Year Member



Hello,

I have a website where I have a submission form that enters the form fields into my mySQL database and uploads a file to the server.

I also want it to send an email notification to myself when the form is submitted and files are uploaded.

Any Suggestions?

Here are the FORM & PHP scripts for both pages.

>>>> submissions.php <<<<

<form action="../../uploads/upload.php" method="post" enctype="multipart/form-data" name="contribute_mail">
<table width="150" border="0" cellspacing="0" cellpadding="2">
<tr>
<td colspan="2" class="Title">&nbsp;</td>
<td colspan="2" class="Title">&nbsp;</td>
</tr>
<tr class="subheading1">
<td colspan="2" class="subheading3">First Name</td>
<td colspan="2" class="subheading3">Last Name</td>
</tr>
<tr>
<td width="5%">&nbsp;</td>
<td><input name="firstname" type="text" id="firstname" size="20"></td>
<td>&nbsp;</td>
<td><input name="lastname" type="text" id="lastname" size="20"></td>
</tr>
<tr class="subheading1">
<td colspan="2"><span class="subheading3">Street Address</span></td>
<td colspan="2" class="subheading3">City</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="address" type="text" id="address" size="20" maxlength="90"></td>
<td>&nbsp;</td>
<td><input name="city" type="text" id="city" size="20"></td>
</tr>
<tr class="subheading1">
<td colspan="2" class="subheading3">Province</td>
<td colspan="2" class="subheading3">Postal Code</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="33%"><select name="province" id="select">
<option value="AB">Alberta</option>
<option value="BC" selected>British Columbia</option>
<option value="MB">Manitoba</option>
<option value="NB">New Brunswick</option>
<option value="NF">New Foundland</option>
<option value="NT">Northwest Territories</option>
<option value="NS">Nova Scotia</option>
<option value="NV">Nunavut</option>
<option value="ON">Ontario</option>
<option value="PI">Prince Edward Island</option>
<option value="PQ">Quebec</option>
<option value="SA">Saskatchewan</option>
<option value="YT">Yukon Territory</option>
</select></td>
<td width="4%">&nbsp;</td>
<td width="58%"><input name="postal" type="text" id="postal" size="20"></td>
</tr>
<tr class="subheading1">
<td colspan="2"><span class="subheading3">Phone Number</span></td>
<td colspan="2" class="subheading3">Email Address</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="phone" type="text" id="phone" size="17"></td>
<td>&nbsp;</td>
<td><input name="email" type="text" id="email" size="20" maxlength="90"></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td><span class="subheading3">Section </span></td>
<td colspan="3"><p class="Body">(where you would like this submission featured)</p></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3"><select name="section" id="section">
<option selected>-- select one --</option>
<option>News</option>
<option>Editorial</option>
<option>Features</option>
<option>Arts &amp; Entertainment</option>
<option>Sports</option>
<option>Comics</option>
</select></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4"><p><span class="subheading3">Currently a student at Malaspina?</span><span class="Body"> (not necesary for publication)</span></p></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3"><table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="25"><input type="radio" name="malastudent" value="Yes"></td>
<td width="45">Yes</td>
<td width="40">&nbsp;</td>
<td width="28"><input name="malastudent" type="radio" value="No" checked></td>
<td width="62">No</td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4"><p><span class="subheading3">Attach File </span><span class="Body">(Please send your work as a word document, RTF file, JPG, or PDF.)</span></p></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3"><input name="docfile" type="file" id="docfile"></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4" class="subheading3">Comments</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3"><textarea name="comments" cols="40" rows="4" id="Comments"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr align="center">
<td colspan="4"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>

>>>> upload.php <<<<

<?php

if ($docfile_name!= "") {
$docfileloc = "/home/*********/public_html/uploads/$docfile_name";
@copy("$docfile" , "$docfileloc")

or die("Couldn't Upload Your File.");

} else {

die("No File Specified");

}

$db=mysql_connect ("localhost", "DB_USER", "*******") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("DB_*********");

$datestamp = date("Y-m-d H:i:s");

$newrec_check_sql = "SELECT * FROM contacts WHERE email = '$email'";

$newrec_check_count = mysql_num_rows(mysql_query($newrec_check_sql));

if($newrec_check_count == 0) {
$sql = "INSERT INTO contacts (firstname, lastname, address, city, province, postal, email, phone, section, malastudent, comments) VALUES ('$firstname', '$lastname', '$address', '$city', '$province', '$postal', '$email', '$phone', '$section', '$malastudent', '$comments')";

$result = mysql_query($sql);
}

$sql = "INSERT INTO contributors (firstname, lastname, address, city, province, postal, email, phone, section, malastudent, filename, comments, datestamp) VALUES ('$firstname', '$lastname', '$address', '$city', '$province', '$postal', '$email', '$phone', '$section', '$malastudent', '$docfile_name', '$comments', '$datestamp')";

$result = mysql_query($sql);

?>

[edited by: jatar_k at 5:17 am (utc) on Sep. 17, 2004]
[edit reason] removed url [/edit]

mincklerstraat

6:32 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sounds like what you need is best answered here: [be2.php.net...] , on the mail() function, it's really easy to use. Why don't you check that out, try it on for size, and then come back and repost about whether it was an edifying experience. (or whether it worked at all).

alcheme

4:35 am on Sep 18, 2004 (gmt 0)

10+ Year Member



Well I have the mail function working. THANKS!

[ca3.php.net...]

BUT NOW the files won't upload:

I get the error "Couldn't Upload Your File."

But I can upload .jpg files but no others.

ANY IDEAS?

>>>>> UPLOAD.PHP <<<<<

<?php

if ($docfile_name!= "") {
$docfileloc = "/home/*******/public_html/uploads/$docfile_name";
@copy("$docfile" , "$docfileloc")

or die("Couldn't Upload Your File.");

} else {

die("No File Specified");

}

$db=mysql_connect ("localhost", "******_******", "*****") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("*******");

$datestamp = date("Y-m-d H:i:s");

$newrec_check_sql = "SELECT * FROM contacts WHERE email = '$email'";

$newrec_check_count = mysql_num_rows(mysql_query($newrec_check_sql));

if($newrec_check_count == 0) {
$sql = "INSERT INTO contacts (firstname, lastname, address, city, province, postal, email, phone, section, malastudent, comments) VALUES ('$firstname', '$lastname', '$address', '$city', '$province', '$postal', '$email', '$phone', '$section', '$malastudent', '$comments')";

$result = mysql_query($sql);
}

$sql = "INSERT INTO contributors (firstname, lastname, address, city, province, postal, email, phone, section, malastudent, filename, comments, datestamp) VALUES ('$firstname', '$lastname', '$address', '$city', '$province', '$postal', '$email', '$phone', '$section', '$malastudent', '$docfile_name', '$comments', '$datestamp')";

$result = mysql_query($sql);

?>

<?php

$sql_id = "SELECT * FROM contributors WHERE datestamp='$datestamp'";

$result_id = mysql_query($sql_id);

$myrow = mysql_fetch_array($result_id);

$id=$myrow["id"];

$fileurl = str_replace(' ','%20',$docfile_name);

$message="Dear Managing Editor, a contribution was received on the ********* website.

Name : $firstname $lastname
Phone # : $phone
Email : $email

To view $firstname's info and download the uploaded file click here ->
[*********.ca...]

In the future, you can view all of the contributors that are
in the database at : [******.ca...]

Have a great day and don't forget to send an email to this contributor
to let them know a real person has received their submission.

--
Navigator WebRobot

";

$to = "Managing Editor <****@*****.ca>". ", " ; // note the comma
$to .= "Web Editor <****@****.ca>";

//$to = "Web Monkey <*****@*******.ca>";

$subject = "********* Contributor Submission Alert System 9000 XTZ: (";
$subject .= $docfile_name;
$subject .= ")";

/* additional headers */
$headers .= "From: Contributor Submissions <****@*******.ca>\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);

?>

[edited by: alcheme at 5:20 am (utc) on Sep. 18, 2004]

alcheme

5:10 am on Sep 18, 2004 (gmt 0)

10+ Year Member



It looks like I solved my own question.

I only had to CHMOD 777 the folder.

So all the above is the working script for submissions.php & uploads.php

LET ME KNOW IF THIS HELPS ANYONE :-)