JavaScript 验证 PHP 电子邮件使用字段中添加的信息


javascript validation php email using information added in fields

im currnetly 制作一个网站,我卡住了,我有一个表格

    <form name="inputForm" onSubmit="return validateForm();" action="#">
<fieldset>
    <p>
      <label>First Name:</label>
      <input type="text" name="first_name"/><br />
      <label>Surname:</label>
      <input type="text" name="surname"/><br />
      <label>Address number:</label>
      <input type="text" name="address_number"/><br />
      <label>Address name:</label>
      <input type="text" name="address_name"/><br />
      <label>Suburb:</label>
      <input type="text" name="suburb"/><br />
      <label>Postcode:</label>
      <input type="text" name="postcode"/><br />      
      <label>State:</label>
      <select name="state">
            <option value="nsw">NSW
            <option value="qld">QLD
            <option value="act">ACT
            <option value="vic">VIC
            <option value="sa">SA
            <option value="tas">TAS
            <option value="nt">NT
            <option value="wa">WA
          </SELECT><br />
      <label>Email:</label>
      <input type="text" name="email"/><br />
      <label>Phone Number:</label>
      <input type="text" name="phone"/><br />
      <input type="submit" name="submit" value="Send form" />
      <input type="reset" name="reset" value="reset" />
      </p>
</fieldset>
</form>

然后由JavaScript验证

function validateForm()
{
    var form = document.forms['inputForm'];
    var formats = 
        {
            first_name: /^[a-zA-Z]+['-''s]?[a-zA-Z]+$/,                             /*works for a-Z allows - and '*/
            surname: /^[a-zA-Z]+['-''s]?[a-zA-z]+$/,                                /*works for a-Z allows - and '*/
            postcode: /^'d{4}$/,                                                    /*4digit post code australia wide*/
            email: /^'w+(['.-]w+)*@'w+(['.-]?'w+)*('.'w{2,4})+$/,                   /*allows all word characters and normal email formats*/
            address_number: /^'d[0-9]$/,                                                    /*allows any number of digits*/
            address_name: /^'w?'s?[a-zA-Z]+(,?'s([a-zA-Z])*)*$/,            /*allows numbers space capital letters and other word characters*/
            suburb: /^'w?'s?[a-zA-Z]+(,?'s([a-zA-Z])*)*$/,          /*allows numbers space capital letters and other word characters*/
            phone: /^'d{8}$/,                                                       /*8 number phone number*/
        };
        var elCount = form.elements.length;
        for(var i = 0; i<elCount; i++)
        {
            var field = form.elements[i];
            if(field.type == 'text')
            {
                if(!formats[field.name].test(field.value))
                {
                    alert('invalid '+ field.name.replace('_',' ')+'.');             /*alerts the name of the area not filled right in a pop up box*/
                    field.focus();
                    return false;
                }
            }
        }
        alert('All fields correct, the form will now submit.')
}

然后我需要PHP来收集这些信息并将其发送到我的电子邮件地址(这是IM卡住的地方)

<?php
if( isset( $_POST['submit'] ) || isset( $_POST['submit_x']))
{
    $to = "myemail@myemail.com";
      $subject = 'subject';
      $message = 'testing the php mail() function';
      $headers = "from: email'r'n";

        $body=" 
        Name: $name'n
        Email: $email'n
";
echo "the information has been sent we will be in contact with you shortly";
mail($to, $subject, $message, $headers);    
}
else
{
    echo "The informations has not been sent";
}
?>

现在我已经为此挠头几天了,查找找不到我仍在学习PHP和JavaScript的任何内容

JavaScript和HTML工作正常,它只是邮件功能请帮助

首先,将 method="post" 添加到表单中,因此它看起来像这样:

下一个

 $headers = "from: email'r'n";

 $headers = "from: {$_POST['email']}'r'n";

您需要指定方法。

form method="post"

而且我不确定action="#"是否也是正确的使用方式

尝试在 PHP 中进行电子邮件验证的 isemail。它是最全面的电子邮件验证脚本。它遵循所有关于电子邮件的 RFC。