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).
Related
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.
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.
Using HTML form with action = "www.google.com", but it redirect to link including localhost/www.google.com . My example code is for your reference. Please anyone help me.
My Forms as follows :
<form method="POST" action="www.google.com">
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">Email:</label>
<input type="email" name="email" id="email">
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments"></textarea>
</p>
<p>
<input type="submit" name="send" id="send" value="Send Comments">
</p>
</form>
But, the file when click send button, it redirects to
" http://localhost/basic/www.google.com" error page. Can any one suggest me to resolve the issue.
It is not allowed to send POST request directly to google.com. Why do you want to do this? In the action argument you should have own script which can process request from your form.
Replace your form action
From
action="www.google.com"
To actual link
action="https://www.google.com"
I have a simple form for a website allowing users to send a query/message to my clients email address.
I'm struggling trying to get this working. So far I have managed to find some code (also via stack overflow) that allows the form to send a blank email to the correct address upon submit. I seem to be missing some code or something that makes the submit button pull information from the form to send.
Below is the code I have.
Thanks (PS I have no experience with PHP, only as to alter the code I previously found to get this far).
<?php
mail('chameleonyeadon#gmail.com', $_POST['name'], $_POST['email'], $_POST['number'], $_POST['message']);
?>
<form method="post" action="email.php" enctype="text/plain">
<!-- name-->
<label class="contact__form--label"><em>What is your name?</em><br>
<input type="text" name="name" class="contact__form__inputbox contact__form__inputbox--marg contact__form__inputbox--pad" placeholder="Enter your name here" maxlength="30" required="required"/></label><br>
<!-- email-->
<label class="contact__form--label"><em>Do you have an email address?</em><br>
<input type="email" name="email" class="contact__form__inputbox contact__form__inputbox--marg contact__form__inputbox--pad" placeholder="Enter your email address here" maxlength="50" required="required"/></label><br>
<!-- number-->
<label class="contact__form--label"><em>Can we contact you by phone?</em><br>
<input type="number" name="number" class="contact__form__inputbox contact__form__inputbox--marg contact__form__inputbox--pad" placeholder="Enter a phone number we can use here" maxlength="50" required="required"/></label><br>
<!-- message-->
<label class="contact__form--label"><em>What is your message?</em><br>
<textarea name="message" required="required" class="contact__form__inputbox contact__form__inputbox--marg contact__form__textarea contact__form__inputbox--pad" placeholder="Enter your message here"></textarea></label><br>
<div class="contact__form__btns">
<input type="submit" value="Send" class="btn btn--brdr btn--padding--less">
<input type="reset" value="Clear" class="btn btn--brdr btn--padding--less btn__formpos">
</div>
</form>
First you have to remove action="email.php" since you put PHP code in the same file.
Then, you have to remove enctype="text/plain" to get it works.
See this stackoverflow subject
Finally, it is good to test what was send in PHP before doing mail function in addition to your required verification
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.