/    Sign up×
Community /Pin to ProfileBookmark

I haven’t done any PHP studying for months now and guess it’s left me completely… Coz I have a simple contact for here and for the life of me, I can’t get it to work.

[code=php]ini_set(“sendmail_from”, “my server email address”);
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}if (isset($_REQUEST[’email’]))
{//if “email” is filled out, proceed //check if the email address is invalid
$mailcheck = spamcheck($_REQUEST[’email’]);
if ($mailcheck==FALSE)
{
echo “Invalid input”;
}
else
{//send email
$email = $_REQUEST[’email’] ;
$subject = $_REQUEST[‘subject’] ;
$message = $_REQUEST[‘message’] ;
mail(“YOUR EMAIL ADDRESS”, “Subject: $subject”,
$message, “From: $email” );
echo “Thank you for using our mail form”;
}
}
else
{//if “email” is not filled out, display the form
echo “<form method=’post’ action=’mailform.php’>
Email: <input name=’email’ type=’text’ /><br />
Subject: <input name=’subject’ type=’text’ /><br />
Message:<br />
<textarea name=’message’ rows=’15’ cols=’40’>
</textarea><br />
<input type=’submit’ />
</form>”;
}[/code]

First, it doesn’t send. Second… It doesn’t validate when there are errors. Any help would be appreciated.

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@jblackwelderDec 18.2008 — Try to see if this will fix your problems:

Change if (isset($_REQUEST['email']))

to

if (isset($_REQUEST['submit']))

and add name='submit' on your button
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 18.2008 — Try to see if this will fix your problems:

Change if (isset($_REQUEST['email']))

to

if (isset($_REQUEST['submit']))

and add name='submit' on your button[/QUOTE]


Nope... doesn't work.

> On our windows server you will need to define this command at the top of
> the script :
>
> ini_set("sendmail_from", " [email][email protected][/email] ");

[/QUOTE]


I remember that line needed to be added, according to my hosting company.

But, it still doesn't send or validate. I can type anything into any of the fields and it acts as if the email has gone thru.

I got this code off of w3schools.com
Copy linkTweet thisAlerts:
@MindzaiDec 19.2008 — What's getting returned from the mail function?
Copy linkTweet thisAlerts:
@SyCoDec 19.2008 — Debug this (and any script) by stripping it down, logically and methodically ruling out things that might cause the problem. Turn error handling up to E_ALL to get all messages that might be causing the problems.

Dees mail() work? Try a running a script with only the mail function in and not variable in the function call eg
[code=php]<?php
mail('[email protected]','mysubject','mymessage');
?>[/code]


Just that and nothing else it you get your mail then mail() is configured correctly if not the error goes beyond your script.

Next echo or print_r() the variables you expect to be present to see what you have.

By now you have a working mail() function and the expected vars so build the script back up piece by piece,checking it each time, until it breaks. When it does you've found your problem.

Yo do this often enough and you'll get much faster at spotting the errors even if you do take a break from coding, the methodical way you approach it will always work. It's the same way of fixing anything.

Just a coupe of other things that wont fix your problem.

[code=php]$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";[/code]


If the function returns true or false you can use if direct in the if()

[code=php] if (spamcheck($_REQUEST['email'])){
echo "Invalid input";
[/code]



You don't need to rename a variable for no reason, just use it as is.
[code=php] $email = $_REQUEST['email'] ;[/code]

You've already validated the REQUEST['email'] var, which is good and prevents the header injection so you can now use it directly. Te extra step is unnecessary.
[code=php]
$message, "From: $_REQUEST['email']l" ); [/code]
Copy linkTweet thisAlerts:
@MindzaiDec 19.2008 — 
Just a coupe of other things that wont fix your problem.

[code=php]$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";[/code]


If the function returns true or false you can use if direct in the if()

