Jump to content

Recommended Posts

Hey all,

 

I'm the definition of a PHP noob...I know my way around xhtml/css just fine and can design, but have only studied a little PHP code I've seen.

 

Onto my problem: I'm doing a website for a friend who has a piano moving company. He wanted a 'contact' form on his rates page, so people could fill out some fields and get a custom quote.

 

I'm using this php script as a template: http://www.phpeasystep.com/phptu/8.html -- really really basic. I've subbed out the current form fields for what I want, that was easy enough.

 

But I have a few things I'd like to change/fix:

 

1) When you receive the information, its all on one line, and without what field it came from which can be confusing. $detail is an original ID, but I've added name, phone, make, size, along with a couple others. They aren't listed here. :-x

 

// Contact subject
$subject ="Customer Quote Form";
// Details
$message="$detail";

2) When the form is submitted, I'd like it to link to a new page i create, instead of displaying a simple message. How would I do this?

 

I think that's it...any help is appreciated...thanks!

Link to comment
https://forums.phpfreaks.com/topic/197175-super-php-noob-help-with-form-mailer/
Share on other sites

HTML Page code:

 

<h3>Want a free quote? Take a moment to fill out our form!</h3>
<table width="650" border="0" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_contact.php">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td style="width:200px">Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"/></td>
</tr>

<tr>
<td>Phone Number</td>
<td>:</td>
<td><input name="phone" type="text" id="phone" size="50"/></td>
</tr>

<tr>
<td>Make & Model of Piano</td>
<td>:</td>
<td><input name="make" type="text" id="make" size="50"/></td>
</tr>

<tr>
<td>Size of Piano (measured)</td>
<td>:</td>
<td><input name="size" type="text" id="size" size="50"/></td>
</tr>

<tr>
<td>Number of stairs</td>
<td>:</td>
<td><input name="stairs" type="text" id="stairs" size="50"/></td>
</tr>

<tr>
<td>Mileage traveled from A to B</td>
<td>:</td>
<td><input name="mileage" type="text" id="mileage" size="50"/></td>
</tr>

<tr>
<td>Truck Access (Yes/No)?</td>
<td>:</td>
<td><input name="access" type="text" id="access" size="50"/></td>
</tr>


<tr>
<td>Any other information we may need to know?</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"/> <input type="reset" name="Submit2" value="Reset"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

 

send_contact.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Contact Form</title>
</head>

<body>
<?php
// Contact subject
$subject ="Customer Quote Form";
// Details
$message="$detail";

// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='<my email here>';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "Thank you for filling out our form! We have received your message and will respond to you soon.";
}
else {
echo "Error: Please email us directly: <customer email here>.";
}
?>
</body>

</html>

 

As you can see my custom fields aren't in the send_contact.php. I wanted to wait until I got the formatting to add it..and to be sure its added right.

For simplicity here's what I would do ..

 

In the code where it says:

if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}

 

I'd put:

 

if($send_contact){
header('Location: thankyou.php');
}
else {
echo "ERROR";
}

 

Create a page called "thankyou.php" with all the same markup and css as your other pages and present your message received text in that.

 

the header function given the "Location" will load a page again so if you load "thankyou.php" - that is the page that you'll see when the form has been completed correctly.

 

- Alex

Awesome, thanks alex.

 

What about having the email display the field names, and their user submitted text?

 

$message="$detail";

 

You can put anything you wish in $detail, such as the $_POST elements of their submitted text.

 

But where is $detail even defined? I don't see it, it should be blank or with an error unless you define it.

 

$message="$detail";

 

You can put anything you wish in $detail, such as the $_POST elements of their submitted text.

 

But where is $detail even defined? I don't see it, it should be blank or with an error unless you define it.

 

Ah, it's hiding.  ;) It was the last option in the form:

<tr>
<td>Any other information we may need to know?</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>

 

Again I apologize; I don't even know how to format the results. Normally I would have plenty of time to learn and test test test, but I am under time constraints.  :-[ If I could have a simple example with one or two lines, I could do the rest myself.

 

