Jump to content

CONTACT FORM


khujo56

Recommended Posts

Hi guys

 

i created a contact form in html and a code in php to send the info to my e-mail account. the problem i have is that i want an error to show if nothing or some of the info is not filled in when i press the submit button. if i click the submit button with nothing filled out, it says the info has been sent to my e-mail account. can anyone look at my code and see what i'm doing wrong. below is the php code.

 

<?php

if(isset($_POST['submit'])) {

$to = "jnr1975@hotmail.com";

$subject = "Contact Info";

$name_field = $_POST['name'];

$email_field = $_POST['email'];

$message = $_POST['message'];

 

$errors = '';

 

if ( empty($name_field) ) $errors .= 'fill out name<br/>';

if ( empty($email_field) ) $errors .= 'fill out email<br/>';

if ( empty($message) ) $errors .= 'fill out message<br/>';

 

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

if ( !empty($errors) ) {

echo $errors;

} else {

mail($to, $subject, $body);

echo "Data has been submitted to $to!";

}

?>

 

Link to comment
Share on other sites

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

<?php

if(isset($_POST['submit']))

{

$to = "jnr1975@hotmail.com";

$subject = "Contact Info";

$name_field = $_POST['name'];

$email_field = $_POST['email'];

$message = $_POST['message'];

 

$errors = '';

 

if (!isset($name_field))

{

$errors .= 'fill out name';

}

if (!isset($email_field))

{

$errors .= 'fill out email';

}

if (!isset($message))

{

$errors .= 'fill out message';

}

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

if ( !empty($errors) )

{

echo $errors;

}

else

{

if(mail($to, $subject, $body)

{

echo "Data has been submitted to $to!";

}

}

}

?>

 

practice this way of coding

Link to comment
Share on other sites

<?php

$var = 0;

 

// Evaluates to true because $var is empty

if (empty($var)) {

    echo '$var is either 0, empty, or not set at all';

}

 

// Evaluates as true because $var is set

if (isset($var)) {

    echo '$var is set even though it is empty';

}

?>

 

see the diff. any way it base on the needs

Link to comment
Share on other sites

Hi Teng

 

Actually i got the code from your prior post.

here it is below.

here is the page where the form is on.

http://www.iheartvacation.com/index-4.html

 

here is the code

 

<?php

if(isset($_POST['submit']))

{

  $to = "jnr1975@hotmail.com";

  $subject = "Contact Info";

  $name_field = $_POST['name'];

  $email_field = $_POST['email'];

  $message = $_POST['message'];

 

  $errors = '';

 

  if (!isset($name_field))

  {

      $errors .= 'fill out name';

  }

  if (!isset($email_field))

  {

    $errors .= 'fill out email';

  }

  if (!isset($message))

  {

      $errors .= 'fill out message';

  }

  $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

  if ( !empty($errors) )

  {

      echo $errors;

  }

  else

  {

      if(mail($to, $subject, $body)

      {

        echo "Data has been submitted to $to!";

      }

  }

}

?>

Link to comment
Share on other sites

  $to = "jnr1975@hotmail.com";

  $subject = "Contact Info";

  $name_field = $_POST['name'];

  $email_field = $_POST['email'];

  $message = $_POST['message'];

 

  $errors = '';

 

  if (!isset($name_field))

  {

      $errors .= 'fill out name';

  }

  if (!isset($email_field))

  {

    $errors .= 'fill out email';

  }

  if (!isset($message))

  {

      $errors .= 'fill out message';

  }

  $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

  if ( !empty($errors) )

  {

      echo $errors;

  }

  else

  {

      if(mail($to, $subject, $body))

      {

        echo "Data has been submitted to $to!";

      }

  }

try that first i remove the condition isset post coz that maybe coz the blank any way try that

and tell me what happen coz it works fine here

Link to comment
Share on other sites

i see the site and its still blank mmm!!

 

try to echo something anywhere on that page and see if there will be an output

because that works fine on me

 

say echo 'test 101'; then check if that will appear i view the source of the site ant thats blank alco so try echoing something that will help you debug

Link to comment
Share on other sites

$to = "jnr1975@hotmail.com";

  $subject = "Contact Info";

  $name_field = $_POST['name'];

  $email_field = $_POST['email'];

  $message = $_POST['message'];

  $errors = '';

 

  if (!isset($name_field))

  {

      $errors .= 'fill out name';

  }

  if (!isset($email_field))

  {

    $errors .= 'fill out email';

  }

  if (!isset($message))

  {

      $errors .= 'fill out message';

  }

  $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

  if ( !empty($errors) )

  {

      echo $errors;

  }

  else

  {

      if(mail($email_field , $subject, $message))

      {

        echo "Data has been submitted to $to!";

      }

  }

 

that will work now ^^

 

if(mail($to, $subject, $body))// you dont change this  $to, $subject, $body this variable are not set

 

 

 

Link to comment
Share on other sites

i know y you dont get the message

 

$to = "jnr1975@hotmail.com";

  $subject = "Contact Info";

  $name_field = $_POST['name'];

  $email_field = $_POST['email'];

  $message = $_POST['message'];

  $errors = '';

 

  if (!isset($name_field))

  {

      $errors .= 'fill out name';

  }

  if (!isset($email_field))

  {

    $errors .= 'fill out email';

  }

  if (!isset($message))

  {

      $errors .= 'fill out message';

  }

  $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 

 

  if ($errors!='')

  {

      echo $errors;

  }

  else

  {

      if(mail($email_field , $subject, $message))

      {

        echo "Data has been submitted to $to!";

      }

  }

 

just paste that

 

 

Link to comment
Share on other sites

Try this:

 

if(isset($_POST['submit']))

{

if ( empty($_POST['name']) )

{

exit("You must fill in your name!");

}

if ( empty($_POST['email']) )

{

exit("You must fill in your email address!");

}

if ( empty($_POST['message']) )

{

exit("You must write a message to send!");

}

}

 

Or, if you want the form to appear under the error message than echo it, like so:

 

if(isset($_POST['submit']))

{

    if ( empty($_POST['name']) )

    {

        echo 'You must fill in your name!';

    }

    if ( empty($_POST['email']) )

    {

        echo 'You must fill in your email address!';

    }

    if ( empty($_POST['message']) )

    {

        echo 'You must write a message to send!';

    }

}

 

 

You can use echo or exit, whichever you prefer.

 

I am a PHP newbie too, but if you're unsure how to use echo or exit look it up on PHP.net :)

Link to comment
Share on other sites

thanks that helped. is there a way you can write a message that is has been posted, then have it re-directed to another page..like back to the main page ?

 

You would have to simply echo the variable you stated for the posted message in order to display it.

 

example:

 

echo $message;

 

Redirection to another page can be done using header.

 

example:

 

header('Location: http://www.placeiwanttoredirectto.com/');

 

this must be placed before the <html> tag in order to work properly though.

Link to comment
Share on other sites

Try this.

<?php
if(isset($_POST['name']) && empty($_POST['name']))
{
     $error[] = "Post your name";
}
if(isset($_POST['email']) && empty($_POST['email']))
{
     $error[] = "Post your name";
}
if(isset($_POST['message']) && empty($_POST['message']))
{
     $error[] = "Post your name";
}
if(is_array($error))
{
    echo("errors:<br>");
    foreach($error as $x => $i)
    {
        echo("- ".$i."<br>");
    }
}
else
{
     // submit mail stuff here!
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.