Click to See Complete Forum and Search --> : [RESOLVED] Trying to add alerts for ...


A_Very_Wild_Wil
09-23-2006, 07:12 PM
I've been trying to add some sort of warning/alert for my form to email page (aka contact us page) and failed :( so now asking for help.

My form has got an alert for an incorrect email format, but I need some extra alerts for name, postcode and phone entry boxes.

Can anyone help me please or point me in the right direction? Can I point out I'm no coder :( , hence asking for help.

My exsisting form has several features I've added and managed to get them working, like forwarding of the IP address, pre-defined text labels in the email body.

I've stripped my code down as much as possible in this request for help, as the 'working' copy has several additional text entry boxes and lots of formatting.

Thanks in advance for looking and any help :)
<?php

require('includes/application_top.php');

$error = false;
if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'send')) {

$email_address = tep_db_prepare_input($HTTP_POST_VARS['email']);

//IP recorder start
$ipaddress = $HTTP_SERVER_VARS["REMOTE_ADDR"];
$ip = "\n\n IP: " . $ipaddress;
$content = $HTTP_POST_VARS['enquiry'];
$content_ip = $content . $ip;
$enquiry = tep_db_prepare_input($content_ip);
//IP recorder end

if (tep_validate_email($email_address)) {
tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT,


// start of pre-entered email text
"Full Name: " . $name . "\n"
. "E-mail: " . $email . "\n"
. "Phone Numer: " . $phone . "\n"
. "Postcode: " . $postcode . "\n\n"
. "Message:<BR> " . $enquiry . "\n"
, $name, $email_address);
// end of pre-entered email text


tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
} else {
$error = true;

$messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
}
}

$breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CONTACT_US));
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
</head>
<body>

<table><tr><td>

<?php echo tep_draw_form('contact_us', tep_href_link(FILENAME_CONTACT_US, 'action=send')); ?>

<table>

<?php
if ($messageStack->size('contact') > 0) {
?>
<tr> <td>
<!-- email errormessage --><?php echo $messageStack->output('contact'); ?></td>
</tr>
<?php
}

if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'success')) {
?>
<tr> <td>
<?php echo tep_image(DIR_WS_IMAGES . 'table_background_man_on_board.gif', HEADING_TITLE, '0', '0', 'align="left"') . TEXT_SUCCESS; ?></td>
</tr>

<tr> <td width="10"></td>
<td align="right">
<?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></td>
<td width="10"></td>
</tr>
</table>

</td> </tr> </table>

<?php
} else {
?>

<table> <tr> <td>

<table>
<tr>
<td width="100%" align="center" colspan="2">
<?php echo TEXT_MAIN_TEXT; ?>
</td></tr>

<td>NAME;</td>
<td width="50%"><?php echo tep_draw_input_field('name'); ?></td>
</tr>

<tr>
<td>ENTRY_EMAIL;</td>
<td width="50%"><?php echo tep_draw_input_field('email'); ?></td>
</tr>

<tr>
<td>POSTCODE</td>
<td width="50%"><?php echo tep_draw_input_field('postcode'); ?></td>
</tr>

<tr>
<td>COUNTRY;</td>
<td width="50%"><?php echo tep_draw_input_field('Country'); ?></td>
</tr>

<tr>
<td>PHONE;</td>
<td width="50%"><?php echo tep_draw_input_field('phone'); ?></td>
</tr>
</table>
</td> </tr>

<tr> <td>Please Enter Your Message:</td> </tr>
<tr> <td><?php echo tep_draw_textarea_field('enquiry', 'soft', 50, 5); ?></td>
</tr>

<tr> <td><?php echo tep_image_submit('submit-enquiry01.gif', IMAGE_BUTTON_CONTINUE); ?></td>
</tr>

<?php
}
?>
</table></form>

</body></html><?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

ronverdonk
09-24-2006, 10:40 AM
At first I suggest that you change usage of HTTP_VARS to $_GET and $_POST. HTTP_VARS is deprecated an you will have to change that in the future (maybe next PHP release?) anyway.

Assuming that you have a hidden field in your form with name='submitted' value=1 to check. The code could be something like:

