FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help with php email





hisdivinedesign
I'm a total novice with very minor knowledge of php's most basic functions. I'm doing a website for a friend and I want their forms to be secure and look nice. I'm used to using the general form that sends info to a sep. php file and then sends you to a separate thank you page. I'm trying to do better.

I took a look at Star Wars Fanatic's web site (http://www.knrdesigns.com/contact.php) since he was on some of the posts I was reading, and I would love to be able to do a contact form the way his is set up in that if you miss inputting something it just posts a message above the form stating you made an error but keeps everything else the same.

I've read the basic tutorials and spent hours on screencasts but they all only seem to give me the most basic information that I already have.

I've already got the .php page set up with the form and it works for simple mail functionality but the echo message I have for errors takes them to a blank page with the text. I just need to know how to make it give an error message and keep the same page showing as well as making it secure.

I'm assuming the same function that allows what i'm looking for in a contact form would be the one used in uploads and other types of forms as well.

Can anyone help me?
rvec
Put all the stuff in one page. It makes it a lot easier to do validation.

Code:
<?php
$submitted = FALSE;
$errors = array();

if (isset($_GET['form'])) {
  //validate your form here and put errors in $errors like this:
  $errors[] = 'Your name isn\'t valid';

  if (!count($errors)) {
    //submit the form here or send the mail
  }
}

if (!$submitted) {
  if (count($errors)) {
    foreach ($errors as $error) {
      echo 'Error:'. $error .'<br />';
    }
  }
 echo <<<TEXT
<form action="?form=true" method="post">
<input....>
</form>
TEXT;
}
Marcuzzo
something like this?

Code:
<?php

if  ( $_POST['send'] == "send" && $_POST['name'] != "" && $_POST['age'] != "" ){
   echo "thank you for sending your details.<br />
   received name: " . $_POST["name"] . "<br /> Age: " .$_POST["age"] ;
}else{

?>

<form name="contact" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<?php
   if ( $_POST['send'] && $_POST['name'] == ""){
      echo "<span style=\"color:brown;\">please enter your name</span><br />";
   }
?>
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo $_POST['name'] ?>" /><br />

<?php
   if ( $_POST['send'] && $_POST['age'] == ""){
      echo "<span style=\"color:brown;\">please enter your age</span><br />";
   }
?>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $_POST['age'] ?>" /><br />

<input type="submit" value="send" name="send" />
</form>

<?php
}
?>
hisdivinedesign
Ok I went in and went with what I thought things were just to play around with it. I posted a contact1.php and unfortunately nothing shows up at all. No page, no nothing. Have no clue what I'm doing wrong. Here's what I did with what you provided.

body tag....
div tags (have some stuff written in html before the form) - close div tags
object tag - for an swf file - closed object tag

div style="position: absolute; width: 389px; height: 100px; z-index: 6; left:596px; top:243px" id="layer5"
<h3> Contact Us </h3>

<?php
if ( $_POST['send'] == "michelle@hisidivinedesign.net" && $_POST['name'] != "" && $_POST['email'] != "" && $_POST['subject'] != "" && $_POST['Reason'] != "" && $_POST['message'] != ""){
echo "<p class="one">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: " . $_POST["name"] . "<br /> Age: " .$_POST["email"] </p>;
}else{

?>

<form name="contact" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<?php
if ( $_POST['send'] && $_POST['name'] == ""){
echo "<span style=\"color:white;\">Please enter your name</span><br />";
}
?>
<label for="name"><p class="one">Your Name:</p></label>
<input type="text" name="name" id="name" value="<?php echo $_POST['name'] ?>" /><br /><br />

<?php
if ( $_POST['send'] && $_POST['email'] == ""){
echo "<span style=\"color:white;\">Email address is required</span><br />";
}
?>
<label for="email"><p class="one">Your Email:</p></label>
<input type="text" name="email" id="email" value="<?php echo $_POST['email'] ?>" /><br /><br />

<?php
if ( $_POST['send'] && $_POST['subject'] == ""){
echo "<span style=\"color:white;\">A subject is required.</span><br />";
}
?>
<label for="email"><p class="one">Subject:</p></label>
<input type="text" name="subject" id="subject" value="<?php echo $_POST['subject'] ?>" /><br /><br />


Reason:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name="reason">
<option value="1">General Feedback</option>
<option value="2">Advertising</option>
<option value="3">Privacy Issues</option>
<option value="4">Other</option>
</select><br>

<?php
if ( $_POST['send'] && $_POST['email'] == ""){
echo "<span style=\"color:white;\">Email address is required</span><br />";
}
?>
<label for="email"><p class="one">Your Message:</p></label>
<textarea name="message" id="message" value="<?php echo $_POST['email'] ?>" cols="30" rows="8" /></textarea><br /><br />

<input type="submit" name="submit" value="Contact Us"></p>
</form>

<?php
}
?>

