PDA

View Full Version : PHP email form help



jynx
July 14th, 2008, 01:36 PM
i got this emailing php form online. its open source. im not too great with php and i tried editing the form to add a "Type of Business" and "Revenue size of Business" Field in the sent email but its not working. Here'e the unedited code:


<?php

// Copyright (C) 2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
// Instead, you may link to the homepage of this code:
// http://www.php-development.ru/php-scripts/contact-form.php
// (3) Use this code as part of another product.
//
// YOU MAY
// (1) Use this code on your website.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

?>


<!-- ***** Configuration ************************************************** * -->

<?php

$contact_form_field_name = 'Name:';
$contact_form_field_email = 'E-mail:';
$contact_form_field_subject = 'Subject:';
$contact_form_field_message = 'Message:';
$contact_form_field_number = 'Turing&nbsp;number:';
$contact_form_field_prompt = 'Enter the number displayed above';

$contact_form_button = 'Submit';

$contact_form_sent = 'Message sent';
$contact_form_not_sent = 'Message not sent';
$contact_form_invalid_data = 'Please, correct the fields marked with red';

?>


<div class="contact_form">


<!-- ***** PHP ************************************************** *********** -->

<?php

// ***** Functions *****

function contact_form_post($name)
{
return isset($_POST[$name]) ? htmlentities($_POST[$name], ENT_COMPAT) : '';
}

// ***** Send Mail *****

if (isset($_POST['Contact']))
{
if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
{
function strip_slashes_deep($value)
{
if (is_array($value)) return array_map('strip_slashes_deep', $value);
return stripslashes($value);
}

$_GET = strip_slashes_deep($_GET);
$_POST = strip_slashes_deep($_POST);
$_COOKIE = strip_slashes_deep($_COOKIE);
}

$patern_aux1 = "(\\w+(-\\w+)*)";
$patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$ patern_aux1";

$valid_name = isset($_POST['Name' ]) && trim($_POST['Name' ]);
$valid_email = isset($_POST['Email' ]) && preg_match("/^$patern_aux2$/iX", $_POST['Email']);
$valid_subject = isset($_POST['Subject']) && trim($_POST['Subject']);
$valid_message = isset($_POST['Message']) && trim($_POST['Message']);
$valid_number = isset($_POST['Number' ]) && isset($_SESSION['Number']) && $_POST['Number'] == $_SESSION['Number'];

if (!$valid_name) $contact_form_field_name = "<em>$contact_form_field_name </em>";
if (!$valid_email) $contact_form_field_email = "<em>$contact_form_field_email </em>";
if (!$valid_subject) $contact_form_field_subject = "<em>$contact_form_field_subject</em>";
if (!$valid_message) $contact_form_field_message = "<em>$contact_form_field_message</em>";
if (!$valid_number) $contact_form_field_number = "<em>$contact_form_field_number </em>";

if ($valid_name && $valid_email && $valid_subject && $valid_message && $valid_number)
{
$error_reporting = error_reporting(E_ERROR | E_PARSE);
$mail_sent = mail($contact_form_your_email,
$_POST['Subject'],
$contact_form_message_prefix . $_POST['Message'],
"To: $contact_form_your_name <$contact_form_your_email>\r\n" .
"From: {$_POST['Name']} <{$_POST['Email']}>\r\n");
error_reporting($error_reporting);

if ($mail_sent)
echo "<div class=\"error\"><em> $contact_form_sent </em></div>";
else echo "<div class=\"error\"><em class=\"error\">$contact_form_not_sent</em></div>";
if ($mail_sent) $_POST = array();
}
else echo "<div class=\"contact_form\"><div class=\"error\"><em>$contact_form_invalid_data</em></div></div>";
}

