Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Programming / Scripting / Coding

Programming / Scripting / Coding Got a question about : PHP / Perl / Java / JavaScript / CGI Scripts / JavaScript coding. Ask here! We'll get you the answers !

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-2007, 12:01 AM
puresea's Avatar
Junior Member
 
Join Date: Feb 2007
Posts: 3
puresea is on a distinguished road
Post Form Validation Help?

Hi everyone,

I was wondering if someone out there would be nice enough to help me with my script. I recently scripted a contact form that has to validate certain fields before being submitted to a database. The strangest thing is, that even if it's not filled out, could be any field, it will still go through without giving the message of a missing field.

If anyone out there could help, it would be grand. Thank you.


PHP Code:
<?php
$to 
"xxx@hhh.com";
$from "from: " .stripslashes($_POST['name']) ."<" .$_POST['email'] .">";
$subject "I want to join";
$message stripslashes($_POST['name']) . "\n" $_POST['email'] . "\n" $_POST['country'];

mail($to$subject$message$from);?>


<?php

 
//Validate form
if (isset($_POST['submit'])) {

    
$message NULL;

//Validate name field
    
if (empty($_POST['name'])) {
        
$n FALSE;
        
$message .='<p>You didn\'t enter your name!</p>';
    } else {
        
$n $_POST['name'];
    }

//Validate e-mail field
    
if (empty($_POST['email'])) {
        
$em FALSE;
        
$message .='<p>You didn\'t enter your e-mail!</p>';
    } else {
        
$em $_POST['email'];
    }
    
//Validate country field
    
if (empty($_POST['country'])) {
        
$c FALSE;
        
$message .='<p>You didn\'t enter your country</p>';
    } else {
        
$c $_POST['country'];
    }    

//Validate comments field
    
if (empty($_POST['country'])) {
        
$co FALSE;
        
$message .='<p>You didn\'t make any comments!</p>';
    } else {
        
$co $_POST['comments'];
    }

?>

Reply With Quote
  #2 (permalink)  
Old 04-18-2007, 12:00 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Quote:
Originally Posted by puresea View Post
Hi everyone,

I was wondering if someone out there would be nice enough to help me with my script. I recently scripted a contact form that has to validate certain fields before being submitted to a database. The strangest thing is, that even if it's not filled out, could be any field, it will still go through without giving the message of a missing field.

If anyone out there could help, it would be grand. Thank you.


PHP Code:
<?php
$to 
"xxx@hhh.com";
$from "from: " .stripslashes($_POST['name']) ."<" .$_POST['email'] .">";
$subject "I want to join";
$message stripslashes($_POST['name']) . "\n" $_POST['email'] . "\n" $_POST['country'];

mail($to$subject$message$from);?>


<?php

 
//Validate form
if (isset($_POST['submit'])) {

    
$message NULL;

//Validate name field
    
if (empty($_POST['name'])) {
        
$n FALSE;
        
$message .='<p>You didn\'t enter your name!</p>';
    } else {
        
$n $_POST['name'];
    }

//Validate e-mail field
    
if (empty($_POST['email'])) {
        
$em FALSE;
        
$message .='<p>You didn\'t enter your e-mail!</p>';
    } else {
        
$em $_POST['email'];
    }
    
//Validate country field
    
if (empty($_POST['country'])) {
        
$c FALSE;
        
$message .='<p>You didn\'t enter your country</p>';
    } else {
        
$c $_POST['country'];
    }    

//Validate comments field
    
if (empty($_POST['country'])) {
        
$co FALSE;
        
$message .='<p>You didn\'t make any comments!</p>';
    } else {
        
$co $_POST['comments'];
    }

?>
Now i think i grasped what you wanted to do here. you want to validate your form.

No problem, but i would reccomend you read the tutorials on W3schools.com & tizag.com on form validation as they are quite usefull.


I have one small question for you if thats ok:

WHy have you used such line as:

$c = FALSE;

A variable is automaticly false when it is created.

You may have another reason that comes aparent later on, but i was just wondering

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #3 (permalink)  
Old 04-18-2007, 12:30 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Quote:
Originally Posted by puresea View Post
Hi everyone,



PHP Code:
<?php
$to 
"xxx@hhh.com";
$from "from: " .stripslashes($_POST['name']) ."<" .$_POST['email'] .">";
$subject "I want to join";
$message stripslashes($_POST['name']) . "\n" $_POST['email'] . "\n" $_POST['country'];

mail($to$subject$message$from);?>


<?php

 
//Validate form
if (isset($_POST['submit'])) {

    
$message NULL;

//Validate name field
    
if (empty($_POST['name'])) {
        
$n FALSE;
        
$message .='<p>You didn\'t enter your name!</p>';
    } else {
        
$n $_POST['name'];
    }

//Validate e-mail field
    
if (empty($_POST['email'])) {
        
$em FALSE;
        
$message .='<p>You didn\'t enter your e-mail!</p>';
    } else {
        
$em $_POST['email'];
    }
    
//Validate country field
    
if (empty($_POST['country'])) {
        
$c FALSE;
        
$message .='<p>You didn\'t enter your country</p>';
    } else {
        
$c $_POST['country'];
    }    

//Validate comments field
    
if (empty($_POST['country'])) {
        
$co FALSE;
        
$message .='<p>You didn\'t make any comments!</p>';
    } else {
        
$co $_POST['comments'];
    }

?>
Another thing you could try is:

PHP Code:
$co $_POST['comments'];
if ( 
$co == "" ) {
        
$co FALSE;
        
$message .='<p>You didn\'t make any comments!</p>';
} else {
    echo 
$co;

Would do the same job!

Cheers

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #4 (permalink)  
Old 04-22-2007, 08:35 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
HmmmDennis is on a distinguished road
Should you be <? echo $message; ?> before php shows anything up on the page itself?...
Reply With Quote
  #5 (permalink)  
Old 04-22-2007, 08:48 PM
Junior Member
 
Join Date: Apr 2007
Posts: 5
nehir is on a distinguished road
PHP Code:
<?php
function validemail($email)
{
    return 
preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>]+\.+[a-z]{2,6}))$#si'$email);
}

function 
validusername($username)
{
    if(!
preg_match("|[^a-z\|A-Z\|0-9]|",$username))
        return 
true;
    else
        return 
false;
}

$name htmlspecialchars($_POST['name']);
$email htmlspecialchars($_POST['email']);
$country htmlspecialchars($_POST['country ']);
$message htmlspecialchars($_POST['message ']);

if (empty(
$name) || empty($email ) || empty($country ) || empty($message ) || !validemail($email) || !validusername($name) || strlen($name) <= 2)
{
die(
'Dont leave any required fields blank!');
}
else
{
// do job...
}
More secure.. thats an example..
Reply With Quote
  #6 (permalink)  
Old 04-23-2007, 12:54 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
nehir's got it.

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #7 (permalink)  
Old 04-26-2007, 06:45 PM
Junior Member
 
Join Date: Mar 2007
Posts: 3
shaza is on a distinguished road
i use it whithout any problem

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 06:16 PM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com