PHP Form syntax error [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
again i trying to do a simple contact form for sending email,
thats the code:
<?php
if (empty($_POST) === false) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name,' ', $email, ' ', $message;
if (empty($name) === true || empty($email) === true || empty($message) === true) {
$errors[] = 'name,email and message are required';
} else{
if(filter_var($email,FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'that\'s not a valid email address';
}
if(ctype_alpha($name) === false) {
$errors ='name must only contain letters';
}
}
if (empty($errors) ===true) {
mail('talrod160#gmail.com','contact form','$message','From:' . $email);
header('location:index.php?sent');
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>A contact form</title>
</head>
<body>
<?php
if (isset($GET ['sent']) === true) {
echo '<p>Thanks for contact us<?/p>';
} else {
if (empty($errors) === false) {
echo '<ul>';
foreach($errors as $error) {
echo '<li>', $error, '</li>';
}
echo '</ul>';
}
?>
<form action="" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" <?php if (isset($_POST['name']) === true){echo ($_POST['name']), '"'} ?>>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"<?php if (isset($_POST['email']) === true){echo 'value="', ($_POST['email']), '"'} ?>>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"><?php if (isset($_POST['message']) === true) { echo strip_tags ($_POST['message']); } ?></textarea>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
<?php
}
?>
</body>
</html>
now i get this problem and cant understand why,
Parse error: syntax error, unexpected '<' in D:\xampp\htdocs\contact_form\index.php on line 46
and would like if someone can explain me me about foreach loop
thanks.

<p>Thanks for contact us<?/p> probably should be <p>Thanks for contact us</p>

Related

CSS padding dissapears when submitting form [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have made a contact form using PhP, HTML and CSS. When I submit the form with incorrect information, the CSS padding dissapears. Do any one see what I am doing wrong? Thank you in advance!
<?php
if (empty($_POST) === false) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) === true || empty($email) === true || empty($message) === true) {
$errors[] = 'Name, email and message are required!';
} else {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'That\'s not a valid email address!';
}
if (ctype_alpha($name) === false) {
$errors[] = 'Name must only contain letters!';
}
}
if (empty($errors) === true) {
mail('name#example.com', 'Contact form', $message, 'From: ' . $email);
header('Location: index.php?sent');
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>A contact form</title>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/css/application.css">
</head>
<body>
<?php
if (isset ($_GET['sent']) === true) {
echo '<p>Thanks for contacting me!</p>';
} else {
if (empty($errors) === false) {
echo '<ul>';
foreach($errors as $error) {
echo '<li>', $error,'</li>';
}
echo '</ul';
}
?>
<form action="" method="post" class="skinny_wrapper wrapper_padding">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" <?php if (isset($_POST['name']) === true) { echo 'value="', strip_tags($_POST['name']), '"'; } ?>>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email" <?php if (isset($_POST['email']) === true) { echo 'value="', strip_tags($_POST['email']), '"'; } ?>>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"><?php if (isset($_POST['message']) === true) { echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
<input type="submit" value="Send Message">
</p>
</form>
<?php
}
?>
</body>
</html>
Screenshots:
Before
After
Probably some css rules were overwriten by additional class which appears during validation.

Contact form with XAMPP not working?

Im trying to create a contact form in xampp, but I don't know why the form isn't sending the email. The php form also displays no errors even if it is told to. I have the file in the proper localhost directory, yet it does not perform correctly. Here is the code I use
<?php
if (empty($_POST) === false) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) === true || empty($email) === true || empty($message) === true) {
$errors[] = 'Name, email and message are required!';
} else {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'That\'s not a valid email address';
}
if (ctype_alpha($name) === false) {
$error[] = 'Name must only contain letters';
}
}
if (empty($errors) === true) {
mail('admin#localhost', 'Contact form', $message, 'From: ' . $email);
header('Location: www.artfbla.org/contact.php?sent');
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for contacting us!</p>';
} else {
if (empty($errors) === false) {
echo '<ul>';
foreach($errors as $error) {
echo '<li>', $error, '</li>';
}
echo '</ul>';
}
?>
<form action="" method"post">
<p>
<label for "name">Name:</label><br />
<input type="text" name"name" id="name" <?php if (isset($_POST['name']) === true) { echo strip_tags($_POST['name']); } ?> >
</p>
<p>
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" <?php if (isset($_POST['email']) === true) { echo strip_tags($_POST['email']); } ?>>
</p>
<p>
<label for "message">Message:</label><br />
<textarea name"message" id="message"><?php if (isset($_POST['message']) === true) { echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
<input type="submit" value="Submit" >
</p>
</form>
<?php
}
?>
</body>
</html>
You need an SMTP server set up on localhost.

How do I validate this email contact form with PHP?

Link to website: http://www.leonardpfautsch.com/contact.php
How do I make my contact form validated only using PHP? I want to be able to have error messages directly under the text field that has an error. For each text field, I do not want multiple errors to show up at once. If you submit the form with nothing in the fields, you see that under name and email two errors show up for each. I want the errors to show up only once due to some type of specifications. Right now I think I am on the right track. However, the code below does not have the email being sent. I am very new to PHP. If anybody could help me, I would really appreciate it.
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))){
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($name === '') {
$name_error_1 = '<div style="color:red;"> Name is a required field! </div>';
}
if ($email === '') {
$email_error_1 = '<div style="color:red;"> Email is a required field! </div>';
}
if ($subject === '') {
$subject_error = '<div style="color:red;"> Subject is a required field! </div>';
}
if ($message === '') {
$message_error = '<div style="color:red;"> Message is a required field! </div>';
}
if (isset($email) && (filter_var($email, FILTER_VALIDATE_EMAIL) === false)){
$email_error_2 = '<div style="color:red;"> The email address must be real! </div>';
}
if (ctype_alpha($name) === false) {
$name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';
}
/*Main way that mail works*/
if (empty($errors) === true) {
/*Where_mail_goes_to, Subject, Body_text, Who_email_is_from*/
mail('email_address', $subject, "From " . $name . "\r\r" . $message, 'From: ' . $email);
/*Shows up in the URL if the message has been sent*/
header('Location: contact.php?sent');
exit();
}
} //end of main if
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<span class="label">Name</span><br/>
<?php if (isset($name_error_1)) { echo $name_error_1; } ?>
<?php if (isset($name_error_2)) { echo $name_error_2; } ?>
<input type="text" class="textfield" name="name" size="50" maxlength="50" <?php if (isset($_POST['name']) === true) { echo 'value="', strip_tags($_POST['name']), '"'; } ?> > <br/>
<span class="label">Email</span><br/>
<?php if (isset($email_error_1)) { echo $email_error_1; } ?>
<?php if (isset($email_error_2)) { echo $email_error_2; } ?>
<input type="text" class="textfield" name="email" size="50" maxlength="50" <?php if (isset($_POST['email']) === true) { echo 'value="', strip_tags($_POST['email']), '"'; } ?> > <br/>
<span class="label">Subject</span><br/>
<?php if (isset($subject_error)) { echo $subject_error; } ?>
<input type="text" class="textfield" name="subject" size="50" maxlength="50" <?php if (isset($_POST['subject']) === true) { echo 'value="', strip_tags($_POST['subject']), '"'; } ?> > <br/>
<span class="label">Message</span><br/>
<?php if (isset($message_error)) { echo $message_error; } ?>
<textarea rows="5" cols="50" name="message" id="textarea" maxlength="500"><?php if (isset($_POST['message']) === true){ echo $_POST['message'];}?></textarea><br/>
<input type="submit" value="Send" id="submit" name="action">
</form>
You could create an array of errors for each field and display just the first error added to it.
<?php
$email_errors = array();
if ($email == '')
{
$email_errors[] = 'First error';
}
if (more_email_checks($email) == false)
{
$email_errors[] = 'Second error';
}
?>
...
<span class="label">Email</span><br />
<?php echo array_shift($email_errors); ?>
To know whether to send e-mails or not, you could do something like this:
$errors_found = 0;
if (check_email($email) == false)
{
$email_error = 'Error message';
$errors_found++;
}
...
if ($errors_found == 0)
{
mail(...);
}
You can do it by using the elseif check
<span class="label">Email</span><br/>
<?php if (isset($email_error_1))
{
echo $email_error_1;
}
elseif(isset($email_error_2)) {
echo $email_error_2;
} ?>
Also move this line after the last validation check
if (ctype_alpha($name) === false) {
$name_error_2 = '<div style="color:red;"> Your name must only contain letters! </div>';
}
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
you can by this code for name
<?
$message = "<div style = 'color :red ' /> ;
if (isset(name == '' ) {
echo $message
}
?>
this is name php vaildation but yo can create js
Change your email validation to:
<span class="label">Email</span><br/>
<?php if (isset($email_error_1))
{
echo $email_error_1;
}else if(isset($email_error_2)) {
echo $email_error_2;
} ?>
same if else can be applied to all the fields with multiple validation conditions.
and then move your error array just above the email condition check:
<?php
$errors = array($name_error_1, $name_error_2, $email_error_1, $email_error_2, $subject_error, $message_error);
//and change your mail function as:
$to = 'email_address';
$headers = $headers .= 'From: $name <$email>';
mail($to, $subject, $message, $headers);
?>
Also perform a check on the control, if you have filled the form completely then it should come to the mail function, I mean just check the if condition, in case you have some issue with the condition, try to put an echo inside if statement(which is responsible for sending email), and if that echo statement executes then mail should work.
:)