First of all: did that code I give you work ?

 

I just uploaded it to test and got this message:

Warning: Cannot modify header information - headers already sent by (output started at /home/vpm/public_html/test/send_contact.php:10) in /home/vpm/public_html/test/send_contact.php on line 29

 

Instead of thankyou.php I used test.php and changed send_contact.php to show that.

Heres a link to my very noob friendly contact form maker.

 

All you do is enter your email, then it generates a phps (re-saveas to .php); then upload.

It sends the logs to your email you enter.

 

http://lukeidiot.com/form/make.php

Try something like this:

<?php
// Contact subject
$subject ="Customer Quote Form";
// Details

$name = (isset($_POST['name'])) ? strip_tags(ucwords(strtolower($_POST['name']))) : NULL;
// Mail of sender
$mail_from= (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : NULL;
$info['Customer Phone'] = (isset($_POST['phone'])) ? strip_tags($_POST['phone']) : 'Phone number not listed';
$info['Piano Make and Model'] = (isset($_POST['make'])) ? strip_tags($_POST['make']) : 'Make & Model not listed.';
$info['Piano Size'] = (isset($_POST['size'])) ? strip_tags($_POST['size']) : 'Size not listed.';
$info['Stairs'] = (isset($_POST['stairs'])) ? (int) $_POST['stairs'] : 0;
$info['Estimated Mileage'] = (isset($_POST['mileage'])) ? (int) $_POST['mileage'] : 0;
$info['Truck Access'] = (isset($_POST['access']) && strtolower($_POST['access']) == 'yes') ?  'Yes' : 'No, or not stated.';
$info['Other Details'] = (isset($_POST['detail'])) ? strip_tags($_POST['detail']) : NULL;

$message = 'Customer: ' . $name . PHP_EOL;
foreach($info as $key => $value) {
$message .= $key . ': ' . $value . PHP_EOL;
}
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='<my email here>';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('Status: 200');
header('Location: thankyou.php');  //This should be a full URI as in 'Location: http://mysite.com/thankyou.php'
}
else {
$comment = "Error: Please email us directly: <customer email here>.";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Contact Form</title>
</head>

<body>
<?php echo $comment; ?>
</body>

</html>

Success!

 

I did notice that the email info was not submitted in the message, so I just added:

 

$info['Customer Email'] = (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : 'Email not listed.';

 

However, if you leave the phone number/email fields empty, no message is displayed prompting you for the information. Is there an easy solution to that? I've seen some other free forms online with these, I'll look at those as a reference and see if I can figure it out.

The HTML page (now is PHP and should be named with the .php extension)

<?php
session_start();
$name = null;
$customer_mail = null;
$customer_phone = null;
$piano_make_and_model = null;
$piano_size = null;
$stairs = null;
$estimated_mileage = null;
$truck_access = null;
$other_details = null;
$customer_email = null;

if(isset($_SESSION['errors']) && is_array($_SESSION['errors'])) {
foreach($_SESSION['errors'] as $error => $desc) {		
		$error = strtolower(str_replace(' ','_',$error));
		$$error = '<br />' . $desc;		
}
}
?>

<h3>Want a free quote? Take a moment to fill out our form!</h3>
<table width="650" border="0" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_contact.php">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td style="width:200px">Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"/><?php echo $name; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"/><?php echo $customer_email; ?></td>
</tr>

<tr>
<td>Phone Number</td>
<td>:</td>
<td><input name="phone" type="text" id="phone" size="50"/><?php echo $customer_phone; ?></td>
</tr>

<tr>
<td>Make & Model of Piano</td>
<td>:</td>
<td><input name="make" type="text" id="make" size="50"/><?php echo $piano_make_and_model; ?></td>
</tr>

<tr>
<td>Size of Piano (measured)</td>
<td>:</td>
<td><input name="size" type="text" id="size" size="50"/><?php echo $piano_size; ?></td>
</tr>

<tr>
<td>Number of stairs</td>
<td>:</td>
<td><input name="stairs" type="text" id="stairs" size="50"/><?php echo $stairs; ?></td>
</tr>

<tr>
<td>Mileage traveled from A to B</td>
<td>:</td>
<td><input name="mileage" type="text" id="mileage" size="50"/><?php echo $estimated_mileage; ?></td>
</tr>

<tr>
<td>Truck Access (Yes/No)?</td>
<td>:</td>
<td><input name="access" type="text" id="access" size="50"/><?php echo $truck_access; ?></td>
</tr>


<tr>
<td>Any other information we may need to know?</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea><?php echo $other_details; ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"/> <input type="reset" name="Submit2" value="Reset"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

 

processing script.  Put your complete URI to the page above in the header function on line 33.

<?php
session_start();
// Contact subject
$subject ="Customer Quote Form";
// Details

$name = (isset($_POST['name'])) ? strip_tags(ucwords(strtolower($_POST['name']))) : NULL;
// Mail of sender
$mail_from= (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : NULL;
$info['Name'] = $name;
$info['Customer Phone'] = (isset($_POST['phone'])) ? strip_tags($_POST['phone']) : NULL;
$info['Piano Make and Model'] = (isset($_POST['make'])) ? strip_tags($_POST['make']) : NULL;
$info['Piano Size'] = (isset($_POST['size'])) ? strip_tags($_POST['size']) : NULL;
$info['Stairs'] = (isset($_POST['stairs'])) ? (int) $_POST['stairs'] : NULL;
$info['Estimated Mileage'] = (isset($_POST['mileage'])) ? (int) $_POST['mileage'] : NULL;
$info['Truck Access'] = (isset($_POST['access']) && strtolower($_POST['access']) == 'yes') ?  'Yes' : 'No, or not stated.';
$info['Other Details'] = (isset($_POST['detail'])) ? strip_tags($_POST['detail']) : NULL;
$info['Customer Email'] = (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : NULL;

$message = NULL;
foreach($info as $key => $value) {

if($value == NULL) { 
$error[$key] = 'You have left ' . $key . ' blank.';
}

$message .= $key . ': ' . $value . PHP_EOL;
}

if(is_array($error) && isset($error)) {
$_SESSION['errors'] = $error;
header('Status: 200');
header('Location: http://www.mysite/myform.php');
}

// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='<my email here>';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('Status: 200');
header('Location: thankyou.php');  //This should be a full URI as in 'Location: http://mysite.com/thankyou.php'
}
else {
$comment = "Error: Please email us directly: <customer email here>.";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Contact Form</title>
</head>

<body>
<?php echo $comment; ?>
</body>

</html>

 

Of course, you will want to style the error messages for display.  Currently this puts them below the inputs the error resides in.

Hey JC, thanks for the reply. My post got buried and I only saw it yesterday.

 

I tried what you said, but when I uploaded the files to my web space I got this error:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/vpm/public_html/test/rates.php:7) in /home/vpm/public_html/test/rates.php  on line 61

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/vpm/public_html/test/rates.php:7) in /home/vpm/public_html/test/rates.php on line 61

You must be including these files into other files.

 

Move:

session_start();

 

To the very top of the parent file.

 

On both files.

 

Sorry, I'm confused. It's at the top of my send_contact.php file, in the PHP tags, and at the beginning of the PHP script in my single webpage (saved as .php), with the PHP tags right before it. Should it be somewhere else?

session_start() has to be at the very top of the page, and before any echo's or regular HTML.

 

So if your page starts with:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

 

Then the session start should be moved before it.

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

OK, interesting.

 

I entered only my name in the form, and clicked 'Submit'. It redirected me to the "thank you" page I made. I hit my browser's back button though, and saw the error messages under each field I didn't enter information in.

 

Again just for reference here is my rates.php page (with the form):

 

<?php
session_start();
$name = null;
$customer_mail = null;
$customer_phone = null;
$piano_make_and_model = null;
$piano_size = null;
$stairs = null;
$estimated_mileage = null;
$truck_access = null;
$other_details = null;
$customer_email = null;

if(isset($_SESSION['errors']) && is_array($_SESSION['errors'])) {

foreach($_SESSION['errors'] as $error => $desc) {

$error = strtolower(str_replace(' ','_',$error));

$$error = '<br />' . $desc;








}
}
?>

<h3>Want a free quote? Take a moment to fill out our form!</h3>
<table width="650" border="0" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_contact.php">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td style="width:200px">Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"/><?php echo $name; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"/><?php echo $customer_email; ?></td>
</tr>

<tr>
<td>Phone Number</td>
<td>:</td>
<td><input name="phone" type="text" id="phone" size="50"/><?php echo $customer_phone; ?></td>
</tr>

<tr>
<td>Make & Model of Piano</td>
<td>:</td>
<td><input name="make" type="text" id="make" size="50"/><?php echo $piano_make_and_model; ?></td>
</tr>

<tr>
<td>Size of Piano (measured)</td>
<td>:</td>
<td><input name="size" type="text" id="size" size="50"/><?php echo $piano_size; ?></td>
</tr>

<tr>
<td>Number of stairs</td>
<td>:</td>
<td><input name="stairs" type="text" id="stairs" size="50"/><?php echo $stairs; ?></td>
</tr>

<tr>
<td>Mileage traveled from A to B</td>
<td>:</td>
<td><input name="mileage" type="text" id="mileage" size="50"/><?php echo $estimated_mileage; ?></td>
</tr>

<tr>
<td>Truck Access (Yes/No)?</td>
<td>:</td>
<td><input name="access" type="text" id="access" size="50"/><?php echo $truck_access; ?></td>
</tr>


<tr>
<td>Any other information we may need to know?</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea><?php echo $other_details; ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"/> <input type="reset" name="Submit2" value="Reset"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

 

...and send_contact.php:

 

<?php
session_start();
// Contact subject
$subject ="Customer Quote Form";
// Details

$name = (isset($_POST['name'])) ? strip_tags(ucwords(strtolower($_POST['name']))) : NULL;
// Mail of sender
$mail_from= (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : NULL;
$info['Name'] = $name;
$info['Customer Phone'] = (isset($_POST['phone'])) ? strip_tags($_POST['phone']) : NULL;
$info['Piano Make and Model'] = (isset($_POST['make'])) ? strip_tags($_POST['make']) : NULL;
$info['Piano Size'] = (isset($_POST['size'])) ? strip_tags($_POST['size']) : NULL;
$info['Stairs'] = (isset($_POST['stairs'])) ? (int) $_POST['stairs'] : NULL;
$info['Estimated Mileage'] = (isset($_POST['mileage'])) ? (int) $_POST['mileage'] : NULL;
$info['Truck Access'] = (isset($_POST['access']) && strtolower($_POST['access']) == 'yes') ?  'Yes' : 'No, or not stated.';
$info['Other Details'] = (isset($_POST['detail'])) ? strip_tags($_POST['detail']) : NULL;
$info['Customer Email'] = (isset($_POST['customer_mail'])) ? strip_tags($_POST['customer_mail']) : NULL;

$message = NULL;
foreach($info as $key => $value) {

if($value == NULL) { 



$error[$key] = 'You have left ' . $key . ' blank.';
}

$message .= $key . ': ' . $value . PHP_EOL;
}

if(is_array($error) && isset($error)) {



$_SESSION['errors'] = $error;



header('Status: 200');



header('Location: http://www.customer-url-here.com/test/rates.php');
}

// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='myemailhere@fortesting.com';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('Status: 200');
header('Location: http://www.customer-url-here.com/test/thank_you.html');  //This should be a full URI as in 'Location: http://mysite.com/thankyou.php'
}
else {
$comment = "Error: Please email us directly: <customer email here>.";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" >
<title>Contact Form</title>
</head>

<body>
<?php echo $comment; ?>
</body>

</html>

 

 

That doesn't make sense

 

That means the $_SESSION['errors'] is being set, so the only thing that can be failing is the re-directing header.

 

After

header('Location: http://www.customer-url-here.com/test/rates.php');

 

add:

die('You should have been redirected!');

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.