/    Sign up×
Community /Pin to ProfileBookmark

Email form sending – sends email when page loaded

Hi ive got a form that sends a persons e-mail address when they type it into the form and submit it which is what it tis supposed to do but it also send the form info (which is blank) when the page/file loads.

Can anyone tell me how to correct this problem, thanks

This is my php file code:

<?php
// get posted data into local variables
$EmailTo = “[email protected]“;
$Subject = “Mailing list”;
$Email = Trim(stripslashes($_POST[‘Email’]));

// prepare email body text
$Body = “”;
$Body .= “Email: “;
$Body .= $Email;
$Body .= “n”;

// send email
$success = mail($EmailTo, $Subject, $Body, “From: <$EmailFrom>”);

?>

<html>

<head>
</head>

<body>

//(some html code here)

<form method=”POST” action=”<?php echo $GLOBALS[“PHP_SELF”]; ?>”>
<p style=”margin-top: 1; margin-bottom: 1″><input type=”text” name=”Email” size=”15″ style=”color: #000000; font-family: Arial; font-size: 11px” value=”Your E-mail address”>&nbsp;

<input type=”submit” value=”Join” name=”B1″ style=”color: #000000; font-family: Arial; font-size: 11px” onClick=”alert(‘Thank you signing up to our mailing list.’); return true”></p>
</form>

</body>

</html>

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@bokehJul 24.2005 — Try something like the following.[code=php]<?php

$value = 'Your E-mail address';

if(isset($_POST['B1'])){

$value = $_POST['Email'];

// get posted data into local variables
$EmailTo = "[email protected]";
$Subject = "Mailing list";
$Email = Trim(stripslashes($_POST['Email']));

// prepare email body text
$Body = "";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "n";


// send email
$CheckEmail = trim($_POST['Email']);
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,6})$";
if (eregi($regexp, $CheckEmail)){
$success = @mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}

}

echo <<< HEAD

<html>

<head>
</head>

<body>

HEAD;


if($success){

print 'Your message was sent';

}elseif(isset($_POST['B1']) && $success == FALSE){

print 'Message sending failed! Please check your details and try again!<br><br>';

}

if(!isset($_POST['B1']) | $success == FALSE){

echo <<< FORM


<form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">
<p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

<input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>
</form>

FORM;

}



echo <<< FOOT

</body>

</html>

FOOT;

?>[/code]
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 24.2005 — Thanks but when i open the file in IE it displays an error on line 30.

Im a beginer at writing php so the code you suggested is a bit hard to uderstand, is it possible to keep my origional code but just add a couple of lines of code that will make it send the info only when the submit button is pressed and if not to do nothing?

Thanks for helping
Copy linkTweet thisAlerts:
@bokehJul 24.2005 — PHP is not browser specific. It works in Firefox so it must work in IE unless you have changed something. I am a beginner too and I have just made a couple of changes so it works properly.
Copy linkTweet thisAlerts:
@CompGeek01Jul 24.2005 — Hm. I've never seen it done that way. Every must have their own styles but I can't seem to find refrences to the <<< operator (?).

[code=php]
// Inaccuracies, use the other suggested form for lack of me wanting to style to my liking :)
[/code]