Contact form issues

I'm having all kinds of issues with contact form. When I test on my home server everything runs smoothly. Once I upload it online it doesn't work. First there were problems with headers and now apparently "This web page has a redirect loop".
Here's my code. Please advice me what to do.
Thanks.
<?php
// Title: Contact Form - Dolce Forno GB
// Updated: 5/9/2012
//Validation code
if (!empty($_POST)) {
$errors = array();
//variables
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//All field are required
if (empty($name) === true || empty($email) === true || empty($phone) === true || empty($subject) === true || empty($message) === true ){
$errors[] = 'Please fill in all the fields.';
}
else {
//This regex allows only: a-z,A-Z, space, comma, full stop, apostrophe, dash
if (!preg_match("/^[a-zA-Z\s,.'-]+$/", $name)) {
$errors[] = 'Invalid name.';
/*die ("Invalid name."); */
}
//var_filter php function
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Invalid email address.';
}
//This regex allows only: 0-9, space, dash, brackets, min-max length: 10-15
if (!preg_match("/^[0-9\s]{10,15}$/", $phone)){
$errors[] = 'Invalid phone number.';
}
}
}
if (empty($errors)) {
//send email
mail('info#dolcefornogb.com', 'Contact Form', $subject, 'Message:' . $message,'From: ' . $name . $email . $phone);
header('Location:mail.php?sent');
exit ();
}
print_r($errors);
?>
<DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for contacting us!</p>';
}
else {
if (!empty($errors)){
echo '<ul>';
foreach ($errors as $error){
echo '<li>', $error,'</li>';
echo '</ul>';
}
}
?>
<form action="" method="post">
<p> <label for="name">Name
<span class="small">Add your name </span></label>
<input type="text" name="name" id="name"
<?php
if(isset($_POST['name']) === true){
echo 'value="', strip_tags($_POST['name']),'"';
}
?>
>
</p>
<p> <label for="email">E-mail address
<span class="small"> Add your e-mail</span></label>
<input type="text" name="email" id="email"
<?php
if(isset($_POST['email']) === true){
echo 'value="', strip_tags($_POST['email']),'"';
}
?>
>
</p>
<p><label for="phone">Phone<span class="small"> Add your phone number</span></label>
<input type="text" name="phone" id="phone"
<?php
if(isset($_POST['phone']) === true){
echo 'value="', ($_POST['phone']),'"';
}
?>
>
</p>
<p><label for="suject">Subject </label>
<input type="text" name="subject" id="subject"
<?php
if(isset($_POST['subject']) === true){
echo 'value="', strip_tags($_POST['subject']),'"';
}
?>
>
</p>
<p><label for="message">Message:</label>
<textarea name="message" id="messgae" rows="10" cols="50">
<?php
if(isset($_POST['message']) === true){
echo strip_tags($_POST['message']);
}
?></textarea>
</p>
<p><label for="call">Request Phone Call</label>
Yes:<input type="radio" value="Yes" name="call">
No:<input type="radio" value="No" name="call">
</p>
<p class="buttons">
<input type="submit" value="Send"> <input type="reset" value="Clear">
</p>
</form>
<?php
}
?>
Try redirecting to a different page with just the success message in it.
i.e. replace
header('Location:mail.php?sent');
with
header('Location:mail-success.php?sent');
Then get rid of (move to the new page)
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for contacting us!</p>';
}
Also try adding 303 status to the header call
http://www.electrictoolbox.com/php-303-redirect/

