PDA

View Full Version : Contact form issues using html and php mixed.



kwood30
11-03-2016, 02:52 PM
I have mixed and matched code to great a contact form to my own specification and I have managed to sort out most errors myself but just can't seem to see what I have done wrong.

The echo $result variable states that its undefined, so that wont work when the form is submitted to give the correct message,

then when i test to see if my error messages work, i just end up with a page stating: Undefined index: email in on line 30 and Undefined variable: human in on line 47

Can anyone offer an extra pair of eyes and assistance please. Thank you.

Form coding:


<form id="contacts-form" method="post" action="php/mailer test.php">

<fieldset>

<div class="field">
<label>Your Name:</label>
<input name="name" type="text" placeholder="First & Last Name">
<?php if (isset($errName)) {
echo '<p class="text-danger">'.$errName.'</p>';
}?>
</div>

<div class="field">
<label>Your E-mail:</label>
<input name="mail" type="email" placeholder="example@domain.com">
<?php if (isset($errEmail)) {
echo '<p class="text-danger">'.$errEmail.'</p>';
}?>
</div>

<div class="field">
<label>Phone Number:</label>
<input name="number" type="text" placeholder="Mobile or Home number">
<?php if (isset($errNumber)) {
echo '<p class="text-danger">'.$errNumber.'</p>';
}?>
</div>

<div class="field">
<label>Subject:</label>
<input name="subject" type="text" placeholder="Wedding, Birthday etc">
<?php if (isset($errSubject)) {
echo '<p class="text-danger">'.$errSubject.'</p>';
}?>
</div>

<div class="field">
<label>Your Message:</label>
<textarea name="message" cols="1" rows="1" placeholder="Write your message here!"></textarea>
<?php if (isset($errMessage)) {
echo '<p class="text-danger">'.$Message.'</p>';
};?>
</div>

<div class="field">
<label for="human" >2 + 3 = ?</label>
<input type="text" id="human" name="human" placeholder="Your Answer">
<?php if (isset($errHuman)) {
echo '<p class="text-danger">'.$errHuman.'</p>';
}?>
</div>

<div class="button">
<input type="Submit" name="Submit" value="Send your message!"></input></div>

<div class="field">
<?php echo $result; ?>
</div>


</fieldset>

</form>

PHP running script:


<?php

if(isset($_POST['Submit'])) {
$youremail = 'example@domain.com';
$fromsubject = 'Website enquire';
$name = $_POST['name'];
$number = $_POST['number'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Masage recived from'.$fromsubject.' Contact Page';
$body = $fromsubject.'

The person that contacted you is '.$name.'
E-mail: '.$mail.'
Phone number: '.$number.'
Subject: '.$subject.'

Message:
'.$message.'

|---------END MESSAGE----------|';


if (!$_POST['name']) {
$errName = 'Please enter your name';
}

if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}

if (!$_POST['number']) {
$errNumber = 'Please enter your mobile or home number ';
}

if (!$_POST['subject']) {
$errSubject = 'Please enter your subject';
}


if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}

if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}

if (!$errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body));

$result='<div class="alert alert-success">Thank You! I will be in touch shortly</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}

?>

Beverleyh
11-03-2016, 05:27 PM
Very quickly as I'm on mobile...

Try defining the $result variable at the top of the page that contains the form. $result = ''; should hopefully do it.

For the email error on line 30, I think it's because you're mixing up 'mail' and 'email' - your name attribute's value in the form is 'mail', and it's $_POSTed as 'mail' too at the top of the processing script, but further down it changes to 'email'.

And the form data from the 'human' field isn't even $_POSTed to the processing script, yet an undefined variable for $human is being used in the !==5 if statement later on.

kwood30
11-03-2016, 06:57 PM
Thank you for your response.

Its amazing how and extra pair of eyes help. I have changed the mail around (not sure why i did that) and now that variable is not showing up anymore. I am yet to test it server side to see if it sends it to an email, but as the errors are not showing, will for now, persume it works.

As for the $result, I have added what you said, but am still receiving the error for it as an Undefined variable.

Here is my current update code :


<form id="contacts-form" method="post" action="php/mailer test.php">

<fieldset>

<div class="field">
<label>Your Name:</label>
<input name="name" type="text" placeholder="First & Last Name">
<?php if (isset($errName)) {
echo '<p class="text-danger">'.$errName.'</p>';
}?>
</div>