div...more html etc..etc... /div
script - a script for a rotating banner blah blah /script

end body tag
end html tag

I'm posting this so you can see where my thought process is and what I think you're telling me b/c obviously i'm getting it very wrong. Maybe this will help you all to correct me? Could I be lucky for this to just be a typo i have in there somewhere that's mucking the whole thing up, I wish but I doubt it lol!






I think so but my brain just screamed out in pain. I hope you understand just how new I am at this lol. I like to make sure I understand what I'm doing though and not just copy and paste so please bare with me if you would. On my page I have the form between <form...> </form> I see where you have the <form...>tag so I'm assuming this is where I would put my input text boxes and form as I have it now but with the php self action. Currently I have it set to action="contact.php", and outside source.

1. So let me make sure I understand, the action you have listed makes the form write the i
information to itself? Also you have the php code with the if statements right into the form itself? Am I reading that correctly?

2. The if statement is what does my validation check, right? And am I correct that you have told the if statement to check multiple values, (name and age) at the same time?

3. Am I correct that the first if is telling it what to do if everything is filled out as it should be, then to send the email, print out a thank you on the same page and clear the imput fields?

I understand parts of the statement, like I know that == means it's evaluating a true or false but I don't understand where it's getting the ['send'] == "send" && . Is the ['send'] a command which tell is it email and then the "send" is where I would put the email address I want it to go to? If not, where is the line for the mail function?

Also after the break it appears you have it stating received name and then break then age. It appears you are asking it to print out received name: and then the value put into the name field and the same for age. Is that correct?

4. What is the label for="name"... do (right above the input box for the name)?

5. Lastly, it appears you have the validation check directly above each input text field. Is that correct that I would do this for each field that I have on this form?

I know my questions are probably very "duh" and I'm sorry but I am soo new at this stuff. It seems logical and to make sense but I want to be sure and also want to know exactly what it means so I can apply it to other things if I need to later on so I thank you now for your patience.
Michelle


Marcuzzo wrote:
something like this?

Code:
<?php

if  ( $_POST['send'] == "send" && $_POST['name'] != "" && $_POST['age'] != "" ){
   echo "thank you for sending your details.<br />
   received name: " . $_POST["name"] . "<br /> Age: " .$_POST["age"] ;
}else{

?>

<form name="contact" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

<?php
   if ( $_POST['send'] && $_POST['name'] == ""){
      echo "<span style=\"color:brown;\">please enter your name</span><br />";
   }
?>
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo $_POST['name'] ?>" /><br />

<?php
   if ( $_POST['send'] && $_POST['age'] == ""){
      echo "<span style=\"color:brown;\">please enter your age</span><br />";
   }
?>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $_POST['age'] ?>" /><br />

<input type="submit" value="send" name="send" />
</form>

<?php
}
?>
hisdivinedesign
Ok now for yours which looks cleaner but is much harder for me to understand.

1. what is isset? and why is it a _get instead of post?
2. What is count($error))?
3. Where you have the first error listed would I just continue to list each field and the message I want for that particular field i.e., $errors[] = Email Address required;
3. Where you have submit the form here is that where I would put the mail function and tell it to post the information from the form into the email?
4. At the bottom where you have echo and then the <form...>, are you telling it to echo the whole form?


rvec wrote:
Put all the stuff in one page. It makes it a lot easier to do validation.


Code:
<?php
$submitted = FALSE;
$errors = array();

if (isset($_GET['form'])) {
  //validate your form here and put errors in $errors like this:
  $errors[] = 'Your name isn\'t valid';

  if (!count($errors)) {
    //submit the form here or send the mail
  }
}

if (!$submitted) {
  if (count($errors)) {
    foreach ($errors as $error) {
      echo 'Error:'. $error .'<br />';
    }
  }
 echo <<<TEXT
<form action="?form=true" method="post">
<input....>
</form>
TEXT;
}
rvec
hisdivinedesign wrote:
Ok now for yours which looks cleaner but is much harder for me to understand.

1. what is isset? and why is it a _get instead of post?
2. What is count($error))?
3. Where you have the first error listed would I just continue to list each field and the message I want for that particular field i.e., $errors[] = Email Address required;
3. Where you have submit the form here is that where I would put the mail function and tell it to post the information from the form into the email?
4. At the bottom where you have echo and then the <form...>, are you telling it to echo the whole form?


