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.
Related
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
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).
Problem
My page seems to be defaulting to my PHP code instead of being handled asynchronously via ajax. As of now, my page just reloads as a blank screen, the input array successfully got passed over to the PHP, which is comforting, but I seem to just be doing something wrong with the ajax.
What I've tried
After viewing this link and this link, I still cant seem to get this figured out.
Current Standing
Here is the HTML:
<form class="cmxform" id="commentForm" method="" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="spinner">How much do you love science? (optional)</label>
<input id="spinner" name="spinner">
</p>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="aoi">Area of Interest (optional)</label>
<input id="aoi" type="text" name="aoi">
</p>
<p>
<label for="currprobs">Current Problems (optional)</label>
<input id="currprobs" type="text" name="currprobs">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input id="launch" type="submit" value="Submit">
</p>
</fieldset>
</form>
<div id="results"></div>
Here is the javascript:
$(document).ready(function(){
$("#commentForm").submit(function(event){
/*
var formData = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
};
*/
alert("SUCCESS?");
});
});
PHP code to return a div after the user has been validated:
<?php
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
echo '<div id=dynamicDiv>
<p>Hello, '.$username.'!</p>
<p>We look forward to contacting you at, '.$email.'</p>
</div>';
?>
Feedback
Any thoughts are appreciated, even intellectual conversation. Code snippets are idea, but I appreciate anything the community is able
Edit 1
I motified the javascript and php to reflect knowledgable input and best practice for PHP and AJAX calls, however the issue still remains.
Edit 2
My goal now is to just get an alert() statement working inside of my javascript, ajax will be the next step.
I think problem is here
$("#results").html = html_i;
You try change to
$("#results").html(html_i);
And in php you should check name and email
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
You should change input type="submit" to input type="button" and call Ajax on onclick function, Its resolve your problem because submit button submit form and Ajax call pending not reached to 400 state.
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.
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.