I'm from a design background and only really have experience with HTML and CSS so I'm very new to anything involving PHP and can only grasp chopping and changing bits of code at best of times.
I have found some PHP code that allows me to create a web form that when used, includes the users name, email, email subject and email body - this has been tested and works just fine and I'm receiving emails from the form with no problems.
What I want to do now is have the form create/update an XML file that I have hosted with the users info every time someone uses the form.
I tried searching and couldn't really find anything that worked.
My PHP:
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
if(!$visitormail == "" && (!strstr($visitormail,"#") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}
$todayis = date("l, F j, Y, g:i a");
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = "
Subject: $attn \n
Message: $notes \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
";
$from = "From: $visitormail\r\n";
mail('youremail#website.com', $subject, $message, $from);
?>
I found this PHP and tried adjusting it to work with my code but it doesn't seem to work:
<?php
$file="test_xml.xml";
$visitor="Name";
$ip="IP";
//load xml object
$xml= simplexml_load_file($file);
//assign name
$xml->auth->ids = $visitor;
//assign ip
$xml->auth->key = $ip;
//store the value into the file
file_put_contents($file, $xml->asXML());
?>
Am I on the right track at all?
Just replace your XML code in your php with this one and try it out
$xml = new SimpleXMLElement('');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor',$Visitor);
$mydata->addChild('Key',$ip);
$mydata->PHP_EOL;
mysql_close($db);
$fp = fopen("VisitorData.xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
Related
I am a not very good with php forms and need help with the one I am working on. First and foremost I would like help with a simple way to ensure that all fields are present and valid. For the sake of space, I am using a form similar to this
link http://distancebrothers.com/request-quote.html
The php code is listed below:
<?php
.
.
.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$cmpny = $_POST['cmpny'];
$street = $_POST['street'];
$city_st_zip = $_POST['city_st_zip'];
$respond = $_POST['respond'];
$num_passengers = $_POST['num_passengers'];
$trip_type = $_POST['trip_type'];
$num_pickups = $_POST['num_pickups'];
$bus_size = $_POST['bus_size'];
$pickup_name = $_POST['pickup_name'];
$pickup_street_name = $_POST['pickup_street_name'];
$pickup_city_st_zip = $_POST['pickup_city_st_zip'];
$depart_date = $_POST['depart_date'];
$depart_time = $_POST['depart_time'];
$dropoff_name = $_POST['dropoff_name'];
$dropoff_street_name = $_POST['dropoff_street_name'];
$dropoff_city_st_zip = $_POST['dropoff_city_st_zip'];
$dropoff_date = $_POST['dropoff_date'];
$dropoff_time = $_POST['dropoff_time'];
$spec_instr = $_POST['spec_instr'];
$meet_greet = $_POST['meet_greet'];
$ada = $_POST['ada'];
$extra = $_POST['extra'];
$find_us = $_POST['find_us'];
$formcontent="<html> <body><b>Contact Information:</b> <br><br>Name: $name
<br>Email: $email <br>Phone: $phone <br>Company / Group Name: $cmpny
<br>Street Name: $street <br>City, State, Zip Code: $city_st_zip <br>Respond
Via: $respond <br><br><b>Event / Trip Details:</b> <br><br>Number of
Passengers: $num_passengers <br>Trip Type: $trip_type <br>Number of
Pickups: $num_pickups <br>Bus Size: $bus_size <br><br><b>Event / Trip
Times - Departure and Destination:<br>Pick up location</b> <br><br>Name:
$pickup_name <br>Street Name: $pickup_street_name <br>City, State, Zip
Code: $pickup_city_st_zip <br>Depart Date: $depart_date <br>Depart Time:
$depart_time <br><br><b>Drop Off Location</b><br><br>Name: $dropoff_name
<br>Street Name: $dropoff_street_name <br>City, State, Zip Code:
$dropoff_city_st_zip <br>Return Date: $dropoff_date <br>Return Time:
$dropoff_time <br><br><b>Contact Information:</b> <br><br>Itinerary /
Special Instructions / Additional Info:<br>$spec_instr <br>Airport Meet &
Greet: $meet_greet <br>ADA Accessible Coach: $ada <br>Extra Storage:
$extra <br>How did you find us? $find_us </body> </html>";
$recipient = "distancebrothers.com,jldouglas58#gmail.com";
$subject = "Request for a Quote";
$mailheader = "From: $email \r\n";
$mailheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header( 'Location: http://www.distancebrothers.com/quote-confirmation.html')
;?>
$valid = 1;
foreach($_POST as $key=>$value)
{
echo "$key=$value";
if ($value=="" || $value===null)
{ $valid = 0; }
}
You should probably trim the variables or white spaces, just in case a user tries to get around a required field. trim($value)
I would like to know how to pass form data from a php processing page to a success page.
How can I pass the $orderid to my success page? I only need to pass this one value so something simple would be great! :-P
<?php
$stamp = date("Ymdhis");
$random_id_length = 6;
$rndid = generateRandomString( $random_id_length );
$orderid = $stamp ."-". $rndid;
function generateRandomString($length = 10) {
$characters = '0123456789';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
$repairtitle = $_POST['courierrepairtitle'];
$repairconsole = $_POST['courierrepairconsole'];
$repairprice = $_POST['courierrepairprice'];
$outwardpostage = $_POST['outwardpostage'];
$returnpostage = $_POST['returnpostage'];
$name = $_POST['couriername'];
$email = $_POST['courieremail'];
$homephone = $_POST['courierhomephone'];
$mobilephone = $_POST['couriermobilephone'];
$address1 = $_POST['courieraddress1'];
$address2 = $_POST['courieraddress2'];
$address3 = $_POST['courieraddress3'];
$city = $_POST['couriercity'];
$county = $_POST['couriercounty'];
$postcode = $_POST['courierpostcode'];
$country = $_POST['couriercountry'];
$formcontent=" Order No: $orderid \n \n Repair Title: $repairtitle \n Console: $repairconsole \n Price: $repairprice \n \n Outward Postage: $outwardpostage \n Return Postage: $returnpostage \n \n Name: $name \n Email: $email \n Home Phone: $homephone \n Mobile Phone: $mobilephone \n \n Address1: $address1 \n Address2: $address2 \n Address3: $address3 \n City: $city \n County: $county \n Postcode: $postcode \n Country: $country ";
$recipient = "info#example.co.uk";
$subject = "Order Form";
$mailheader = "From: $email \r\n";
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($homephone) && !empty($address1) && !empty($city) && !empty($postcode) && !empty($country)){
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: http://www.example.co.uk/courier-mailer-success.htm");
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
exit;
?>
You could place it at the end of the URL as a GET parameter.
'success.php?orderid=one'
on that page access it with:
$_GET['item']
You could Store your data in a session and access it from your success page
A session variable would work great in this case. These are used to persist data between pages, and this gives the benefit of being able to access this variable throughout your application without having to pass as a GET parameter for every page where necessary. At the very top of your file, you'll need to start your session:
<?php
session_start();
$stamp = date("Ymdhis");
...
From here, you now have access to assign a session variable. the code will be as follows:
if(mail($recipient, $subject, $formcontent, $mailheader)){
$_SESSION['orderid'] = $orderid;
header("Location: http://www.example.co.uk/courier-mailer-success.htm");
}
From here, redirect to your success page. You will need to make your courier-mailer-success.htm into a .php file in order to access this data. You'll also need to add session_start(); to the top of your success page to access the session data. You can access your variable like so:
<?php
session_start();
...
$id = $_SESSION['orderid'];
echo $id;
I am using the following script to read emails.
<?php
//Fetch users from table
$sql_users=mysql_query("select userName, realpass,userID from users ");
while($row = mysql_fetch_assoc($sql_users)){
$username=$row['userName'].'#example.com'; // Email address is username#domain.com
$password=$row['realpass'];
$hostname = '{example.com:995/pop3/ssl/novalidate-cert}';
$username = $username; $password = $password;
$imap = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
for ($i = 1; $i <= $message_count; ++$i){
$header = imap_header($imap, $i);
// fetch message body and mark it as read
$body = imap_fetchbody($imap, $i,2,FT_PEEK);
$prettydate = date("jS F Y", $header->udate);
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$subject=$header->Subject;
//display email content
$email = "$personal <{$header->from[0]->mailbox}#{$header->from[0]->host}>";
echo "On $prettydate, $email said \"$body\".\n";
echo '<br><br>';
}
print_r(imap_errors());
imap_close($imap);
}
The problem is the email message returns from extra characters with it which need to be removed. Also I need to mark the emails as read.
Here is a sample message:
"
On 20th March 2013, Joe said "email prayer content.
This =A0is a test email for example.com. It should be converted into
a n= ew prayer request.
Thanks, Joe ". "
In the PHP reference, there is a comment with a similar issue to yours. In that comment he reads the content this way:
$text = trim( utf8_encode( quoted_printable_decode(
imap_fetchbody( $this->inbox, $emailNo, $section ) ) ) );
Try adjusting that example to your code and give it a try.
I did a webpage for a client that involved a series of text boxes asking for specific information such as a person's name, e-mail address, company, etc. Along with a button that would e-mail the information to my client. Whenever I tested the button it seemed to work perfectly, I uploaded the page and thought I was done. But, the other day my client got this email from the site:
Name: rfhopzdgmx rfhopzdgmx
Email: envlxw#lnlnsm.com
Company: zUDXatAfoDvQrdH
Mailing Address:
AaSsXklqpHIsoCNcei
gXsimMPRBYZqq
vGLvZraZNdpOAV, ChsmuibE PoKzaSCubXPRI
Home Phone: CIJbIfjMfjIaTqAlD
Work Phone: JFLZBOvru
Cell Phone: XlFJTTFGiTTiiFQfy
Fax: UEJMOVZodWPkKxew
Comments:
sPvSCE hgetwoguderu,*
[url=http://atyktjlxcznl.com/]atyktjlxcznl[/url],
[link=http://nudvfcehwpyg.com/]nudvfcehwpyg[/link], http://lvvwkbzbhnzp.com/
Note: The * line contained HTML link code, I just don't know how to get this site to show it.
Here is the PHP code in the site for the e-mail button.
<?php
//This Sends A Formatted Text Email Using The Text Boxes
if ($_POST['submit']){
//This Gets The Form Data
$fname = $_POST['fName'];
$lname = $_POST['lName'];
$email = $_POST['email'];
$company = $_POST['co'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$homep = $_POST['homeP'];
$workp = $_POST['workP'];
$cellp = $_POST['cellP'];
$fax = $_POST['fax'];
$comments = $_POST['txaOutputField'];
//echo "<script language = 'javascript'>alert('YAY');</script>";
if ($fname && $lname && $email && $comments){ //Check If Required Fields Are Filled
//This Sets The SMTP Configuration In php.ini
ini_set("SMTP", "smtp.2ndsourcewire.com");
//This Replaces Any Blank Fields With 'None's
if ($company == ""){
$company = "None";
}
if ($address1 == ""){
$address1 = "None";
}
if ($city == ""){
$city = "None";
}
if ($state == ""){
$state = "None";
}
if ($zip == ""){
$zip = "None";
}
if ($homep == ""){
$homep = "None";
}
if ($workp == ""){
$workp = "None";
}
if ($cellp == ""){
$cellp = "None";
}
if ($fax == ""){
$fax = "None";
}
//This Creates The Variables Necessary For The Email
$to = "CLIENT EMAIL WHICH I'M CENSORING";
$subject = "Email from 2ndSourceWire.com";
$from = "From: noreply#2ndsourcewire.com";
$secondEmail = "MY EMAIL WHICH I'M ALSO CENSORING";
if ($address2 == ""){
$body = "Name: $fname $lname\n".
"Email: $email\n".
"Company: $company\n\n".
"Mailing Address:\n".
"$address1\n".
"$city, $state $zip\n\n".
"Home Phone: $homep\n".
"Work Phone: $workp\n".
"Cell Phone: $cellp\n".
"Fax: $fax\n\n".
"Comments:\n".
"$comments";
}
else {
$body = "Name: $fname $lname\n".
"Email: $email\n".
"Company: $company\n\n".
"Mailing Address:\n".
"$address1\n".
"$address2\n".
"$city, $state $zip\n\n".
"Home Phone: $homep\n".
"Work Phone: $workp\n".
"Cell Phone: $cellp\n".
"Fax: $fax\n\n".
"Comments:\n".
"$comments";
}
//This Sends The Email
mail($to, $subject, $body, $from);
mail($secondEmail, $subject, $body, $from);
echo "<script language = 'javascript'>alert('The email was sent successfully.');</script>";
}
else {
//The Required Fields Are Not Filled
echo "<script language = 'javascript'>alert('Please fill your first name, last name, email address, and your comment or question.');</script>";
}
}
?>
I'm a little dumbfounded on how this happened, the client mentioned a couple e-mails of this, so I don't think it is a random glitch. Also, the e-mail address was formatted like an e-mail address, so someone or some program was interpreting the labels next to each text box. I also noticed that the first and last names entered are the same word, even though they were in different text boxes, I'm thinking its some spam program, but wouldn't they try to advertise something and make money, rather than just spouting out random text? Also, the comments section makes no sense to me at all, the links goto nowhere and they're all perfectly formatted, a random person just screwing around wouldn't know those tags, and a programmer doing it wouldn't bother with it, but also neither would a program.
I have no idea what caused this or how to fix it, I'm drawing a blank here. Anyone have any ideas?
A spammer/bot entered duff data into your page and you dutifully sent it on in your application.
Why do you think this is a mystery?
add a CAPTCHA to stop it happening. If you dont what to write your own you can use reCAPTCHA
even a simple question like "are you a human Y/n?" or "2+2?" will stop the bot,
also using some js to set an hidden value on submit and check for that on the server.
some validation on $email and $phone would be nice to have.
Instead of making people try to read CAPTCHAs, I like to have four text boxes in a row and ask the user to check two random ones (e.g. "Please check the first and third boxes") and make sure those are the only two checked in the validation.
Upon submitting this form on my site. It send me to a page that says.
"Use Back - fill in all fields Use
back! ! "
But this html isn't in the mail script anywhere. Where could this be coming from? I started out using this contact form (http://www.ibdhost.com/contact/) then changed it a little.
Here is the mail script.
<?php session_start(); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>
<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->
<?php
//the 3 variables below were changed to use the SERVER variable
$ip = $_SERVER['REMOTE_ADDR'];
$httpref = $_SERVER['HTTP_REFERER'];
$httpagent = $_SERVER['HTTP_USER_AGENT'];
$visitorf = $_POST['visitorf'];
$visitorl = $_POST['visitorl'];
$visitormail = $_POST['visitormail'];
$visitorphone = $_POST['visitorphone'];
//$notes = $_POST['notes'];
//$attn = $_POST['attn'];
$lookup = array(
'The Election Report' => 'http://www.mydowmain.net/',
'5 Resons' => 'http://www.mydomain.net/',
'Report 3' => 'http://someotherurl3.com/',
'Report 4' => 'http://someotherurl4.com/',
'Report 5' => 'http://someotherurl5.com/',
// et cetera for your other values
);
$attn = trim($_POST['attn']);
$url = $lookup[$attn];
//echo 'attn: ' . $attn . ', url:' . $url; die;
//additional headers
$headers = 'From: US <info#mailinator.net>' . "\r\n";
//$headers .= 'BCC: kelbizzle#mailinator.com' . "\r\n";
$todayis = date("l, F j, Y, g:i a") ;
$subject = "your lead has downloaded a report.";
$subjectdp = "Someone has downloaded a report!";
$notes = stripcslashes($notes);
$message = "Dear PAl Affiliate,\n\nA prospective lead of yours has downloaded a report from our Website.\nAny contact information they have left and a link to the report they downloaded\ncan be found below. This is the perfect opportunity for you to open up a line of\ncommunication with the prospect and find out their intrests! If you have any questions\nabout this email please feel free to email us at info#mailinaot.net\n\n\nFrom: $visitorf $visitorl ($visitormail)\nTelephone Number: $visitorphone \nReport Downloaded:$url\n \n\nBest regards,\nThe Crew";
//$message = "$todayis [EST] \nAttention: \nMessage: $notes \nFrom: $visitorf $visitorl ($visitormail) \nTelephone Number: //$visitorphone \nReport Downloaded:$url\nAdditional Info : IP = $ip \nBrowser Info: $httpagent \nReferral : $httpref\n";
$messagedp = "A Visitor has just downloaded a report. You can find their contact information below.\n
\n
***********************************************************************\n
From: $visitorf $visitorl\n
Email: $visitormail\n
Telephone Number: $visitorphone \n
Report Downloaded:$url\n
\n
\n
Best regards,\n
The Crew\n";
$messagelead = "Dear, $visitorf\n
\n
\n
We appreciate your interest. Below you will find the URL to download the report you requested.\n
Things are always changing in costa rica , so check back often. Also, check us out on Facebook & Twitter \n
for daily updates. If there is anything we can do at anytime to enhance your experience, please do\n
not hesitate to contact us.\n
\n
To download your report simply click on the link below. (You must have Adobe Reader or an alternative PDF reader installed)\n
\n
*** Download Link ***\n
$url\n";
//check if the function even exists
if(function_exists("mail"))
{
//send the email
mail($_SESSION['email'], $subject, $message, $headers) or die("could not send email");
} else {
die("mail function not enabled");
}
//send the email to us
mail('info#mailinator.com', $subjectdp, $messagedp);
//send the email to the lead
mail($visitormail, 'Thanks for downloading the report!', $messagelead, $headers);
header( "Location: http://www.mydomain.com/thanks_report.php" );
?>
</body>
</html>
According to the page you linked (an interesting shade of pink I might add ;) ), you have a sendeail.php (though I assume it's sendemail.php) and in that it contains the code:
if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}
You are implying that you've modified the above code to do what you want it to do, but did you change the location of the form POST on the page before? e.g. the example on your link posts to the page stated, if you haven't updated that link to point to your new version - then it would account for this error. (A side note, you probably should make sure that you've refreshed the page with the user-completed form on it too).