2016-08-11 9 views
0

私は連絡先ページの設定についてのチュートリアルに従っていましたが、送信時にsubmitFormが定義されていないというエラーメッセージがコンソールに表示されていました。私の問題は別のWebサイトで同じコードを扱うことですが、まったく同じコードをコピーするとうまくいきません。ここに私のjsのコードは次のとおりです。php Contact Formリファレンスエラー

function _(id){ return document.getElementById(id); } 
function submitForm(){ 
    _("mybtn").disabled = true; 
    _("status").innerHTML = 'please wait ...'; 
    var formdata = new FormData(); 
    formdata.append("n", _("n").value); 
    formdata.append("e", _("e").value); 
    formdata.append("m", _("m").value); 
    var ajax = new XMLHttpRequest(); 
    ajax.open("POST", "example_parser.php"); 
    ajax.onreadystatechange = function() { 
     if(ajax.readyState == 4 && ajax.status == 200) { 
      if(ajax.responseText == "success"){ 
       _("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>'; 
      } else { 
       _("status").innerHTML = ajax.responseText; 
       _("mybtn").disabled = false; 
      } 
     } 
    } 
    ajax.send(formdata); 
} 

、ここでは私のPHPコードは次のとおりです。

<?php 
if(isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m'])){ 
    $n = $_POST['n']; // HINT: use preg_replace() to filter the data 
    $e = $_POST['e']; 
    $m = nl2br($_POST['m']); 
    $to = "skoolboi[email protected]"; 
    $from = $e; 
    $subject = 'Contact Form Message'; 
    $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>'; 
    $headers = "From: $from\n"; 
    $headers .= "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    if(mail($to, $subject, $message, $headers)){ 
     echo "success"; 
    } else { 
     echo "The server failed to send the message. Please try again later."; 
    } 
} 
?> 

あなたは私が機能を定義したが、そのエラーを取得して見ることができるように。どんな助けでも大歓迎です。

+0

どのようにHTMLにjavascriptを入れていますか? HTMLページの '

関連する問題