Exporting data from an HTML form into an XML document - php

Alright here is what I am attempting to do. I have a form written in HTML. It has a few HTML5 elements to it though. What I would like to happen, is when the user fills out all the required fields (validated with PHP and some HTML5) and submits the form, it will export to an existing XML file. The form also uses AJAX to validate as the user fills out the fields, and to validate the form as a whole (once submitted) without reloading my page.
The XML file is just a framework of empty tags set up to receive the information from form submissions, built upon a schema (I made) to restrict it to certain values and what not. I would also need to be able to have a script to validate the new XML values against the schema to make sure all values are valid, and if not return an error message.
The XML Schema validation is as close as possible to the PHP validation with a few character amount limits added in here and there.
If you need me to post the schema for the XML file in order to give a more specific answer then I will upon request.
XML Framework:
<form>
**<!-- First Name -->**
<fname></fname>
<!-- Last Name -->
<lname></lname>
<!-- Phone Number -->
<phone></phone>
<!-- Email -->
<email></email>
<!-- Website -->
<website></website>
<!-- Subject -->
<subject></subject>
<!-- Message -->
<message></message>
</form>
HTML Form:
<form method="post" action="php/validator.php" name="contactform" id="contactform" autocomplete="on">
<fieldset>
<legend>Contact Details</legend>
<div>
<label for="fname" accesskey="F">Your First Name*</label>
<input name="fname" type="text" id="name" placeholder="Enter your first name" tabindex="1" required />
</div>
<div>
<label for="lname" accesskey="L">Your Last Name*</label>
<input name="lname" type="text" id="name" placeholder="Enter your last name" tabindex="2" required />
</div>
<div>
<label for="email" accesskey="E">Email*</label>
<input name="email" type="email" id="email" placeholder="myEmail#example.com" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)#([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" tabindex="3" required />
</div>
<div>
<label for="phone" accesskey="P">Phone <small>(optional)</small></label>
<input name="phone" type="tel" id="phone" size="12" placeholder="555-555-5555" tabindex="4" />
</div>
<div>
<label for="website" accesskey="W">Website <small>(optional)</small></label>
<input name="website" type="url" id="website" placeholder="www.yourWebDomain.com" tabindex="5" />
</div>
</fieldset>
<fieldset>
<legend>Your Comments</legend>
<div>
<label for="subject" accesskey="S">Subject*</label>
<select name="subject" id="subject" tabindex="6" required="required">
<option value=""></option>
<option value="Support">Support</option>
<option value="Sale">Sales</option>
<option value="Bug">Report a bug</option>
<option value="Other">Other</option>
</select>
</div>
<div>
<label for="comments" accesskey="C">Comments*</label>
<textarea name="comments" cols="40" rows="3" id="comments" class="comments" placeholder="Enter your comments" spellcheck="true" required tabindex="7"></textarea>
</div>
</fieldset>
<input type="submit" class="submit" id="submit" value="Submit" tabindex="9" />
</form>

After validation use PHP to capture the form fields, wrap them in XML elements, then save it.
e.g.
<form>
<fname><?php=$_REQUEST['fname']?></fname>
<lname><?php=$_REQUEST['lname']?></lname>
...
..
</form>
No sense in double validation either, with one slightly different than the other. It would be annoying for the user to submit their data successfully but somehow get rejected in the backend without warning. DOMDocument is a little overkill as well.

