PDA

View Full Version : great form script but...


KevinG
09-24-2002, 04:12 PM
Hey everyone below is the script i use to process my forms.

<?
$sitename=getenv("HTTP_HOST");
$message.="Site: $sitename\n\n";
while ( list( $key, $val )=each($HTTP_POST_VARS) )
{
$message .="$key : $val\n" ;
}
mail("my@email.com","enquiry",$message,"From: mail@jugs.com");
?>

the script works fine but lately i have been receiving a lot blank emails from my site - yet my forms have behaviours on them that require all the fields must be completed?

so why am i getting so many blank emails from my site? :(

http://www.websitevrm.com/contact.php

mordred
09-25-2002, 10:34 PM
What is the code you use for validating that the form is filled out correctly?

KevinG
09-26-2002, 08:27 AM
<form method="post" action="thanks.php" name="Website Enquiry">
<table width="455" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="42%" class="formtext">Name</td>
<td width="58%">
<input type="text" name="Name:" size="28" maxlength="36" class="boxes">
</td>
</tr>
<tr>
<td width="42%" class="formtext">Company Name</td>
<td width="58%">
<input type="text" name="Company" size="28" maxlength="36" class="boxes">
</td>
</tr>
<tr>
<td width="42%" class="formtext">Enquiry Type</td>
<td width="58%">
<select name="Enquiry Type" class="boxes">
<option selected>please choose</option>
<option>Advertise</option>
<option>Newsletter</option>
<option>Support</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td width="42%" class="formtext">Website</td>
<td width="58%">
<input type="text" name="WebsiteURL" size="28" maxlength="32" class="boxes">
</td>
</tr>
<tr>
<td width="42%" class="formtext">Email Address</td>
<td width="58%">
<input type="text" name="Email Address" size="28" maxlength="36" class="boxes">
</td>
</tr>
<tr>
<td width="42%" class="formtext"> Comments</td>
<td width="58%">
<textarea name="Comments:" wrap="VIRTUAL" cols="36" rows="4" class="boxes"></textarea>
</td>
</tr>
<tr>
<td width="42%">&nbsp;</td>
<td width="58%">
<input type="reset" value=" Reset " class="buttons" name="reset">
<input type="submit" value="Submit" onClick="MM_validateForm('Name:','','R','Company','','R','WebsiteURL','','R','Email Address','','RisEmail','Comments:','','R');return document.MM_returnValue" class="buttons" name="submit">
</td>
</tr>
</table>
</form>


I'm using the form check behaviour in dreamweaver.

mordred
09-26-2002, 07:38 PM
Are you aware that users have the option to disable javascript? And by thus bypassing your MM_javascript code?

P.S: You also forgot to include the js code, but it doesn't matter, MM code is usually quite convoluted.

KevinG
09-26-2002, 09:54 PM
without meaning to sound helpless, would you be able to post an alternate way to validate my form as i'm stuck! (and confused)

mordred
09-26-2002, 10:06 PM
An different way of validating your form is to do this completely by PHP. This has the big advantage that PHP, as it is running on the server, can't be disabled like javascript can.

From your description I suppose that you want to check if a form element has been filled out by the user.


if ( isset($HTTP_POST_VARS['websiteURL']) && !empty($HTTP_POST_VARS['websiteURL']) ) {
// element is filled, do sth.
}


You first have to check that the variable has been set at all in the HTTP_POST_VARS array, and then check if it's not empty. Just as an example, but that's roughly how you do it. You'll have to extend it to suit your needs.

If you want to make a detailed introspection of the POST variables submitted to you, you should employ Regular Expressions. Even though they look like the cat jumped on the keyboard, they are quite powerful.
http://www.php.net/manual/en/ref.pcre.php

Edit: I have no idea why, but Mozilla inserted linebreaks seemingly randomly in the textarea, so here goes the corrected code.

KevinG
09-26-2002, 10:14 PM
I'm trying my best to grasp this. where would the php validation code go in the form page? if so where? head or body?

or in the actual processor code -

<?
$sitename=getenv("HTTP_HOST");
$message.="Site: $sitename\n\n";
while ( list( $key, $val )=each($HTTP_POST_VARS) )
{
$message .="$key : $val\n" ;
}
mail("my@email.com","enquiry",$message,"From: mail@jugs.com");
?>

I am well a truly confused as to how i get this -

<input type="submit" value="Submit" onClick=" MM_validateForm('Name:','','R','Company','','R','W
ebsiteURL','','R','Email Address','','RisEmail','Comments:','','R');return document.MM_returnValue" class="buttons" name="submit">

reworked in to this -

if ( isset($HTTP_POST_VARS[
'websiteURL']) && !empty($HTTP_POST_VARS[
'websiteURL']) {
// element is filled, do sth.
}

I don't understand where the pop up message appears and how i stipulate what is allowed to typed into certain form fields i.e. email address, numbers only or anything.

could you provide the behaviour based on my above form so that i can see exactly how it works pplllleaaassseeeee?

once i've grasped it for this form i will understand for any form thereafter.

mordred
09-26-2002, 11:55 PM
You have to realize how both technologies work: PHP is processed on the server, and JavaScript on the client machine. This has the consequence that JavaScript and PHP can not run simultaneously in one script. Before you try to rework your validation code, you have to understand this issue thoroughly!

You can't make PHP do the same thing as JavaScript does. Everytime a PHP script shall be processed, a request to that particular file on the server must be issued.

The PHP code I posted should go into the page that handles the form variables, i.e. that is specified in the action attribute of the form. It's just a simple if-statement that checks for a value that must not be empty. Wrap this if-statement around the code you currently have for sending the mail and you'll see that you can only send mails if you typed something into the field named "websiteURL".

As for your other concerns... try to experiment with the code and refine what exactly you want to achieve, that makes it a lot easier to help you efficiently.

SYP}{ER
09-27-2002, 01:09 PM
$required = array (
array ($var1,"Var 1's name"),
array ($var2,"Var 2's name"),
array ($var3,"Var 3's name")
);
for ($i=0;$i<count($required);$i++){
if ($required[$i][0] == ""):
print_error ("You missed the \"{$required[$i][1]}\" field.");
$continue = "no";
endif;
}

That's what I use :) Works wonders. Give'er a try.

KevinG
09-27-2002, 05:17 PM
hey thanks SYP}{ER

I've never tried using php as a form checker, therefore i'm still unsure as to how and where to insert the script?

How do i attach this script behaviour to the submit button?