PHP mail issues using javascript validation

I am trying to send an email from the same PHP page after Javascript validation but have some issues sending email. Can someone help me out with this?
Here is the JavaScript code:
<!-- -->
<script type="text/javascript">
function checkCheckBoxes() {
if (document.form.inforequired.checked == true)
{
if (document.form.option1.checked == false && document.form.option2.checked == false && document.form.option3.checked == false && document.form.option4.checked == false && document.form.option5.checked == false && document.form.option6.checked == false && document.form.option7.checked == false && document.form.option8.checked == false && document.form.option9.checked == false && document.form.option10.checked == false)
{
document.getElementById('error').innerHTML = "Please choose your options.";
return false;
}
if (document.form.option8.checked == true || document.form.option9.checked == true || document.form.option10.checked == true)
{
if (document.form.address.value == "" )
{
document.getElementById('erroraddress').innerHTML = "Please enter your address.";
return false;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
else
{
document.forms["form"].submit();
return true;
}
}
</script>
<!-- Email -->
Here is the PHP code to send email in the else part.
<div id="content">
<h1 id="formSpacingHeading"> Visitor Information </h1>
<?if( !isset($_POST['submit'])):?>
<form id="form" name="form" onsubmit="return checkCheckBoxes();" action="" method="POST">
<p id="formSpacing"><label for="username" class="iconic user" > Name <span class="required">*</span></label> <input type="text" name="username" id="username" required="required" placeholder="Enter your name" /></p>
<p id="formSpacing"><label for="usermail" class="iconic mail-alt"> E-mail address <span class="required">*</span></label> <input type="email" name="usermail" id="usermail" placeholder="Enter your E-mail ID" required="required" /> </p>
<p id="formSpacing"><label for="contactno" class="iconic link"> Contact number <span class="required">*</span></label> <input type="text" pattern="^\d\d\d\d\d\d\d\d\d\d$" name="contact" id="contactno" placeholder="Enter your contact number" required="required" /></p>
<input type="submit" name="submit" value="Submit Form" />
</form>
<?php else :
$name=$_POST['username'];
$mail=$_POST['usermail'];
$contact=$_POST['contact'];
$to = "admin#xxx.com";
$subject = "New Visitor Information";
$message = $name;
$from = $mail;
$headers = "From:" . $from;
$email = mail($to, $subject, $message, $headers);
if($email){
echo "Thank you. We will keep you updated with the latest information.";
}
else{
echo "Error processing your request. Please try again later.";
}
?>
<? endif ?>
I'm not a PHP guru, but I have programmed a few basic things in it.
I think you have your if statement all wrong.
Change..
<?if( !isset($_POST['submit'])):?>
to
<?if( !isset($_POST['submit'])){?>
Change
<?php else :
To
<?php } else {
Change
?>
<? endif ?>
to
}?>

Categories