2017-05-29 88 views
0

我是jquery,php和ajax的新手,並且在一位同事的幫助下編寫了這段代碼。.focus()和電子郵件功能

我的代碼:

if(name == ''){ 
    alert("Please enter your name"); 
    $("#name", first).focus(); 
    status = 'N'; 
} 
else { 
    status = 'Y'; 
}; 

if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { 
    alert("Not a valid e-mail address"); 
    $("#email", second).focus(); 
    status = 'N'; 
} 
else { 
    status = 'Y'; 
}; 

if(message == ''){ 
    alert("Please enter your message"); 
    $("#message", third).focus(); 
    status = 'N'; 
} 
else{ 
    status = 'Y'; 
}; 

if(status == 'Y'){ 
    $.ajax({ 
     type: "POST", 
     url: url, 
     data: {name:name, email:email, phone:phone, message:message}, 
     success: function(data) 
     { 
      alert(data); 
      $("message_span").html(data); 
           // show response from the php script. 
     } 

}; 

所以我的問題是,我想.focus()把重點放在其未按升序排列填補文本區域,這是不會發生,這就是爲什麼我m使用第一個,第二個參數。

現在第二個問題是,AJAX是不是給我「成功發送郵件」的彈出是存在在我的PHP代碼

我的PHP代碼:

$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$message=$_POST['message']; 

$to='myemail'; 
$subject='Form Submition'; 
$message='Name: '.$name.'\n Phone: '.$phone.'\n Wrote the following messsage \n\n'.$message; 
$headers='From: '.$email; 
echo 'Message successfully sent'; 

任何幫助非常感謝,如果之前詢問過這個問題,我很抱歉,但我無法找到它。 謝謝!

回答

0

這裏是一個示例代碼來幫助你。試試看:

$("#submit").click(function() { 
 

 
    var status = 'Y'; 
 
    if ($("#name").val() == '') { 
 
    alert("Please enter your name"); 
 
    $("#name").focus(); 
 
    status = 'N'; 
 
    return false; 
 
    } 
 

 
    /* if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) { 
 
     alert("Not a valid e-mail address"); 
 
     $("#email").focus(); 
 
     status = 'N'; 
 
    } 
 
    */ 
 
    if ($("#message").val() == '') { 
 
    alert("Please enter your message"); 
 
    $("#message").focus(); 
 
    status = 'N'; 
 
    return false; 
 
    } 
 

 
    if (status == 'Y') { 
 
    $.ajax({ 
 
     type: "POST", 
 
     url: url, 
 
     data: { 
 
     name: name, 
 
     email: email, 
 
     phone: phone, 
 
     message: message 
 
     }, 
 
     success: function(data) { 
 
     alert(data); 
 
     $("message_span").html(data); 
 
     // show response from the php script. 
 
     } 
 
    }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' id='name' /> 
 
<input type='text' id='message' /> 
 

 

 
<button id="submit">Submit</button>

+0

作品。非常感謝你的幫助! –

+0

不客氣! –

相關問題