CustomX
CustomX

Reputation: 10113

PHP - send two fields through mail

I have a document with 2 textboxes and 1 submit button. I want to send an email when I press the submit button and in it should be the value of the textboxes. My PHP knowledge is a bit dusty. I don't want to bother the user with the emailproces, so he can't see this and should just be redirected.

<html>
<body>

<div class="section_form" id="usernameSection"> 
<label for="username">Login:</label> 
<input size="20" type="text" name="username" id="username" /> 
</div> 

<div class="section_form" id="emailSection"> 
<label for="email">Email:</label> 
<input size="20" type="text" id="email" name="email" maxlength="20"/> 
</div> 

<div id="submit_button"> 
<button type="submit" value="Submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button> 
</div>

</body>
</html>

Many thanks!

Upvotes: 0

Views: 73

Answers (4)

Alex Jose
Alex Jose

Reputation: 464

Use this code for your task.

<?php 
if(isset($_POST['submit'])){
    extract($_POST);
    $message = "username : $username ; email : $email";
    $to = "[email protected]";
    $subject  = "Email Subject";
    mail($to, $subject, $message);
    }
    ?>

<html>
<body>
<form method="POST" name="form1">
<div class="section_form" id="usernameSection"> 
<label for="username">Login:</label> 
<input size="20" type="text" name="username" id="username" /> 
</div> 

<div class="section_form" id="emailSection"> 
<label for="email">Email:</label> 
<input size="20" type="text" id="email" name="email" maxlength="20"/> 
</div> 

<div id="submit_button"> 
<button type="submit" value="Submit" name="submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button> 
</div>
</form>
</body>
</html>

Upvotes: 0

ssapkota
ssapkota

Reputation: 3302

Start with:
- $_POST
- mail()

Also you need to check: Forms in HTML, you might want to use form to send the data of input fields to server.

Upvotes: 0

Shadikka
Shadikka

Reputation: 4276

Basically the core of what you're looking for is

<?php
// Check that all fields are present, construct the message body, etc
mail($to, $subject, $body);
header("Location: wherever.php");
exit();
?>

See the mail function in PHP documentation. (Also, thank you Alex for the exit() reminder.)

Upvotes: 1

Chris Grant
Chris Grant

Reputation: 2385

Use the PHP mail function - listed here.

That should be able to help.

Upvotes: 2

Related Questions