[code=php] if (spamcheck($_REQUEST['email'])){
echo "Invalid input";
[/code]

[/QUOTE]


Just to clarify as I think SyCo made a typing error, that code should be:

[code=php] if (!spamcheck($_REQUEST['email'])){
echo "Invalid input";
[/code]


The ! before the function name inverts the returned value, ie it checks for false in this case.
Copy linkTweet thisAlerts:
@SyCoDec 19.2008 — Doh! thank you!
Copy linkTweet thisAlerts:
@bluestartechDec 20.2008 — a javascript check on the email address would be a better practice here
Copy linkTweet thisAlerts:
@SyCoDec 20.2008 — JavaScript should never be used for actual validation, just a convenience to the user. You turn off jS, you turn of validation.

As the REQUEST vars are being checked a jS check on the email would allow for a header injection attack.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 21.2008 — What's getting returned from the mail function?[/QUOTE]

I'm not getting anything returned from anything. When I click submit... it goes thru the the next page and says thanks for contacting us.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 21.2008 — SyCo...

Starting from scratch now.

This is the simplified version of that script

[code=php]<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "[email protected]", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>[/code]


plus the... [code=php]ini_set("sendmail_from", " [email protected] "); [/code] that goes at the top, as per hosting provider instructions. And it still doesn't go thru...

This line mail( "[email protected]", "Subject: $subject", and this line ini_set("sendmail_from", " [email][email protected][/email] "); I have changed the "sendmail_from" to the email address of the web server and the mail( "[email protected]", I changed to the email box I want to receive the emails too.
Copy linkTweet thisAlerts:
@SyCoDec 21.2008 — A really simple mail script is just

[code=php]<?php
mail('[email protected]','mysubject','mymessage');
?> [/code]

nothing else. Of course change the email to yours or you wont get it. However if you host has requirements and you've done what they ask and thisimple mail() dopesnt work, you need to contact your host.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 21.2008 — A really simple mail script is just

[code=php]<?php
mail('[email protected]','mysubject','mymessage');
?> [/code]

nothing else. Of course change the email to yours or you wont get it. However if you host has requirements and you've done what they ask and thisimple mail() dopesnt work, you need to contact your host.[/QUOTE]


<?php

ini_set("sendmail_from", " MyEmail.com ");

mail('MyOtherEmail.com','mysubject','mymessage');

echo "<form method='post' action='mailform.php'>

Email: <input name='email' type='text' /><br />

Subject: <input name='subject' type='text' /><br />

Message:<br />

<textarea name='message' rows='15' cols='40'>

</textarea><br />

<input type='submit' />

</form>";

?>

Right... Got it working. Yay!!! Where do we go from there???
Copy linkTweet thisAlerts:
@SyCoDec 21.2008 — So you've now done the first part of the debugging I suggested in post #5. Have a read and do the next thing. There are no shortcuts to debugging an unobvious problem. Sucks and boring I know but that's debugging ?
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 22.2008 — So you've now done the first part of the debugging I suggested in post #5. Have a read and do the next thing. There are no shortcuts to debugging an unobvious problem. Sucks and boring I know but that's debugging ?[/QUOTE]

Hahaha... I knew you were going to say that. Well, I'm afraid, that is where my PHP skills come to a complete halt. Writing functions is not my current strong point.

I have gotten the small email script working and also 'pimped' up the form. What I would like to attempt now, is try insert a drop down list, for options so that when is chosen, it will be the Subject of the email.
Copy linkTweet thisAlerts:
@SyCoDec 22.2008 — When someone on the forum asks you to try something you need to say what is happening at your end. What you did, what worked, what you didn't understand.
Where do we go from there??? [/QUOTE]
to
I have gotten the small email script working [/QUOTE]

So is the original script working as you wanted? Did you get were you were trying to originally before trying to pimp the form further? Without feedback, helping becomes a chore.

When a form submits the values are access via the $_POST array. if you print_r($_POST) you'll see the values (if any).

When you add a select list to your form, choose a value and hit submit, you'll see the values printed by print_r().

So you can access the values like any array value.

[code=php]echo $_POST['myselectname'];[/code]

And you can add them to your form.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 22.2008 — does the submit button show up in the text area???

[code=php]echo "<form method='post' action='mailform.php'>
<table width='300' border='1' cellspacing='0' cellpadding='0'>
<tr><td width='80'>Email:</td>
<td><input name='email' type='text' size='30' maxlength='50' /></td>
</tr>
<tr><td>Subject:</td>
<td><form name='form1' id='form1'>
<select name='menu1'>
<option>Modeling</option>
<option>Complaint</option>
<option>Comment</option>
</select>
</form>
</td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name='message' cols='23' rows='4></textarea></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
</form>";
?>[/code]
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 22.2008 — When someone on the forum asks you to try something you need to say what is happening at your end. What you did, what worked, what you didn't understand.

to


So is the original script working as you wanted? Did you get were you were trying to originally before trying to pimp the form further? Without feedback, helping becomes a chore.

When a form submits the values are access via the $_POST array. if you print_r($_POST) you'll see the values (if any).

When you add a select list to your form, choose a value and hit submit, you'll see the values printed by print_r().

So you can access the values like any array value.

[code=php]echo $_POST['myselectname'];[/code]

And you can add them to your form.[/QUOTE]


I've scraped the original script and thought that if I am going to learn PHP properly, then best to start from scratch and work from there. Like you suggested with the debugging. So, I've gotten the simple email script to work as I need it to. Now I am playing/learning how to do a layout for it. Next will hopefully be the Subject options... :-)
Copy linkTweet thisAlerts:
@SyCoDec 22.2008 — I've scraped the original script and thought that if I am going to learn PHP properly, then best to start from scratch and work from there.[/QUOTE]

I think that was going to be my next suggestion ?

Find a tutorial and have a read. There are a ton on the net already and you'll be able to find one that suits your style.

Good luck.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorDec 22.2008 — I think that was going to be my next suggestion ?

Find a tutorial and have a read. There are a ton on the net already and you'll be able to find one that suits your style.

Good luck.[/QUOTE]


Thanks SyCo... Much appreciated. Jumping the gun on this PHP stuff, especially since when I get to that chapter, my course will be covering PERL.
Copy linkTweet thisAlerts:
@SyCoDec 22.2008 — Previous xkcd comic might be appropriate then.

http://xkcd.com/519/

?
×

Success!

Help @Hooded_Villian 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.25,
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,
)...