Jump to content
Sign in to follow this  
BeckyKing

Php form to send XML

Recommended Posts

Thank you. I am very new to PHP and XML and still abit confused. Any chance anyone can give me abit of code to work with? I have the PHP form just need the bit where I add the XML attachment. I know there were a lot of examples in those links but as I said still confused. Thank you in advance

Share this post


Link to post
Share on other sites

We're really here to help people learn how to program, rather than write code for people. There should be some examples you can find online though:https://www.google.com/search?client=opera&q=php+create+xml+from+form+data&sourceid=opera&ie=UTF-8&oe=UTF-8If you have specific questions about how things work, we can help fill in the blanks.

Share this post


Link to post
Share on other sites

How do I now attach the xml doc? Thats if I have done the creating of the xml doc correct?

 

 

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>
PHP:
<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$xmlname = $xmlDoc->createElement('contact_name', $input_name);
$xmlIndividual->appendChild($xmlname);
$xmlemail_address = $xmlDoc->createElement('contact_email', $input_email_address);
$xmlIndividual->appendChild($xmlemail_address);
$xmlphone = $xmlDoc->createElement('contact_phone', $input_phone);
$xmlIndividual->appendChild($xmlphone);
$xmlmessage = $xmlDoc->createElement('contact_message', $input_message);
$xmlIndividual->appendChild($xmlmessage);
$content = chunk_split(base64_encode($xmlDoc->saveXML()));
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid 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.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$headers = "From: $email_addressrn";
$headers .= "Reply-To: $email_addressrn";
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('becky.king99@gmail.com', 'Westover', $email_content, $headers);
// send the user back to the form
header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;
?>

Share this post


Link to post
Share on other sites

Using PHP's mail function to send with attachments means that you have to build the entire message and headers yourself. There are other PHP packages to send mail that will handle that for you, such as PHPMailer. With PHPMailer, for example, it would be as easy as this to add your attachment:$mail->addStringAttachment($xml, 'filename.xml', 'base64', 'application/xml', 'attachment');If you want to stick with PHP's regular mail function, then there's an example function here to do that:http://stackoverflow.com/questions/4586557/php-send-email-with-attachmentThat function expects a filename though, and you aren't saving a file, you're just generating a string of XML. The function can be modified like this:

function mail_attachment($to, $subject, $message, $from, $data, $filename) {  $content = chunk_split(base64_encode($data));   $uid = md5(uniqid(time()));  $from = str_replace(array("r", "n"), '', $from); // to prevent email injection  $header = "From: ".$from."rn"      ."MIME-Version: 1.0rn"      ."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"      ."This is a multi-part message in MIME format.rn"       ."--".$uid."rn"      ."Content-type:text/plain; charset=iso-8859-1rn"      ."Content-Transfer-Encoding: 7bitrnrn"      .$message."rnrn"      ."--".$uid."rn"      ."Content-Type: application/octet-stream; name="".$filename.""rn"      ."Content-Transfer-Encoding: base64rn"      ."Content-Disposition: attachment; filename="".$filename.""rnrn"      .$content."rnrn"      ."--".$uid."--";   return mail($to, $subject, "", $header);}
So, you pass that function the to address, subject, email text, from address, the file data, and the filename you want to use, and you use that function instead of PHP's mail function (it calls mail at the end).Your code has these lines:
$content = chunk_split(base64_encode($xmlDoc->saveXML()));  /* No need to create an actual file, just use the content of the created XML document */$content = $xmlDoc->saveXML();
Both of those are setting the $content variable with your XML data. There's no need to do that twice, and since that function expects normal un-encoded data, just remove the first line.At the end of your code, you have lines where you set the From and Reply-To headers, you can delete those lines. Then you have a section where you build the $email_content variable with the email text, and after that you call the mail function and send it everything. Keep the part where you build $email_content, but remove the line where you call the mail function. Instead of that line, add this line to use the above function:
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'attachment.xml');
In that line, $content is the XML string that you generated, and you can change the last value if you want to change the filename of the attachment. Make sure you also include that function definition somewhere in your code.

Share this post


Link to post
Share on other sites

Thank you for your help I have a fully working form that attaches the xml document.

 

My problem now is that the information is not being transferred onto the xml document? so when i open the xml document attachment there is no information?

Share this post


Link to post
Share on other sites

In the code you posted above you're using variables like $input_name, $input_email_address, etc. Make sure those variables have the values that you want before you write them to the XML file.

Share this post


Link to post
Share on other sites

You have lines like this:$xmlname = $xmlDoc->createElement('contact_name', $input_name);If $input_name isn't set to anything, then the XML file isn't going to contain anything for that element. Get those values from the form and set those variables before trying to write them into the XML document.

Share this post


Link to post
Share on other sites

This didn't seem to work?

<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$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"];
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid 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.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'test2.xml');
// send the user back to the form
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."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$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>

Share this post


Link to post
Share on other sites

You're getting the values from the form after building the XML document, move the code that gets the values to the top.

Share this post


Link to post
Share on other sites