http://www.php.net for all php functions you don't understand.
1. isset is used to not generate a notice if the variable isn't set(see php.net)
get is used because the variable I want there is in the url (see action argument in the form element)
2. count() is used to see if there are any errors inside the variable (see php.net)
3. Yes you can add errors to the variable just like that, just set the error if there is anything wrong and it'll show up in the form.
4. yes
sonam
Error report will not work if you make wrong codding like this one:
Code:
echo "<p class="one">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: " . $_POST["name"] . "<br /> Age: " .$_POST["email"] </p>;

You will get blank page allways. Be carreful with quotes and use slashes regulary.

Code:
echo "<p class=\"one\">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: " . $_POST["name"] . "<br /> Age: " . $_POST["email"] . "</p>";


I didn't check all mail but when you get form then see what is the next. I am post one tutorial here:
http://www.frihost.com/users/sonam/blog/vp-83950.html

Sonam
rvec
you won't get a blank page but a couple of errors (unless you have all errors off). And the more readable solution would be to use ' instead of " like this:
Code:
echo '<p class="one">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: ' . $_POST["name"] . '<br /> Age: ' .$_POST["email"] .'</p>';
sonam
rvec wrote:
And the more readable solution would be to use ' instead of " like this:
Code:
echo '<p class="one">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: ' . $_POST["name"] . '<br /> Age: ' .$_POST["email"] .'</p>';


Right, but I am following his concept. Wink

Sonam
hisdivinedesign
OK I am totally frustrated here. I still cannot get the page to display period. So let me ask some basic questions. Maybe my throught process is flawed from the start and I'm poisoning the whole apple so to speak.

1. Can I make an html page and then insert the php codes, assuming the php code is correctly written, within that html page, name it a php page and have it work?

2. Can I set up a contact form on that page and have no additional php form for it to send and process the fprm information but have it process the form information itself?

3. If I have a regular html file with form within and separate php file to process the information, can I still set up code that will cause the form to redisplay with information in tact in the case of a missed field or invalid entry AND also to have it display blank form with a thank you message upon sucessful submission?

Man I feel like such an idiot when it comes to this stuff!






First, I am more than happy to change my concept to something that is correct or more correct I guess lol. So I went in and followed rvec's solution. I put the ' in place of the " on anything between the ?php tag. I'm assuming what's not between the ?php tags is read like standard html and didn't need any special quotes etc. Still getting no page showing at all though just like before. I'm going to go through sonam's tutorial and I'll let you know how that goes.
Thank you
Michelle



sonam wrote:
rvec wrote:
And the more readable solution would be to use ' instead of " like this:
Code:
echo '<p class="one">Thank you for contacting us. Someone will respond as soon as possible.<br />
received name: ' . $_POST["name"] . '<br /> Age: ' .$_POST["email"] .'</p>';


Right, but I am following his concept. Wink

Sonam
sonam
Quote:
I put the ' in place of the " on anything between the ?php tag.


I think this is again wrong. We are talking only for echo function not for all between php tags. You can use double quotes or single quotes for echo function. Double quotes allow variables. Single quotes not, it is strict mode (sorry if I not use right computer language). You cannot use same quotes in between echo quotes. What they mean.

Code:
echo "This is produce "blank" page";


Using twice double quotes (without slashes) will produce blank page. The same is if you use single quotes:

Code:
echo 'This is produce 'blank' page';


Now in the game come variables. This is working because you can use variables between double quotes:

Code:
$email = "my_mail@myhost.com"
echo "My mail is $email";


But this is not working because you will get $email like result instead of my_mail @ myhost.com:
Code:
$email = "my_mail@myhost.com"
echo 'My mail is $email';


End on the end when html is comming in the game the problem is bigger because html code need double quotes. This is not working without slashes because four double quotes produce error:
Code:
$email = "my_mail@myhost.com"
echo "My mail is <p class="blue">$email</p>";


But if you try here to use single quotes for echo this will not work, too.
Code:
$email = "my_mail@myhost.com"
echo 'My mail is <p class="blue">$email</p>';


Why? Becaue variable is inside single quotes and you will get result:
My mail is $email; instead of My mail is my_mail@myhost.com.

On the end, how rvec sugest, for echo html the best way is using single quotes and separated variables (note: you need to use dot and variables without quotes).
Code:
$email = "my_mail@myhost.com"
echo 'My mail is <span class="blue">' . $email . '</span>';



BTW, you cannot use php without php extension (myform.php). Confused OK, you can but you must to do something in htaccess. And if you want to test your scripts on home computer (localhost) you need to instal php and server.

Sonam
hisdivinedesign
Sonam. I completely scrapped mine as I was obviously going nowhere with it. I followed your tutorial, at least I think. With this tutorial everything seems to be working fine except:
a. It does not actually send the mail even though I get the "Your email has been
sent...." message.