<div class="field">
<label>Your E-mail:</label>
<input name="email" type="email" placeholder="example@domain.com">
<?php if (isset($errEmail)) {
echo '<p class="text-danger">'.$errEmail.'</p>';
}?>
</div>

<div class="field">
<label>Phone Number:</label>
<input name="number" type="text" placeholder="Mobile or Home number">
<?php if (isset($errNumber)) {
echo '<p class="text-danger">'.$errNumber.'</p>';
}?>
</div>

<div class="field">
<label>Subject:</label>
<input name="subject" type="text" placeholder="Wedding, Birthday etc">
<?php if (isset($errSubject)) {
echo '<p class="text-danger">'.$errSubject.'</p>';
}?>
</div>

<div class="field">
<label>Your Message:</label>
<textarea name="message" cols="1" rows="1" placeholder="Write your message here!"></textarea>
<?php if (isset($errMessage)) {
echo '<p class="text-danger">'.$Message.'</p>';
};?>
</div>

<div class="field">
<label for="human" >2 + 3 = ?</label>
<input type="text" id="human" name="human" placeholder="Your Answer">
<?php if (isset($errHuman)) {
echo '<p class="text-danger">'.$errHuman.'</p>';
}?>
</div>

<div class="button">
<input type="Submit" name="Submit" value="Send your message!"></input></div>

<div class="field">
<?php echo $result; ?>
</div>


</fieldset>

</form>



<?php

$result = '';

if(isset($_POST['Submit'])) {
$youremail = '';
$fromsubject = 'Website enquire';
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = $youremail;
$mailsubject = 'Masage recived from'.$fromsubject.' Contact Page';
$body = $fromsubject.'

The person that contacted you is '.$name.'
E-mail: '.$email.'
Phone number: '.$number.'
Subject: '.$subject.'

Message:
'.$message.'

|---------END MESSAGE----------|';


if (!$_POST['name']) {
$errName = 'Please enter your name';
}

if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}

if (!$_POST['number']) {
$errNumber = 'Please enter your mobile or home number ';
}

if (!$_POST['subject']) {
$errSubject = 'Please enter your subject';
}


if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}

if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}

if (!$errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body));

$result='<div class="alert alert-success">Thank You! I will be in touch shortly</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}

?>

Beverleyh
11-04-2016, 06:29 AM
As for the $result, I have added what you said, but am still receiving the error for it as an Undefined variable.
You misunderstood....
Try defining the $result variable at the top of the page that contains the form

kwood30
11-04-2016, 10:32 AM
Ah sorry, yes I miss read what you said.

Thank you I put it in the <head> tag on my form page an the error has now gone.

The trouble I am now facing is that when i execute the submit button it just goes to a blank page instead of running the script as designed, ie if i don't fill out anything instead of it stating under each section, it goes straigh to a blank page.

What am I missing? Do i need to link the php in my <head> tag aswell as it being linked in the action= of my form? Sorry to be a pain, I always seem to get issues when I try and right my own script to how i want it.

DyDr
11-04-2016, 02:00 PM
For these two pieces of code to work, they would have to be on the same page. Otherwise, none of the variables for the error and result messages, that are being set in the form processing code, will exist where they are used in the form code.

Your code should have the php form processing code at the top, before you start to output anything for the html document. Defining the $result variable would come before the php form processing code so that when it actually runs and assigns a message to $result, it will have that value when the html document is output.

kwood30
11-04-2016, 05:11 PM
Thank you, I have put all that code in the <head> of my contact page and it is now working, but with a new error and I don't know why. I am getting Undefined variable error for this line of code:


if (!$errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman)

The complete section of code as you will see above in my previous message. To behonest I cannot see that it is wrong, so don't understand. Surley this is stating that if there is no errors the send the form??

DyDr
11-04-2016, 06:08 PM
The place where you found this style of coding would have had a line like the following, to initialize those variables, before the start of the form processing code -

$errName = $errEmail = $errNumber = $errSubject = $errMessage = $errHuman = '';

kwood30
11-04-2016, 08:45 PM
To be honest, I have looked at many different types of codes and tried to mix and match to my own specification, and I am still really new to all this, as I am also trying to teach myself Javascript too.

So have i got this right, what yours saying is that the line of code you have shown needs to be placed at the top of my <php> script before if(isset($_POST['Submit'])) or in a seperate tag on its own like the $result variable?

kwood30
11-05-2016, 10:18 AM
I have worked it out so all sorted. Thank you all for your help and input