Jump to content


Photo

prevent blank field submission


  • Please log in to reply
6 replies to this topic

#1 sandesh.acharya

sandesh.acharya

    Newbie

  • Members
  • Pip
  • 61 posts

Posted 06 October 2006 - 02:02 PM

i have a website with simple contact form that asks

1. Name
2. E-mail
3. Tel
4. Subject
5. Comments

the simple form is in .htm format and uses .php file for data submission.

now, problem is that someone stupid submits the contact form without any text (so i receive hundreds of blank e-mals everyday).

can you please suggest some script that prevents blank field from being submitted?

i searched around the net too but couldn't get exact solution.

for your info i have copied scripts of 'submit.htm' file and 'sendmail.php' files


CODE FROM MY submit.htm FILE that has contact form

<form method="POST" action="sendmail.php">
<input type="text" name="Name" >
<input type="text" name="UserEmail" >
<input type="text" name="Tel" >
<input type="text" name="Subj" >
<textarea rows="5" name="Comments" >
<input type="reset" value="Reset" >
<input type="submit" value="Submit" >
</form>



CODE FROM MY submit.php FILE that receives data from submit.htm file and processes e-mail


<?php
/////////////////////////////////////////////////////////////


if (!empty($_POST)){
$redirect = "thanks.htm";
$subject = $_POST['Subj'];
$to = "my@emailaddress.com";
$from = $_POST['UserEmail'];
$message = "Information submitted on " . date( "l, F d, Y") . "\n
------------------------------------------------\n
Name: {$_POST['Name']}\n
E-mail: {$_POST['UserEmail']}\n
Tel: {$_POST['Tel']}\n
Subject: {$_POST['Subj']}\n
Comments:\n\n{$_POST['Comments']}\n\n";

mailIt($to, $from, "", "", $subject, $message);
echo " message received - thank you";

if ($redirect != "thanks.htm") header("Location: {$redirect}");
}


////////////////////////////////////////////////////////////
// The actual mail function.

function mailIt($to, $from, $cc, $bcc, $subject, $message){
if (!is_array($to)){
$to = "{$to}";
}
else{
foreach($to as $to_nick=>$to_mail){
$to_arr[] = "{$to_mail}";
}
$to = implode(', ', $to_arr);
}

if (!is_array($from)){
$from = "{$from}";
}
else{
$from = key($from[0]);
}



/* To send HTML mail, you can set the Content-type header. */
//$headers = "MIME-Version: 1.0\r\n";
//$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: {$to}\r\n";
$headers .= "From: {$from}\r\n";
if ($cc != "") $headers .= "Cc: {$cc}\r\n";
if ($bcc !="") $headers .= "Bcc: {$bcc}\r\n";

/* and now mail it */
echo mail($to, $subject, $message, $headers);


}

?>

#2 dipeshmnm

dipeshmnm

    http://nepaleseteacher.org/

  • National Committee
  • PipPipPipPip
  • 1,131 posts

Posted 07 October 2006 - 07:47 AM

but i use asp .. other members ..please come on to rescue!
hey dude.. you can copy the CODE from http://www.wavemeg.com.np

#3 Limitation//Moon

Limitation//Moon

    Advanced Honourable Member

  • National Committee
  • PipPipPipPipPip
  • 2,810 posts

Posted 09 December 2006 - 11:24 AM

QUOTE(sandesh.acharya @ Oct 6 2006, 03:02 PM)  



the simple form is in .htm format and uses .php file for data submission.



it uses either .htm /or .php, it cann't use both file extension, convert your .htm file into .php and make all the files extension same.

or just download fresh copy of scripts from hotscripts.

#4 MohanTara

MohanTara

    Honourable Member

  • Moderator
  • 866 posts

Posted 09 December 2006 - 02:21 PM

Sandesh,
it's been ages since i've did programming,
n if i'm not wrong,
u need 2 have a $_REQUEST Variable 2 get the result from
form data sent with POST methods.

Name: <?php echo $_REQUEST["name"]; ?>.<br />
E-mail <?php echo $_REQUEST["email"]; ?>


Pardon me if i got it all wrong... blink.gif huh.gif

Without Faith,Nothing's Possible
With It,Nothing's Impossible!!

#5 Limitation//Moon

Limitation//Moon

    Advanced Honourable Member

  • National Committee
  • PipPipPipPipPip
  • 2,810 posts

