1

I'm very, very new to PHP and I'm still trying to learn it. But I have a problem right now that I need to fix immediately so I am seeking any kind of help for it.

I'm trying to make a contact form which can send an message by emailing it.

Here is my html:

<form method="POST" action="?" >
<div id="header2">
<p id="heading">CONTACT ME</p>
<p> Send your inquiries or comments here and I'll get back to you right away.</p>
</div>

<label for="name">Your Name </label>
<input type="text" name="name" placeholder="Juan Dela Cruz" width="100px;">

<label for="email">  Email Address </label>
<input type="text" name="email" placeholder="yourname@email.com">

<label for="contact">  Contact Number </label>
<input type="text" name="contact" placeholder="Mobile or Landline">



<label for="message">  Message </label>
<textarea type="text" name="message" rows="10" cols="25" placeholder="Your Message"></textarea>

<input type="submit" value="Send Message" id="submit">  
<input type="reset" value="Reset Fields" id="reset">
<p id="feedback"> <?php echo $feedback; ?> </p>
<div id="socialcontact">
<h4> CJSS </h4>
    <p> Address 
    </p> 
    <a href="#"> <img src="images/fb-2.png" alt="Find me on Facebook"/> </a> <a href="#"> <img alt="Follow me on Twitter" src="images/twitter-2.png" /> <a href="#"> <img alt="Connect with me on Linked In" src="images/linkedin-2.png" /> <a href="#"> <img alt="Check me out on Behance" src="images/behance-2.png" />

</div>      

</form>

Now here is my php:

<?php

$to = 'myemail@gmail.com'; 
$subject = 'An inquiry from your website';

$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$body = $_POST['message'];

$message = <<<EMAIL

$name
$email
$contact

$message

EMAIL;

$header = "From: $email";

if($_POST){
if($name == '' || $email == '' || $message == ''){
$feedback = 'Kindly fill our all the (*)necessary fields';
}else{ mail($to, $subject, $body, $header);
$feedback = 'Thank you, email sent. I will get back to you as soon as I can.';}

}

?>

I put this php on top of everything.

When I preview it through my testing server, it shows error messages:

( ! ) Notice: Undefined index: email in C:\wamp\www\projectlevity4\contact.php on line 8 Call Stack

Time Memory Function Location

1 0.0007 258368 {main}( ) ..\contact.php:0

( ! ) Notice: Undefined index: contact in C:\wamp\www\projectlevity4\contact.php on line 9 Call Stack

Time Memory Function Location

1 0.0007 258368 {main}( ) ..\contact.php:0

( ! ) Notice: Undefined index: message in C:\wamp\www\projectlevity4\contact.php on line 10 Call Stack

Time Memory Function Location

1 0.0007 258368 {main}( ) ..\contact.php:0

( ! ) Notice: Undefined variable: message in C:\wamp\www\projectlevity4\contact.php on line 19 Call Stack

Time Memory Function Location

1 0.0007 258368 {main}( ) ..\contact.php:0

What should I do to fix this? I'm trying to beat a deadly deadline, I hope you can help me out.

Thanks

cjss
  • 11
  • 1
  • 2
  • Your code is prone for mail header splitting and allowing attackers to sent SPAM. – MrTux Sep 06 '14 at 16:32
  • 1
    Since your php code is in the same file as your html form on initial page load none of your `$_POST` keys are set. Wrap you php code with `if(isset($_POST['name']) && !empty($_POST['name']) ) { ...[your php code]... }` – Sean Sep 06 '14 at 16:35
  • thank you for the input MrTux. I am aware that these codes are pretty novice, I will look into improving it. However, I need to make this work first for the time being and improve on it later on. Any suggestions? Thanks – cjss Sep 06 '14 at 16:38
  • Hi @Sean, it works! But there is still an error message on the feedback: – cjss Sep 06 '14 at 16:52
  • Then check if it is set before using -> `

    `
    – Sean Sep 06 '14 at 17:25

1 Answers1

1

In HTML:

<input type="submit" value="Send Message" id="submit" name="submit">  

in PHP:

if(isset($_POST['submit'])){
    // your code
}
Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64
  • Hi @DiKey! This works thanks! But there is still an error on the feedback: it says: Notice: Undefined variable: feedback in C:\wamp.................... – cjss Sep 06 '14 at 16:54
  • Put PHP code before HTML and add $feedback = ''; – Daniel Kucal Sep 06 '14 at 17:06
  • yes, $feedback = 'Thank you, email sent. I will get back to you as soon as I can.'; is already on top of the html. I'm trying to have a result message once the submit button is clicked. Here it is:

    – cjss Sep 06 '14 at 17:15