0

我正在制作一个网站,但我卡住了我有一个表格

    <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 来收集这些信息并将其发送到我的电子邮件地址(这是我卡住的地方)

<?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工作正常,它只是邮件功能,请帮忙

4

3 回答 3

0

你需要指定方法..

form method="post"

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

于 2012-04-10T07:04:23.120 回答
0

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

于 2012-04-10T07:10:28.833 回答
0

首先,将 method="post" 添加到表单中,如下所示:

下一个

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

 $headers = "from: {$_POST['email']}\r\n";
于 2012-04-10T07:04:57.293 回答