PHP contains a class called DOMDocument specifically for the purpose of building, altering, importing and exporting XML data. If you are familiar with constructing DOM elements in Javascript, it is the same idea. Create your DOMDocument, then create each element and append those elements to DOMDocument (or that element's parent) after you've made them. After you're done there, use the save function to write it to a file.

Related

Saving and displaying user input using HTML and PHP

I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?
So far I have this:
<html lang>
<head>
<link rel="stylesheet" href="css/addOrEditCinemaPage.css">
</head>
<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>
<body>
<div class="myForm">
<form>
<h2>Add or Edit a Cinema</h2>
<label for="name"><b>Name of Cinema</b></label>
<input type="text" placeholder="Enter name">
<label for="str"><b>Street</b></label>
<input type="text" placeholder="Enter street">
<label for="nr"><b>Nr.</b></label>
<input type="number" placeholder="Enter Nr.">
<label for="plz"><b>Post Code</b></label>
<input type="number" placeholder="Enter Post Code"><br><br>
<label for="ct"><b>City</b></label>
<input type="text" placeholder="Enter City">
<label for="sta"><b>State</b></label>
<input type="text" placeholder="Enter State">
<label for="descr"><b>Description</b></label><br>
<textarea placeholder="Enter Discription"></textarea>
<div class="imagebutton">
Add Image
</div>
<button type="submit">Save</button>
</form>
</div>
<?php include "php/footer.php" ?>
</body>
</html>```
Can the PHP to save and display to input infomration also be written here?
Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST.
Instead of using
<form>
use
<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">
Then, set the 'name' attribute of each input.
For example, Instead of using
<input type="text" placeholder="Enter name">
use
<input type="text" name="name" placeholder="Enter name">
You'll also have to set the 'name' attribute of the submit button to 'submit':
<button type="submit" name="submit">Save</button>
Once you've done that, the PHP code to access the form data would be:
if (isset($_POST['submit'])) {
echo $_POST['name'];
}
send your data with html to another php page
you must use get or post for sending form data to php page
like below

HTML Form Validation in PHP.

I am completely brand-new to php and after researching about it I am still unable to find the appropriate answer. As I am trying to get my form validated with php and the error messages customised with CSS.
My HTML code is :
<div class="container">
<form action="/action_page.php">
<div class="flex-row">
<input type="text" id="name" name="name" placeholder="Name">
<input type="email" id="email" name="email" placeholder="Email Address">
<input type="number" id="age" name="age" placeholder="Age">
</div>
<div class="flex-row">
<input type="text" id="subject" name="subject" placeholder="Subject">
<select type="dropdown" name="contact" placeholder="Age">
<option placeholder="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
</div>
<div class="flex-row">
<textarea id="message" name="message" placeholder="Talk To Us" style="height:200px"></textarea>
</div>
<input type="submit" value="Submit">
</form>
My CSS is on pastebin below:
Pastebin
P.S I am also thinking about putting the for validation code into a separate file and that file will be saved into the includes folder.
The data will be available in the php-script through the $_GET global variable as an array. Each input is an item in this array, e.g. $_GET["name"].
The script action_page.php could look something like this (this is just a beginning...):
<?php
//Test the input, for example like this:
if(empty($_GET["name"])) $error="You need to provide a name";
if(strpos($_GET["email"],"#")==0) $error="The e-mail is not valid"; //Of course you could test for a lot more, for example using regular expressions
//Now you can build your response to the user
if($error) {
echo ("<h1>Error</h1>".$error);
}
include("form.html"); //If you want to show the same form again.
You could also include the errors more integrated in the form - but then you would need to integrate PHP into the HTML.
You will find good documentation at http://www.php.net, and http://w3schools.com/ has a lot of good stuff as well.

How can I embed http referrer in divi contact form

How can I embed http referrer in divi contact forms. I am able send only email, message text fields which are given inbetween %%field id%%. In Divi documentation they given that we need to put field id in between %% like this %%field id%%. But I am unable to add the http referrer or page url. I saw source code like this
"
<label for="et_pb_contact_name_1" class="et_pb_contact_form_label">Name</label>
<input type="text" id="et_pb_contact_name_1" class="input" value="" name="et_pb_contact_name_1" data-required_mark="required" data-field_type="input" data-original_id="name" placeholder="Name">
</p><p class="et_pb_contact_field et_pb_contact_field_1 et_pb_contact_field_last" data-id="email" data-type="email">
<label for="et_pb_contact_email_1" class="et_pb_contact_form_label">Email Address</label>
<input type="text" id="et_pb_contact_email_1" class="input" value="" name="et_pb_contact_email_1" data-required_mark="required" data-field_type="email" data-original_id="email" placeholder="Email Address">
</p><p class="et_pb_contact_field et_pb_contact_field_2 et_pb_contact_field_last" data-id="phone" data-type="input">
<label for="et_pb_contact_phone_1" class="et_pb_contact_form_label">Phone Number</label>
<input type="text" id="et_pb_contact_phone_1" class="input" value="" name="et_pb_contact_phone_1" data-required_mark="required" data-field_type="input" data-original_id="phone" placeholder="Phone Number" pattern="[0-9]*" title="Only numbers allowed.">
</p><p class="et_pb_contact_field et_pb_contact_field_3 et_pb_contact_field_last" data-id="message" data-type="text">
<label for="et_pb_contact_message_1" class="et_pb_contact_form_label">Message</label>
<textarea name="et_pb_contact_message_1" id="et_pb_contact_message_1" class="et_pb_contact_message input" data-required_mark="required" data-field_type="text" data-original_id="message" placeholder="Message"></textarea>
</p><p class="et_pb_contact_field et_pb_contact_field_4 et_pb_contact_field_last" data-id="url" data-type="input">
<label for="et_pb_contact_url_1" class="et_pb_contact_form_label">New Field</label>
<input type="text" id="et_pb_contact_url_1" class="input" value="" name="et_pb_contact_url_1" data-required_mark="not_required" data-field_type="input" data-original_id="url" placeholder="New Field">
</p>
<input type="hidden" value="et_contact_proccess" name="et_pb_contactform_submit_0">
<input type="text" value="" name="et_pb_contactform_validate_0" class="et_pb_contactform_validate_field" />
<div class="et_contact_bottom_container">
<button type="submit" class="et_pb_contact_submit et_pb_button">Send Message</button>
</div>
<input type="hidden" id="_wpnonce-et-pb-contact-form-submitted" name="_wpnonce-et-pb-contact-form-submitted" value="8b651527df" /><input type="hidden" name="_wp_http_referer" value="/data-analytics/test" />
</form>
</div> <!-- .et_pb_contact -->
</div> <!-- .et_pb_contact_form_container -->
Clearly there is hidden field but I dont know how to embed in form. Thanks
You can create new field and set display: none then just copy id of this input field.
Next create below divi code module and paste this
<script> const newInput = document.querySelector('#et_pb_contact_oboz-nazwa_0'); newInput.setAttribute('value', document.title) </script>
Where et_pb_contact_oboz-nazwa_0 - is ID of input field. You have to choose yours
document.title - is a title of page
If you want get url of page change document.title to window.location.href
And of course set this new input field to be not required

Two working forms on one page

I want two forms on my page that use the same script. The first one is sent correctly, but the second is not. I'm very beginner in PHP and I have no idea how to do it. I am trying to solve this, unfortunately without results. I'm using the phpmailer.
First form:
<div id="form-main">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="company">
<input name="company" type="text" class="feedback-input" placeholder="Nazwa firmy" id="company"/>
</p>
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<p class="email">
<input name="email" type="email" required class="feedback-input" id="email" placeholder="Adres e-mail"/>
</p>
<p class="text">
<textarea name="message" class="feedback-input" id="comment" placeholder="Opis zamówienia"></textarea>
</p>
<button type="submit" class="button-blue">Send</button>
</div>
</form>
</div>
Second form:
<div id="second-form">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<button type="submit" class="button-blue">Send</button>
</form>
handler.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','phone','company'])->areRequired()-
>maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$pp->attachFiles(['image']);
$pp->sendEmailTo('sadowm1990#gmail.com'); // ← Your email here
echo $pp->process($_POST);
First: I am pretty sure both of your forms are sent/submitted without any problems. Your second form misses the div closing tag </div> but that shouldn't make such problems.
By "the first one is sent correctly, but the second is not." I assume you mean that you are not getting any email on the selected address when using the second form. If that is the case then:
I am not sure how your validator functions but it looks like that your problem is the validation process.
Why? : In your php code you are validating name, email, phone, company, message but only your first form have chance to satisfy those because it has those fields. Your second form doesn't have all those fields which have to be validated, so, your validation is going to fail. As consequence you are not going to receive any email (if the validator functions in that way).
In order to make your second form to function on this php code you have either to:
1. differentiate those two forms in your php code and apply the correct validation on them or
2. add missing fields to your second form (you can still make them hidden using html/css, but you need to set their default values and submit them altogether, I don't recommend doing this).

inserting a premade email contact form into website

I have very little knowledge of php so I went ahead and used one from css-tricks email forms he set up for free use:http://css-tricks.com/nice-and-simple-contact-form/.
However I am not sure how to put it into the website without breaking the php code. I assume I can't simply just take the code that I want, which is
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
<div style="clear: both;"></div>
<p>Check out a version of this with SPAM protection.</p>
</div>
and put it into my html page. I tried using an iframe to link the html page into my html page for my website and it worked, but is this method ok?
Yes, you can just place that coding in your HTML page as long as all the webpage and css file's are linked correctly.(e.g. In the same folder as this page with the coding you have now.)
Using an i-frame for this is not a good idea as it can really mess up your pages and submitting your form.

Categories