b. When I do cause an error, such as I input an improper email address, it clears all the fields and makes me start all over instead of retaining the information that has already been entered. How do I make it retain the information that's already been entered when it prints an error message?

Can you tell me what I screwed up that it's not actually sending the mail and what I would need to add to make it not clear the form?
Thank you
Michelle



The mailform.php:
Code:
<?php
session_start();

// prepare spam check
$id = session_id();
$_SESSION['check'] = session_id();
$id = str_split($id, 5);
foreach($id as $key => $numIn) {
$res = strlen($numIn);
if($res!=5) {
 unset($id[$key]);
}
}
shuffle($id);
$_SESSION['spam'] = $id['0'];

//messages part
switch(@$_SESSION['message']) {
case "1": $message = "Sorry, some error has occured! <br />"; break;
case "2": $message = "Please complete all fields. <br />"; break;
case "3": $message = "Your validation key is not correct. <br />"; break;
case "4": $message = "Your email address is not valid. <br />"; break;
case "5": $message = "Your e-mail has been sent successfully. <br />"; break;
 
default: "";
}
unset($_SESSION['message']);
?>
<html>
<head>
<title>Mail Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">

<form name="form1" method="post" action="mailsender.php">

<?php
// display right message
echo @$message;
?>

Your Name:<br />
<input type="text" name="name">
<br /><br />

Your E-mail:<br />
<input type="text" name="email">
<br /><br />

Subject:<br />
<input type="text" name="subject">
<br /><br />

<select name="reason">
   <option value="1"></option>
   <option value="2">General Feedback</option>
   <option value="3">Advertising</option>
   <option value="4">Privacy Issues</option>
</select><br><br/>

Your message:<br />
<textarea name="message" rows="5"></textarea>
<br /><br />

Type:
<h2>
<?php echo $id['0']; ?>
<input type="text" name="spam" maxlength="5">
</h2>
<br />

<input type="submit" name="sent" value="Send">

</form>
</body>
</html>




The mailsender.php:
Code:
<?php
session_start();
$id = session_id();
$return = "mailform.php";
$search = array("name", "email", "subject", "reason", "message", "spam", "sent"); 
if($id != $_SESSION['check']) {
$_SESSION['message'] = 7;
header("Location:$return");
exit;
}
foreach($_POST as $key => $val) {
if(array_search($key, $search) === FALSE) {
$_SESSION['message'] = 1;
header("Location:$return");
exit;
}
if($val == "") {
$_SESSION['message'] = 2;
header("Location:$return");
exit;
}
}
if($_POST['spam'] != $_SESSION['spam'] || strpos($id, $_POST['spam']) === FALSE) {
$_SESSION['message'] = 3;
header("Location:$return");
exit;
}

if(!eregi("^[a-z0-9._-]+@+[a-z0-9._-]+\.+[a-z]+$", $_POST['email'])) {
$_SESSION['message'] = 4;
header("Location:$return");
exit;
}

if(strpos($_POST['sent'], "Send") === FALSE) {
$_SESSION['message'] = 1;
header("Location:$return");
exit;
} else {
$reply = $_POST['email'];
$subject = htmlspecialchars($_POST['subject']);
$subject = trim($subject);
$message = htmlspecialchars($_POST['message']);
$message = trim($message);

$to = "myemail@mydoman.net"; // type here right email

mail($to, $subject, $name, $reason, $message, $email,  "From: $reply\r\n"."Reply-To: $reply\r\n");
$_SESSION['message'] = 5;
header("Location:$return");
exit;
}
?>
sonam
This what do you do is looking good but I am little bit update script for your needs. I am insert check for your drop down list (it cannot to be empty - option 1) and form now keep inserted data except spam control. When is mail sent and form is cleaned. Here you can read code for both mailform.php and mailsender.php. You MUST change definiton for $to (very near to the end of mailsender.php), write there your real email, and you must be carefull if your mailsender.php is not in same folder like mailform.php. In that case you must change action definition in the form on the right path. Wink

http://www.phoenix.frihost.net/zadnji/hisdivinedesign

Sonam
Related topics
What to do BEFORE making a new topic!
[HELP] PHP -> Scripting
Needing a Bit Og Help {PHP}!
need some help - php switch code
php dropdown lists with if statements or switch
neep help making email form that accepts html tags
PHP email varification saying invalid email
PHP echoing need help with email post form.
[HELP] PHP (and some MyBB) Administration
Error with MySQL and PHP script in my CMS
Session directory permissions issue
selecçao de emails no php . email selection on php
[Help] php script to post to twitter
htaccess pattern for an email
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.