/    Sign up×
Community /Pin to ProfileBookmark

Blank Fields

Hi, i am currently using this to check for blank fields.

[code=php]
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ”))
return false;
}
return true;
[/code]

but this checks every field, how can i exclude a certain fields from here as it is optional. Either that or get it to check the fields i say.

Thanks Adam

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@squallflightAug 06.2004 — hmm

why dont you put OPT_ in front of the name of the optional fields and then use the strstr function to check for OPT_ and if it is found to ignore that $key and continue looping like so
[code=php]
if(isset[$_POST['submit'])
{
foreach ($form_vars as $key => $value)
{
if(!strstr($key, 'OPT_'))
{
if ($value == '')

return false;
}
}

return true;
}

[/code]

as far as am aware as long as $key is a var made by the foreach statement it will always be set so the isset fuction was a bit pointless really.

if you give the submit button a name i.e. submit you can use that to ensure that the form has been submitted and all fields will be set as they are stored in the $_POST global var regardless if they have values or not because they are from the same form.

strstr() looks for a needle in a haysack.
Copy linkTweet thisAlerts:
@k0r54authorAug 06.2004 — ok that dont seam to be workin either that or it is what i have done?

[code=php]
<?php
require_once('data_valid')

if (!fields($HTTP_POST_VARS)){
header("Location : ../html/contact_us_nf.htm");
}

//check that the correct fields are field in and that the email is right
if (!valid_email($email)){
header("Location : ../html/contact_us_nfe.htm");
exit;
}

[/code]


this is how i am calling it

and this is the data_valid functions

[code=php]
<?php
function fields(form_vars)

if(isset[$_POST['submit'])
{
foreach ($form_vars as $key => $value)

{

if(!strstr($key, 'OPT_'))
{
if ($value == '')

return false;

}
}

return true;
}

function valid_email($address)
{
// check an email address is possibly valid
if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $address))
return true;
else
return false;
}
?>
[/code]


The error message i am getting is :

Parse error: parse error, unexpected T_IF in /home/sites/apc-compunet.co.uk/public_html/webdev/webtemp/AL3PHP1UD2/functions/mail.php on line 14

Line 14 is if (!fields($HTTP_POST_VARS)){ bit and when i take it out the same problem happens at if (!valid_email($email)){

Thanks adam (sorry bout the length)
Copy linkTweet thisAlerts:
@squallflightAug 06.2004 — Try this:
[code=php]
require_once('data_valid');
[/code]

you've just made a common mistake by myself and that is you forgot the semi-colon at the end of the line.

One thing I've learned is to alway check for semi-colons, god the amount of times I've forgot them is scary. I'd rack my brain for ages thinking why? why? WHY WONT IT WORK? I'd goe through everything and repeatedly overlook the semi-colon.

When ever it says Unexpected T_<whatever> it usually means u've messed up with the syntax sumwhere
Copy linkTweet thisAlerts:
@squallflightAug 06.2004 — oh and you missed off the {} in the function page

Try this:
[code=php]
function fields(form_vars)
{
if(isset[$_POST['submit'])

{

foreach ($form_vars as $key => $value)

{

if(!strstr($key, 'OPT_'))

{

if ($value == '')

return false;

}

}

return true;

}
}
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 06.2004 — Hi, its still the same error?
Copy linkTweet thisAlerts:
@Paul_JrAug 06.2004 — This line&#8230;
[code=php]
if(isset[$_POST['submit'])

[/code]

Should be like this&#8230;
[code=php]
if(isset($_POST['submit']))
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 06.2004 — ok after goin through a series of problems, it is now saying that it worked, but i know it aint, coz i aint got any email lol i did work until u put in the validations

I hate attached the pages if some1 could perhaps take a look at them

Thanks Adam

[upl-file uuid=ca3f6844-e930-4fbd-953d-9815e0d6d40b size=1kB]data_valid.zip[/upl-file]
Copy linkTweet thisAlerts:
@squallflightAug 08.2004 — Hmm not much of a problem here just a few things missed off

Just change what I've shown here
[code=php]
require_once('data_valid.php');
[/code]

and
[code=php]
//set up the email
$recipient = "$_POST[emailtest]";
$subject = "PHP web form result, this can be changed";
$mailheaders = "From: APC-Compunet Demo Site <[email protected]> n";
$mailheaders .= "Reply-To: $_POST[email]n";
$mailheaders .= "X-Mailer: PHP/".phpversion();

//send mail
mail($recipient, $subject, $msg, $mailheaders);
[/code]


Some email servers require the X-Mailer header otherwise it classes it as automated email (i.e. spam) and ignores it (or so i've been told).

Finally change your data valid to this
[code=php]
<?php
function valid_email($address)
{
// check an email address is possibly valid
if (ereg('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $address))
{
return true;
}
else
{
return false;
}
}
?>
[/code]

you didn't have the {} in the if statement

but I dn't see why mail() didn't work
Copy linkTweet thisAlerts:
@k0r54authorAug 14.2004 — ok i got it working by doin this

[code=php]
function filled_out_apcreg($form_vars)
{

foreach ($form_vars as $key => $value)

{

if(substr($key, 0, 4) != "OPT_")

{
if ($value == '')

return false;

}

}

return true;

}
[/code]


I took out the submit bit.

Ok now the next thing is this

i am guesing it is something like this but i am not to sure

[code=php]
//Used to ignore the APC Registration bits
function filled_out($form_vars)
{

foreach ($form_vars as $key => $value)

{

if(substr($key, 0, 4) != "OPT_")

{

if(substr($key, 0, 4) != "APCREG_")

{
if ($value == '')

return false;

}
}

}

return true;

}
[/code]


What i need is that it ignores both OPT_ and APCREG_

Any idea's thanks adam
Copy linkTweet thisAlerts:
@Paul_JrAug 14.2004 — [code=php]
function filled_out($form_vars) {

foreach ($form_vars as $key => $value) {

if((substr($key, 0, 4) != 'OPT_') && (substr($key, 0, 7) != 'APCREG_')) {

if ($value == '') {
return false;
}
}
}
return true;

}
[/code]

Try that.
Copy linkTweet thisAlerts:
@k0r54authorAug 14.2004 — Yep works fine, y didn't i think of doin it like that lol

Thanks Alot
Copy linkTweet thisAlerts:
@Paul_JrAug 14.2004 — You&#8217;re welcome, glad I could help. ?
×

Success!

Help @k0r54 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...