Need assistance with PHP and my HTML5 form - php

I used an anchor tag for my submit button so I think that this is probably why I'm having so much trouble trying to figure this out.
I'm not sure if it's the "href" that is messing it all up. I also would like some assistance with server side validation, not having a lot of experience in PHP, I was only able to validate using JavaScript.
<form id="myform" method="post" name="contact_form" action="process.php">
<input id="cname" type="text" name="name" minlength="2" placeholder="Full Name" class="form-control" required>
<input id="cemail" type="email" name="email" placeholder="Email Address" class="form-control" required>
<textarea id="ccomment" rows="5" name="message" placeholder="Message..." class="form-control" required></textarea>
<div id="send-btn">
SEND
<asp:Button runat="server" Text="submit"/>
</div>
</form>
This below is my PHP code. Please assist with validations and making it work with an anchor tag. I could have messed up the form somewhere.
<?php ob_start();
if (isset($_POST['submit'])) {
$to = "someoneelse#someone.com";
$name = $_POST['name'];
$email = $_POST['email'];
$txt = $_POST['message'];
$headers = "From: .$email" . "\r\n" .
mail($to,$email,$txt,$headers);
header("Location: index.html");
}
?>

Ideally, with modern Javascript you would avoid having any script in your markup and would attach the click event using jquery and prevent the default action for the anchor tag using preventDefault. (See https://api.jquery.com/click/ and https://api.jquery.com/event.preventDefault/ )
However, if you are going to use an onclick attribute, you need to return false. If the onclick event returns false, then the browser will cancel the default action.
SEND
When the anchor link is clicked, the javascript to submit the form will run, but the return false will prevent the link from being followed. The return false will also prevent the browser from jumping to the top when you use a # in your href.
As for server side validation in PHP, you will need to use conditional statements to check that the posted variables match a pattern. See How to validate an email address in PHP .

As said in the comment you just need:
SEND
Small explanation javascript: void(0) or #
If you use javascript: void(0) when you click on the a element the browser will try to run a function called void(0) which should be undefined and do nothing but skip to your onClick.
Instead if you use the # syntax the browser will jump to the top of the page because is looking for some tag with the attribute id empty.

I did as instructed in the anchor tag. The other issue now is nothing happens when I click on submit, is my php code incorrect?
<form id="myform" method="post" name="contact_form" action="process.php">
<input id="cname" type="text" name="name" minlength="2" placeholder="Full Name" class="form-control" required>
<input id="cemail" type="email" name="email" placeholder="Email Address" class="form-control" required>
<textarea id="ccomment" rows="5" name="message" placeholder="Message..." class="form-control" required></textarea>
<div id="send-btn">
SEND
<asp:Button runat="server" Text="submit">
</div>
</form>

Related

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.

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).

Ajax call on submit refreshing page

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.

PHP Form Redirect to error link

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"

HTML5 form with jQuery doesn't work on Chrome and Opera

I've used the exact files from this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
The ideea is to submit the form without refreshing, and displaying a nice animation after submission.
But when I try to use the files for my personalised form, the animation doesn't work on Chrome and Opera. Why?
<div id="contact_form">
<form name="contact" method="post" action="">
<p>Hi, my name is</p>
<input type="text" name="name" id="name" class="input" size="20" value="" class="text-input" />
<p> and my email is</p>
<input type="text" name="email" id="email" class="input" size="20" value="" class="text-input"/>
<p>More Random Text.</p>
<textarea name="message" id="message" class="input" onfocus="this.value='';" class="text-input"> ... </textarea>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</form>
</div>
Everything works ok in this fiddle:
http://jsfiddle.net/CTGa3/2/
Works in Firefox and Chrome. I am not using your php because I don't have access to it. Instead, I am doing a dummy ajax call via jsfiddle, but the result should be the same.
The only thing I could see is that you didn't include the code for runOnLoad. If you include the code, or remove the runOnLoad() call, everything works fine. That piece of code is outdated anyway, because jQuery can do that for us. You just have to move this line:
$("input#name").select().focus();
To the top of your jQuery code block:
http://jsfiddle.net/CTGa3/3/

Categories