/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]<?php
$to = “[email protected]”;
$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’];
}

//If everything is filled in
if ($n && $em && $c && $co) {

//Add fan to the database

define (‘db_user’, ‘xxx_xxx’);
define (‘db_password’, ‘xxx);
define (‘db_host’, ‘xxx’);
define (‘db_name’, ‘xxx_xxxx’);

$dbc = @mysql_pconnect(db_host, db_user, db_password) OR die(‘Could not connect to MySQL: ‘ . mysql_error());
@mysql_select_db (db_name) OR die(‘could not connect to the database: ‘ . mysql_error());
$query = “INSERT INTO fans (name, email, country, comments) VALUES (‘$n’, ’em’, ‘$c’, ‘$co’)”;
$result = @mysql_query ($query);

if ($result) {
echo ‘<p>Thank you for joining!</p>’;
exit();
} else {
$message .= ‘<p>We are sorry, but you could not be added to the fanlisting due to a system error. We apologize for any inconvenience, please try again later.</p><p>’ . mysql_error() . ‘</p>’;
}

mysql_close();

} else {
$message .= ‘<p>We are sorry. Please try again later</p>’;

}

}

?>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@vijirubaApr 17.2007 — Itz better to validate in javascript itself.
Copy linkTweet thisAlerts:
@gtzpowerApr 17.2007 — I agree, validate in Javascript (also validate in PHP since javascript is easily bypassed)

also, your missing a single quote at the end of -> define ('db_password', 'xxx);
Copy linkTweet thisAlerts:
@CrucialApr 17.2007 — Without testing it myself, my first thought would be that this test isn't working as expected

if ($n && $em && $c && $co)

When I validate server side, I use a $form_good variable.

Something like this

[code=php]$form_good = true;

if (empty($_GET['input_one'])) {
$response_message = "Please complete field x";
$form_good = false;
}

if (empty($_GET['input_two'])) {
$response_message = "Please complete field x";
$form_good = false;
}

if($form_good) {
// do sql stuff
}[/code]
×

Success!

Help @ninjetti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...