$error_msgs = array();
// setting of $_POST['submitted'] indicates form submission
if (isset($_POST['submitted'])) {
// name must be specified and at least 5 chars long
if (!isset($_POST['name']) OR strlen($_POST['name']) < 5)
$error_msgs[] = "Field 'name' not specified or invalid content");
// phone must be specified, exactly 10 long and numeric only
if (!isset($_POST['phone']) OR strlen($_POST['phone']) != 10 OR !is_numeric($_POST['phone']) )
$error_msgs[] = "Field 'phone' not specified or invalid content");
// postcode must be specified, and exeactly 7 bytes long
if (!isset($_POST['postcode']) OR strlen($_POST['postcode']) != 7 )
$error_msgs[] = "Field 'postcode' not specified or invalid content");
// print out any error messages
if ($error_msgs != "") {
print 'You have the following errors:<br />';
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$error_msgs);
print '</b></li></ul></span>';
}
else {
// input is correct, do some cleansing and process further
$name = strip_tags($_POST['name']);
$phone = strip_tags($_POST['phone']);
$postcode = strip_tags($_POST['postcode']);
}
}
This code must be stored at the beginning of your script.
You also should echo the variables in the form's text fields, so people don't have to enter the correct values twice.

Ronald :cool:

A_Very_Wild_Wil
09-24-2006, 06:08 PM
Hi Ronald,

Thanks for your message.

I pasted your code almost at the top of my code, as shown here
<?php

require('includes/application_top.php');

$error_msgs = array();
// setting of $_POST['submitted'] indicates form submission..............I had an error message when I tried to run the file, as the message didn't like the >> ) << at the end of three lines as shown below $error_msgs[] = "Field 'name' not specified or invalid content"); Regarding your comment You also should echo the variables in the form's text fields, so people don't have to enter the correct values twice. I'm afraid you've now lost me, sorry. Can you give an example please?

Regards

Steve

ronverdonk
09-25-2006, 06:12 AM
Sorry about the error, I typed it just out of my head.

Bij echoing the variables I mean the following: when your user fills in all fields, and one or more of them are incorrect, you (I assume) re-display the form again with the error message(s). You don't want the user to fill in all those fields AGAIN, especially not the correct ones. So you must fill the form fields with the values that he previously typed in using an echo in the <input> value.

I include the same code I showed before, but now I added the handling of the form and the re-display of the values (echo-ing) the user had already filled in. So you see for each field a statement like: value="<?php echo (isset($_POST['field_name'])) ? $_POST['field_name'] : ""; ?>"

Try it out (this code is tested) and you will see what I mean. Good luck.

<?php
$error_msgs = array();
// setting of $_POST['submitted'] indicates form submission
if (isset($_POST['submitted'])) {
// name must be specified and at least 5 chars long
if (!isset($_POST['name']) OR strlen($_POST['name']) < 5)
$error_msgs[] = "Field 'name' not specified or invalid content";
// phone must be specified, exactly 10 long and numeric only
if (!isset($_POST['phone']) OR strlen($_POST['phone']) != 10 OR !is_numeric($_POST['phone']) )
$error_msgs[] = "Field 'phone' not specified or invalid content";
// postcode must be specified, and exeactly 7 bytes long
if (!isset($_POST['postcode']) OR strlen($_POST['postcode']) != 7 )
$error_msgs[] = "Field 'postcode' not specified or invalid content";
// print out any error messages
if ($error_msgs != "") {
print 'You have the following errors:<br />';
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$error_msgs);
print '</b></li></ul></span>';
}
else {
// input is correct, do some cleansing and process further
$name = strip_tags($_POST['name']);
$phone = strip_tags($_POST['phone']);
$postcode = strip_tags($_POST['postcode']);
exit; // just to test
}
}
?>
<form name="MyForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name (mandatory, min 5 chars): <br />
<input type="text" name="name" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>" /><br />
Phone number (mandatory, 10 digits): <br />
<input type="text" name="phone" value="<?php echo (isset($_POST['phone'])) ? $_POST['phone'] : ""; ?>" /><br />
Postcode (mandatory, 7 chars) : <br />
<input type="text" name="postcode" value="<?php echo (isset($_POST['postcode'])) ? $_POST['postcode'] : ""; ?>" /><br />
<input type="hidden" name="submitted" value="1" />
<input type="submit" value="Submit" />
<input type="reset" value="Reset fields" />
</form>

Ronald :cool:

A_Very_Wild_Wil
09-29-2006, 07:09 AM
Just like to say I'm now sorted - up and running with the help of ronverdonk :D

Thanks for your help and support :D