Posted 09 December 2006 - 04:26 PM

QUOTE
Name: <?php echo $_REQUEST["name"]; ?><br />
E-mail <?php echo $_REQUEST["email"]; ?>


echo $_wonderful coding.

I am delighted with our very own Tara's programming skills, long time no see Tara. How's your baby smile.gif

#6 kbroka

kbroka

    Good things come to those who wait...

  • National Committee
  • PipPipPip
  • 283 posts

Posted 10 December 2006 - 05:21 AM

You can use Form Validations Java Script code like:

<script language="JavaScript" type="text/javascript">
var frmvalidator = new Validator("myform");
frmvalidator.addValidation("Name","req","Please enter your Name");
frmvalidator.addValidation("Name","maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("Name","alpha");

frmvalidator.addValidation("UserEmail","maxlen=50");
frmvalidator.addValidation("UserEmail","req");
frmvalidator.addValidation("UserEmail","email");

</script>

For details, You can see on: http://www.javascrip...alidation.phtml

thank you,



QUOTE(sandesh.acharya @ Oct 6 2006, 07:32 PM)  

i have a website with simple contact form that asks

1. Name
2. E-mail
3. Tel
4. Subject
5. Comments

the simple form is in .htm format and uses .php file for data submission.

now, problem is that someone stupid submits the contact form without any text (so i receive hundreds of blank e-mals everyday).

can you please suggest some script that prevents blank field from being submitted?

i searched around the net too but couldn't get exact solution.

for your info i have copied scripts of 'submit.htm' file and 'sendmail.php' files


CODE FROM MY submit.htm FILE that has contact form

<form method="POST" action="sendmail.php">
<input type="text" name="Name" >
<input type="text" name="UserEmail" >
<input type="text" name="Tel" >
<input type="text" name="Subj" >
<textarea rows="5" name="Comments" >
<input type="reset" value="Reset" >
<input type="submit" value="Submit" >
</form>



CODE FROM MY submit.php FILE that receives data from submit.htm file and processes e-mail


<?php
/////////////////////////////////////////////////////////////


if (!empty($_POST)){
$redirect = "thanks.htm";
$subject = $_POST['Subj'];
$to = "my@emailaddress.com";
$from = $_POST['UserEmail'];
$message = "Information submitted on " . date( "l, F d, Y") . "\n
------------------------------------------------\n
Name: {$_POST['Name']}\n
E-mail: {$_POST['UserEmail']}\n
Tel: {$_POST['Tel']}\n
Subject: {$_POST['Subj']}\n
Comments:\n\n{$_POST['Comments']}\n\n";

mailIt($to, $from, "", "", $subject, $message);
echo " message received - thank you";

if ($redirect != "thanks.htm") header("Location: {$redirect}");
}


////////////////////////////////////////////////////////////
// The actual mail function.

function mailIt($to, $from, $cc, $bcc, $subject, $message){
if (!is_array($to)){
$to = "{$to}";
}
else{
foreach($to as $to_nick=>$to_mail){
$to_arr[] = "{$to_mail}";
}
$to = implode(', ', $to_arr);
}

if (!is_array($from)){
$from = "{$from}";
}
else{
$from = key($from[0]);
}



/* To send HTML mail, you can set the Content-type header. */
//$headers = "MIME-Version: 1.0\r\n";
//$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: {$to}\r\n";
$headers .= "From: {$from}\r\n";
if ($cc != "") $headers .= "Cc: {$cc}\r\n";
if ($bcc !="") $headers .= "Bcc: {$bcc}\r\n";

/* and now mail it */
echo mail($to, $subject, $message, $headers);


}

?>


KB Roka
(Web Developer)
www.wnso.org.np
www.kbroka.com
Email: wnsonepal@gmail.com

#7 MohanTara

MohanTara

    Honourable Member

  • Moderator
  • 866 posts

Posted 11 December 2006 - 03:59 PM

As kbroka said,
validator can be also used.


Hi Limitation//Moon
I'm doing good.
My Baby Brian Jay's fast asleep..
**it's midnight out here** Ssshhhhh.....
Howdy?? How's life out there?...smile.gif
Hope it's treating u pretty well,
aside from the weather..

Without Faith,Nothing's Possible
With It,Nothing's Impossible!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users