$_SESSION['Number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);

?>


<!-- ***** IE Patch ************************************************** ****** -->

<!--[if IE]><style type="text/css"> div.contact_form div.input input { margin-top: -1px; margin-bottom: -1px; } </style><![endif]-->
<!--[if IE]><style type="text/css"> div.contact_form div.input textarea { margin-top: -1px; margin-bottom: -1px; } </style><![endif]-->


<!-- ***** XHTML ************************************************** ********* -->

<?php if ( $contact_form_xhtml) { ?>

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>

<tr><th><?=$contact_form_field_name; ?></th><td><div class="input"><input name= "Name" type="text" value="<?=contact_form_post( 'Name');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td><div class="input"><input name= "Email" type="text" value="<?=contact_form_post( 'Email');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>

<tr><th><?=$contact_form_field_number;?></th><td>
<div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off" /></div>
&nbsp;
<img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>&amp;rand=<?=rand();?>" alt="" />
<br style="clear: both" />
<small><?=$contact_form_field_prompt;?></small>
</td></tr>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>" /></td></tr>

</table>
</form>

<?php } ?>


<!-- ***** HTML ************************************************** ********** -->

<?php if (!$contact_form_xhtml) { ?>

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>

<tr><th><?=$contact_form_field_name; ?></th><td><div class="input"><input name= "Name" type="text" value="<?=contact_form_post( 'Name');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td><div class="input"><input name= "Email" type="text" value="<?=contact_form_post( 'Email');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>

<tr><th><?=$contact_form_field_number;?></th><td>
<div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off"></div>
&nbsp;
<img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>" alt="">
<br style="clear: both">
<small><?=$contact_form_field_prompt;?></small>
</td></tr>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>"></td></tr>

</table>
</form>

<?php } ?>


</div>

and here's how I tried to edit it


<?php

// Copyright (C) 2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
// Instead, you may link to the homepage of this code:
// http://www.php-development.ru/php-scripts/contact-form.php
// (3) Use this code as part of another product.
//
// YOU MAY
// (1) Use this code on your website.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

?>


<!-- ***** Configuration ************************************************** * -->

<?php

$contact_form_field_name = 'Name:';
$contact_form_field_email = 'E-mail:';
$contact_form_field_subject = 'Subject:';
$contact_form_field_type = 'Type:';
$contact_form_field_revenue = 'Revenue:';
$contact_form_field_message = 'Goal:';
$contact_form_field_number = 'Turing&nbsp;number:';
$contact_form_field_prompt = 'Enter the number displayed above';

$contact_form_button = 'Submit';

$contact_form_sent = 'Message sent';
$contact_form_not_sent = 'Message not sent';
$contact_form_invalid_data = 'Please, correct the fields marked with red';

?>


<div class="contact_form">


<!-- ***** PHP ************************************************** *********** -->

<?php

// ***** Functions *****

function contact_form_post($name)
{
return isset($_POST[$name]) ? htmlentities($_POST[$name], ENT_COMPAT) : '';
}

// ***** Send Mail *****

if (isset($_POST['Contact']))
{
if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
{
function strip_slashes_deep($value)
{
if (is_array($value)) return array_map('strip_slashes_deep', $value);
return stripslashes($value);
}

$_GET = strip_slashes_deep($_GET);
$_POST = strip_slashes_deep($_POST);
$_COOKIE = strip_slashes_deep($_COOKIE);
}

$patern_aux1 = "(\\w+(-\\w+)*)";
$patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$ patern_aux1";

$valid_name = isset($_POST['Name' ]) && trim($_POST['Name' ]);
$valid_email = isset($_POST['Email' ]) && preg_match("/^$patern_aux2$/iX", $_POST['Email']);
$valid_subject = isset($_POST['Subject']) && trim($_POST['Subject']);
$valid_type = isset($_POST['Type']) && trim($_POST['Type' ]);
$valid_revenue = isset($_POST['Revenue']) && trim($_POST['Revenue' ]);
$valid_message = isset($_POST['Message']) && trim($_POST['Message']);
$valid_number = isset($_POST['Number' ]) && isset($_SESSION['Number']) && $_POST['Number'] == $_SESSION['Number'];

if (!$valid_name) $contact_form_field_name = "<em>$contact_form_field_name </em>";
if (!$valid_email) $contact_form_field_email = "<em>$contact_form_field_email </em>";
if (!$valid_subject) $contact_form_field_subject = "<em>$contact_form_field_subject</em>";
if (!$valid_type) $contact_form_field_type = "<em>$contact_form_field_type </em>";
if (!$valid_revenue) $contact_form_field_revenue = "<em>$contact_form_field_revenue</em>";
if (!$valid_message) $contact_form_field_message = "<em>$contact_form_field_message</em>";
if (!$valid_number) $contact_form_field_number = "<em>$contact_form_field_number </em>";

if ($valid_name && $valid_email && $valid_subject && $valid_message && $valid_number)
{
$error_reporting = error_reporting(E_ERROR | E_PARSE);
$mail_sent = mail($contact_form_your_email,
$_POST['Subject'],
$contact_form_message_prefix . $_POST['Type'] , $_POST['Revenue'] $_POST['Message'],
"To: $contact_form_your_name <$contact_form_your_email>\r\n" .
"From: {$_POST['Name']} <{$_POST['Email']}>\r\n");
error_reporting($error_reporting);

if ($mail_sent)
echo "<div class=\"error\"><em> $contact_form_sent </em></div>";
else echo "<div class=\"error\"><em class=\"error\">$contact_form_not_sent</em></div>";
if ($mail_sent) $_POST = array();
}
else echo "<div class=\"contact_form\"><div class=\"error\"><em>$contact_form_invalid_data</em></div></div>";
}

$_SESSION['Number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);

?>


<!-- ***** IE Patch ************************************************** ****** -->

<!--[if IE]><style type="text/css"> div.contact_form div.input input { margin-top: -1px; margin-bottom: -1px; } </style><![endif]-->
<!--[if IE]><style type="text/css"> div.contact_form div.input textarea { margin-top: -1px; margin-bottom: -1px; } </style><![endif]-->


<!-- ***** XHTML ************************************************** ********* -->

<?php if ( $contact_form_xhtml) { ?>

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>

<tr><th><?=$contact_form_field_name; ?></th><td><div class="input"><input name= "Name" type="text" value="<?=contact_form_post( 'Name');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td><div class="input"><input name= "Email" type="text" value="<?=contact_form_post( 'Email');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_type;?></th><td><div class="input"><input name="Type of Business" type="text" value="<?=contact_form_post('Type of Business');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_revenue;?></th><td><div class="input"><input name="Revenue Size of Business" type="text" value="<?=contact_form_post('Revenue Size of Business');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>

<tr><th><?=$contact_form_field_number;?></th><td>
<div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off" /></div>
&nbsp;
<img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>&amp;rand=<?=rand();?>" alt="" />
<br style="clear: both" />
<small><?=$contact_form_field_prompt;?></small>
</td></tr>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>" /></td></tr>

</table>
</form>

<?php } ?>


<!-- ***** HTML ************************************************** ********** -->

<?php if (!$contact_form_xhtml) { ?>

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<table>

<tr><th><?=$contact_form_field_name; ?></th><td><div class="input"><input name= "Name" type="text" value="<?=contact_form_post( 'Name');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_email; ?></th><td><div class="input"><input name= "Email" type="text" value="<?=contact_form_post( 'Email');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_subject;?></th><td><div class="input"><input name="Subject" type="text" value="<?=contact_form_post('Subject');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_type;?></th><td><div class="input"><input name="Type of Business" type="text" value="<?=contact_form_post('Type of Business');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_revenue;?></th><td><div class="input"><input name="Revenue Size of Business" type="text" value="<?=contact_form_post('Revenue Size of Business');?>" ></div></td></tr>
<tr><th><?=$contact_form_field_message;?></th><td><div class="input"><textarea name="Message" cols="35" rows="6"><?=contact_form_post('Message');?></textarea></div></td></tr>

<tr><th><?=$contact_form_field_number;?></th><td>
<div class="input" style="float: left;"><input type="text" name="Number" autocomplete="off"></div>
&nbsp;
<img width="60" height="17" src="<?=$contact_form_image_url;?>?sname=<?=session_name();?>" alt="">
<br style="clear: both">
<small><?=$contact_form_field_prompt;?></small>
</td></tr>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" name="Contact" value="<?=$contact_form_graphic_button ? '' : $contact_form_button;?>"></td></tr>

</table>
</form>

<?php } ?>


</div>

any help?

jynx
July 17th, 2008, 10:21 AM
bump

jynx
July 21st, 2008, 11:46 AM
been trying to figure this out for over a week now. can anyone give me any hints? the developer hasn't answered my emails.

lorren.biffin
July 21st, 2008, 11:18 PM
You're not likely to get an answer with such a vague post. Can you be more specific about what the script *should* do, what you have done to it (give line numbers and edit details), and what it's actually doing (provide error messages, descriptions, etc.)?

Thanks! :) I'm looking forward to solving your problem when there's more detail.

skyraider
July 22nd, 2008, 12:02 AM
Why don't you echo the value of $_SERVER['REQUEST_URI'] to make sure that the data is being posted to the correct location?

jynx
July 22nd, 2008, 02:06 AM
the script is working fine. the email form can be seen on: http://summitpcg.com/contact.php

I want to add another couple of text fields to the contact form that get sent along with the message such as "Business Type:" and "Size of Business Revenue:". I tried following the pattern that everything else seems to be following but when I got it to show up on the form, it wouldn't be in the email it sent.

Molerat
July 24th, 2008, 10:36 PM
Im likely too late but its worth a go.



$valid_type = isset($_POST['Type']) && trim($_POST['Type']);
$valid_revenue = isset($_POST['Revenue']) && trim($_POST['Revenue']);


Here is where i think it goes wrong. You have these variables, which basically say you have apparently posted 'type' and 'revenue'.


<tr><th><?=$contact_form_field_type;?></th><td><div class="input"><input name="Type of Business" type="text" value="<?=contact_form_post('Type of Business');?>" /></div></td></tr>
<tr><th><?=$contact_form_field_revenue;?></th><td><div class="input"><input name="Revenue Size of Business" type="text" value="<?=contact_form_post('Revenue Size of Business');?>" />

But in your actual HTML, the names you give these input types are name="Revenue Size of Business" and name="Type of Business". I believe in order for these to be work, you should change:

name="Type of Business"
to name='Type'

and

name="Revenue Size of Business"
to name='Revenue'

So they match up with what the PHP thinks you've submitted. If you didn't figure it out already, give it a go. XD