If it errors out on a line, please share the error message. I just changed how I would integrate the HTML, nothing logical.
Copy linkTweet thisAlerts:
@bokehJul 24.2005 — My personal peev is code that keeps switching in and out of PHP, like this: ?> some html <?php etc. It's very uncomfortable. If you don't know the "Heredoc" construct you [URL=http://es2.php.net/language.types.string]can read about it here[/URL] in the PHP manual. I'm surprised you aren't familiar with it though having been programing for so long.

Just briefly there are 3 forms of string literal:

Single quoted

Double quoted

and Heredoc syntax.
Copy linkTweet thisAlerts:
@CompGeek01Jul 24.2005 — I guess you just get into a groove. I live by the PHP manual, just never seen that page. I really like being able to switch in and out of PHP with "<?", it's comfortable for me. Thanks for the information. ?
Copy linkTweet thisAlerts:
@bokehJul 24.2005 — The code you posted just will not work. For a start you have this: '

<form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">' and '$value' in an html section. That's the whole reason why I don't want to keep switching between PHP and HTML.

Also I don't understand why you are recommending these changes when my script works properly and yours doesn't work at all.
Copy linkTweet thisAlerts:
@CompGeek01Jul 25.2005 — Edit, decided it best to just leave yours, if it works it works I guess. ? Will teach me to throw something up and not check it for syntax I hadn't seen. I do see how it helps with things, but my version of UltraEdit doesn't highlight it as PHP ?
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 25.2005 — Hi, the script given by bokeh in reply #2 displays this error when opened in IE.

Parse error: parse error, expecting ','' or ';'' in /vhost/vhost2/t/h/e/theparoles.co.uk/www/mailing.php on line 30


What about this: (it also displays an error on line 203)


<?php

if(isset($_POST['B1'])){

$value = $_POST['Email'];

// get posted data into local variables

$EmailTo = "[email protected]";

$Subject = "Mailing list";

$Email = Trim(stripslashes($_POST['Email']));

// prepare email body text

$Body = "";

$Body .= "Email: ";

$Body .= $Email;

$Body .= "n";

$CheckEmail = trim($_POST['Email']);


// send email

if (eregi($regexp, $CheckEmail)){

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

?>

<html>

<head>

<title>The Paroles - Welcome to the official website.</title>

</head>

<body>

<form method="POST" action="<?php echo $GLOBALS["PHP_SELF"]; ?>">
<p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="Your E-mail address">&nbsp;


<input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px" onClick="alert('Thank you signing up to our mailing list.'); return true"></p>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@bokehJul 25.2005 — 

Parse error: parse error, expecting ','' or ';'' in /vhost/vhost2/t/h/e/theparoles.co.uk/www/mailing.php on line 30


What about this: (it also displays an error on line 203)
[/QUOTE]
First I just copied the code I posted back out of the forum and guess what? It works flawlessly in IE and Firefox.

Line 30 is this: "echo <<< HEAD" My guess is you have added something after this. There must be an imediate return after the D and absolutely no white space.

I dont follow your second point about line 203. It is a mystery as the script only has 74 lines.

I'm sorry but I can't help you further. My time is precious to me and I can't waste it give advice to someone who is incapable of even copying and pasting working code without damaging it.
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 25.2005 — Sorry if i'm not understanding. Ive done as you say and line 30 does not have an error any more, but line 45 give this error:

Parse error: parse error, expecting T_STRING' or T_VARIABLE' or `T_NUM_STRING' in /vhost/vhost2/t/h/e/theparoles.co.uk/www/mailing.php on line 45
Copy linkTweet thisAlerts:
@bokehJul 25.2005 — Re-post the script back up here so I can see what the error is.
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 25.2005 — <?php

$value = 'Your E-mail address';

if(isset($_POST['B1'])){

$value = $_POST['Email'];

// get posted data into local variables

$EmailTo = "[email protected]";

$Subject = "Mailing list";

$Email = Trim(stripslashes($_POST['Email']));

// prepare email body text

$Body = "";

$Body .= "Email: ";

$Body .= $Email;

$Body .= "n";


// send email

$CheckEmail = trim($_POST['Email']);

$regexp = "^([_
a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,6})$";

if (eregi($regexp, $CheckEmail)){

$success = @mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

}

}

echo <<< HEAD

<html>

<head>

</head>

<body>

HEAD;

if($success){

print 'Your message was sent';

}elseif(isset($_POST['B1']) && $success == FALSE){

print 'Message sending failed! Please check your details and try again!<br><br>';

}

if(!isset($_POST['B1']) | $success == FALSE){

echo <<< FORM


<form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">

<p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

<input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>

</form>

FORM;

}



echo <<< FOOT

</body>

</html>

FOOT;

?>
Copy linkTweet thisAlerts:
@bokehJul 25.2005 — That runs fine for me
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 25.2005 — I copied the code and deleted all the spaces after each line, like u said for one of the lines before:

"There must be an imediate return after the D and absolutely no white space."

And it works great for me to!! thanks a lot for your time and patientce!
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 25.2005 — i do have just one more thing i need help with, where it says:

if($success){

print 'Your message was sent';


Could i have this as an alert instead, i tried changing print to alert but that didn't work. Also, i don't want it to display (print) text to say please fill in your details again, i would also like this to be an alert.

Thanks
Copy linkTweet thisAlerts:
@james_d_kellyauthorJul 26.2005 — Ok ive fixed that problem now but ive gt another:

The code all works apart from when you submit a valid e-mail address and then refresh or re-visit the page, where it posts the e-mail adress you submitted again as the page is loading.

  • - perhaps the form/page needs resetting somehow...??


  • Its a bit of a challenge but could anyone solve the problem. Any help will be much appriciated, thanks.


    <?php

    $value = 'Your E-mail address';

    if(isset($_POST['B1'])){

    $value = $_POST['Email'];

    // get posted data into local variables

    $EmailTo = "[email protected]";

    $Subject = "Mailing list";

    $Email = Trim(stripslashes($_POST['Email']));

    // prepare email body text

    $Body = "";

    $Body .= "Email: ";

    $Body .= $Email;

    $Body .= "n";


    // send email

    $CheckEmail = trim($_POST['Email']);

    $regexp = "^([_
    a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,6})$";

    if (eregi($regexp, $CheckEmail)){

    $success = @mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    }

    }

    echo <<< HEAD




    <html>

    <head>

    </head>

    <body>

    //my web pgae html code goes here


    HEAD;

    if($success){

    echo "<script language="javascript">n";

    echo "<!--n";

    echo "alert("Thank you, your request has been sent.");";

    echo "n//-->";

    echo "</script>";


    print <<< FORM

    <form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">

    <p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

    <input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>

    </form>

    FORM;

    }


    elseif(isset($_POST['B1']) && $success == FALSE){

    echo "<script language="javascript">n";

    echo "<!--n";

    echo "alert("Message sending failed! Please check your details and try again!");";

    echo "n//-->";

    echo "</script>";

    }

    if(!isset($_POST['B1']) | $success == FALSE){

    echo <<< FORM


    <form method="POST" action="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}">

    <p style="margin-top: 1; margin-bottom: 1"><input type="text" name="Email" size="15" style="color: #000000; font-family: Arial; font-size: 11px" value="$value">&nbsp;

    <input type="submit" value="Join" name="B1" style="color: #000000; font-family: Arial; font-size: 11px"></p>

    </form>

    FORM;

    }



    echo <<< FOOT


    </body>

    </html>

    FOOT;

    ?>
    ×

    Success!

    Help @james_d_kelly 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.24,
    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,
    )...