1

入力用の HTML5 検証 (必要に応じて追加され、名前/電子メールを入力) を備えた、動作する php 連絡フォームがありますが、1 つの問題があります。

メッセージが送信されると、新しいタブが開き、メッセージが送信されたことに感謝します。これは、process.php のため正常です。

<?php
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, 
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your message.'; 
//if the errors array is empty, send the mail
if (!$errors) {
    //recipient - change this to your name and email
    $to = 'youremail@email.com';    
    //sender
    $from = $email;

    //subject and the html message
    $subject = 'Hello from ' . $name;    
    $message = '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
    <table>
        <tr><td>Name: </td><td>' . $name . '</td></tr>
        <tr><td>Email: </td><td>' . $email . '</td></tr>
        <tr><td>Message: </td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';
    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;    
    }
//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="index.html">Back</a>';
    exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}
?>

しかし、送信ボタンの下にメッセージを表示するにはどうすればよいですか? これはHTML側です

    <form method="post" action="process.php">
<div class="element">
    <label><i class="icon-user"></i></label>
    <input type="name" name="name" placeholder="write your name here" class="text" required/>
</div>
<br>
<div class="element">
    <label><i class="icon-envelope"></i></label>
    <input type="email" name="email" placeholder="write your e-mail here" class="text" required/>
</div>
<br>
<div class="element">
    <label><i class="icon-comment"></i></label>
    <textarea name="comment" placeholder="write your message here" class="text textarea"required></textarea>
</div>
<br>
<div class="element el-submit">

    <input class="submit" type="submit" id="submit"/>

</div>
</form>

つまり、送信ボタンを押して大丈夫です「メッセージが送信されました」のようなメッセージを表示したいのですが、新しいページではなく、送信ボタンの下に表示したいのです

4

3 に答える 3

0

次のページでそれを表示したくない場合は、ありがとうメッセージを表示するためのポップアップを作成してください。このようにhtmlコードを使用してください。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
function myFunction()
{
alert("Thanks for  mail!");
}
</script>
</head>

<body>
<form method="post" action="process.php">
<div class="element">
    <label><i class="icon-user"></i></label>
    <input type="name" name="name" placeholder="write your name here" class="text" required/>
</div>
<br>
<div class="element">
    <label><i class="icon-envelope"></i></label>
    <input type="email" name="email" placeholder="write your e-mail here" class="text" required/>
</div>
<br>
<div class="element">
    <label><i class="icon-comment"></i></label>
    <textarea name="comment" placeholder="write your message here" class="text textarea"required></textarea>
</div>
<br>
<div class="element el-submit">

    <input type="button" onclick="myFunction()" value="Submit">

</div>
</form>
</body>
</html>
于 2013-08-22T12:00:58.033 に答える