Monday, 11 August 2014

PHP Form to crete an XML attachment



Below is my code of my PHP form to attach an XML file. It all works okay but my xml file has not got any information in it?


I have added "echo $content;" to view the xml code in the page source but all I get is:



<?xml version="1.0"?>
<AllData/>


This is quite urgent so any help is appreciated.



<?php


$xmlDoc = new DOMDocument('1.0');


$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);

$xmlIndividual = $xmlDoc->createElement('Individual');

$xmlname = $xmlDoc->createElement('contact_name', $name);
$xmlIndividual->appendChild($xmlname);

$name = $_POST["contact_name"];

$xmlemail_address = $xmlDoc->createElement('contact_email', $email_address);
$xmlIndividual->appendChild($xmlemail_address);

$email_address = $_POST["contact_email"];

$xmlphone = $xmlDoc->createElement('contact_phone', $phone_number);
$xmlIndividual->appendChild($xmlphone);

$phone_number = $_POST["contact_phone"];

$xmlmessage = $xmlDoc->createElement('contact_message', $message);
$xmlIndividual->appendChild($xmlmessage);

$message = $_POST["contact_message"];


$content = $xmlDoc->saveXML();

if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}

$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];

if (empty($name))
$error = 'You must enter your name.';
elseif (empty($email_address))
$error = 'You must enter your email address.';
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z] {2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
if (empty($phone))
$error = 'You must enter your phone number.';
elseif (empty($message))
$error = 'You must enter a message.';
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";

mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'test2.xml');

header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;

function mail_attachment($to, $subject, $message, $from, $data, $test2) {
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
$from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
$header = "From: ".$from."\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
."This is a multi-part message in MIME format.\r\n"
."--".$uid."\r\n"
."Content-type:text/plain; charset=iso-8859-1\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$message."\r\n\r\n"
."--".$uid."\r\n"
."Content-Type: application/octet-stream; name=\"".$test2."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$test2."\"\r\n\r\n"
.$content."\r\n\r\n"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>


Form:
<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"> </textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

No comments:

Post a Comment