如何将php文件连接到HTML表单?

时间:2020-07-28 07:29:29

标签: php html forms email action

我需要使用(mailto无效)

形式的数据发送电子邮件

以某种方式,php文件未连接到html。当我在本地运行index.html时,如果按 submit ,浏览器将显示php代码,而不执行其他任何操作。当我使用localhost时,如果我按下 submit ,则会抛出405 Error

PLS帮助,因为我不是PHP开发人员。

<form method="POST" name="myName" action="registration.php">
  <textarea name="message"></textarea>
  <div class="form_submit">
    <input type="submit" value="Submit" class="form_submit-btn">
   </div>
</form>


<?php

if($_POST["message"]) {

mail("myemail@gmail.com", "Here is the subject line",

$_POST["insert your message here"]. "From: an@email.address");

}

?>

3 个答案:

答案 0 :(得分:0)

您对mail函数的调用看起来不太正确-调用的headers元素似乎附加在邮件正文中,而不是作为单独的参数。

<?php
    # registration.php
    
    error_reporting( E_ALL );
    ini_set( 'display_errors', 1 );

    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['message'] ) ){
        ob_clean();
        
        $lb="\r\n";
        
        $recipient='geronimo@example.com';
        $message=$_POST['message'];
        $subject='This is a test';
        $headers=array(
            'From: webmaster@example.com',
            'Reply-To: webmaster@example.com',
            'Cc: joe.bloggs@example.com'
        );
        
        $status=mail( $recipient, $subject, $message, implode( $lb, $headers ) ;
        exit( header( sprintf('Location: index.html?mailsent=%s', $status ) ) );
    }
?>





<!-- index.html -->
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form method='POST' action='registration.php'>
            <textarea name='message'></textarea>
            <div class='form_submit'>
                <input type='submit' value='Submit' class='form_submit-btn'>
            </div>
        </form>
    </body>
</html>

答案 1 :(得分:-1)

将文件index.html重命名为index.php。抛出405错误,因为index.html无法接受POST请求。在这种情况下,您需要使用服务器端语言处理表单。将您的index.html更改为index.php。

答案 2 :(得分:-1)

将文件从index.html重命名为index.php