If you moved this:$name = $_POST["contact_name"];before this:$xmlname = $xmlDoc->createElement('contact_name', $name);And the XML document has a contact_name element that is empty, then it sounds like that field wasn't filled in on the form (or else it wouldn't be empty). Try printing out the XML document (the $content variable) after creating it to see what it actually creates.

Share this post


Link to post
Share on other sites

I have moved the code to where you said but my XML is still not being generated.

 

Do i need to write anything in the XML document or write the XML code in the PHP?

 

<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$contact_name = $_POST["contact_name"];
$contact_email = $_POST["contact_email"];
$contact_phone = $_POST["contact_phone"];
$contact_message = $_POST["contact_message"];
$xmlname = $xmlDoc->createElement('contact_name', $contact_name);
$xmlIndividual->appendChild($xmlname);
$xmlemail_address = $xmlDoc->createElement('contact_email', $contact_email);
$xmlIndividual->appendChild($xmlemail_address);
$xmlphone = $xmlDoc->createElement('contact_phone', $contact_phone);
$xmlIndividual->appendChild($xmlphone);
$xmlmessage = $xmlDoc->createElement('contact_message', $contact_message);
$xmlIndividual->appendChild($xmlmessage);
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid 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.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'test2.xml');
// send the user back to the form
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."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>

Share this post


Link to post
Share on other sites

The variable $content contains the XML content. Print that out to see what it's generating.

Share this post


Link to post
Share on other sites

I know its probably simple but I keep getting errors when I try to print out the variable $content?

 

What is the best code to use and where abouts do I put it?

Share this post


Link to post
Share on other sites

Just use "echo $content;", and put it anywhere after the $content variable is created. You'll need to view the HTML source of the page to see the actual content without the browser trying to render it. You'll get an error about headers already being sent unless you comment out the header lines so that it doesn't try to redirect you.

Share this post


Link to post
Share on other sites

You're not doing anything with the $xmlindividual node. You create that node, and add all of the child nodes to it, but nothing else. After adding the child nodes to it you should append that node to the root node, which will add it to the XML document.

Share this post


Link to post
Share on other sites

Your code uses appendChild in several places. Every XML node has an appendChild method. You need to use the root node's appendChild method to append the individual node.

Share this post


Link to post
Share on other sites

I have used $xmlroot->appendChild($xmlindividual); and tried $xmlroot->appendChild($xmlfirstname); But keep getting and error 'Fatal error: Call to a member function appendChild() on a non-object in /websites/123reg/LinuxPackage22/ki/ng/_c/king-creations.co.uk/public_html/beckyking/contact-form-submission.php on line 23'

 

Code:

<?php

/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$firstname = $_POST["firstname"];
$surname = $_POST["surname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$brand = $_POST["brand"];
$vehmodel = $_POST["vehmodel"];
$vehcolor = $_POST["vehcolor"];
$contact_message = $_POST["contact_message"];
$xmlfirstname = $xmlDoc->createElement('firstname', $firstname);
$xmlIndividual->appendChild($xmlfirstname);
$xmlroot->appendChild($xmlIndividual); "<<<<--- line 23 "
$xmlsurname = $xmlDoc->createElement('surname', $surname);
$xmlIndividual->appendChild($xmlsurname);
$xmlroot->appendChild($xmlIndividual);
$xmlemail = $xmlDoc->createElement('email', $email);
$xmlIndividual->appendChild($xmlemail);
$xmlroot->appendChild($xmlIndividual);
$xmlphone = $xmlDoc->createElement('phone', $phone);
$xmlIndividual->appendChild($xmlphone);
$xmlroot->appendChild($xmlIndividual);
$xmlbrand = $xmlDoc->createElement('brand', $brand);
$xmlIndividual->appendChild($xmlbrand);
$xmlroot->appendChild($xmlIndividual);
$xmlvehmodel = $xmlDoc->createElement('vehmodel', $vehmodel);
$xmlIndividual->appendChild($xmlvehmodel);
$xmlroot->appendChild($xmlIndividual);
$xmlvehcolor = $xmlDoc->createElement('vehcolor', $vehcolor);
$xmlIndividual->appendChild($xmlvehcolor);
$xmlroot->appendChild($xmlIndividual);
$xmlmessage = $xmlDoc->createElement('contact_message', $contact_message);
$xmlIndividual->appendChild($xmlmessage);
$xmlroot->appendChild($xmlIndividual);
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$brand = $_POST['brand'];
$vehmodel = $_POST['vehmodel'];
$vehcolor = $_POST['vehcolor'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($firstname))
$error = 'You must enter your name.';
// check that an surname was entered
elseif (empty($surname))
$error = 'You must enter your surname.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that an email address was entered
elseif (empty($email))
$error = 'You must enter an email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/', $email))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a Franchise was entered
if (empty($brand))
$error = 'You must enter a Franchise.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "First Name: $firstnamen";
$email_content .= "Surname: $surnamen";
$email_content .= "Email: $emailn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Franchise: $brandn";
$email_content .= "Model: $vehmodeln";
$email_content .= "Colour: $vehcolorn";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email, $content, 'test2.xml');
// send the user back to the form
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."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>
Edited by BeckyKing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×