2017-04-01 62 views
0

我正在研究一个代码,它会在表单提交后显示警报,然后重定向回主页。如果提交空表单,我想显示错误提示。我意识到我无法从onclick调用PHP,而是需要调用一个JS函数来启动我的PHP函数。有人可以告诉我,如果我的PHP代码是在正确的轨道上,以及如何初始化AJAX功能?试图在没有JQuery的情况下做到这一点。谢谢!如何从HTML/JS调用Ajax来启动PHP函数?

HTML

<form method="POST" action="contact.php"> 

<label for='message'>Leave a Message:</label> <br><br> 
    <textarea rows="15" cols="45" name="message" id="message" ></textarea> 
<br><br> 
    <input type="submit" name='submit' value="send!" onclick="startajax()"/> 

</form> 

和PHP

<?PHP 


$subject="New Message!"; 
$message=$_POST ["message"]; 

function leavemessage() { 

if(!empty($_POST['message'])) { 

echo '<script type="text/javascript"> 
function error(){ 
alert("Error!"); 
window.location.replace=(contact.html);} 
</script>'; 

} 

else 

{ 


{mail ($email, $subject, $message, "From:".$from);} 

echo '<script type="text/javascript"> 
function confirmation(){ 
alert("Message Sent!"); 
window.location.replace (index.html);} 
</script>'; 

} 

} 


?> 
+0

对你有语法错误,首先在你的PHP。在你正在做'window.location.replace'的两个位置。你需要在外部使用“整个字符串并使用'作为内部引号,或者跳过围绕HTML文件的单引号。 – TheValyreanGroup

+0

这个函数的位置在哪里?startajax()'?还是你希望我们写它对于你? –

+0

我还没有写它,我完全是新的编码,我主要想知道什么需要改变我的PHP,但所有的帮助表示感谢! – stone

回答

0

你可以做到这一点与jQuery Ajax来完成你的要求是这样的:

HTML:

<form method="POST" id="contact-form" onsubmit="return startajax()" > 

<label for='message'>Leave a Message:</label> <br><br> 
    <textarea rows="15" cols="45" name="message" id="message" ></textarea> 
<br><br> 
    <input type="submit" name='submit' value="send!"/> 

</form> 

PHP:

<?PHP 


$subject="New Message!"; 
$message=$_POST ["message"]; 
if(!empty($message)) { 
    //your email code here 
    exit(json_encode(array('error' => 0, 'errorMessage' => ''))); 
} else { 
    exit(json_encode(array('error' => 1, 'errorMessage' => 'Message is required'))); 

} 


} 


?> 

的Jquery:

<script> 
    var startajax = function(){ 
     var url = 'contact.php'; 
     $.ajax({ 
      url : url, 
      type : "POST", 
      dataType : "JSON", 
      data : $('#contact-form').serialize(), 
      success : function(response) { 
       if (response.error == 0) { // success 
        $('#contact-form')[0].reset(); 
        alert('SUCCESS MESSAGE'); 
       } else { // error 
        alert(response.errorMessage);//form is invalid 
       } 
      } 
     }) 
     return false; 
    } 


</script> 

只要确保你有包含你的网页jquery库。

根据要求,如果没有下面的jQuery代码:

HTML:

<form method="POST" id="contact-form" action="contact.php" > 

<label for='message'>Leave a Message:</label> <br><br> 
    <textarea rows="15" cols="45" name="message" id="message" ></textarea> 
    <?php if (isset($_GET['error'])) { ?> 
    <p style="color:red;">Message is required</p> 
    <?php } ?> 
<br><br> 
    <input type="submit" name='submit' value="send!"/> 

</form> 

PHP:

<?PHP 

    if (isset($_POST) && !empty($_POST)) { 
     $subject="New Message!"; 
     $message=$_POST ["message"]; 
     if(!empty($message)) { 
      //your email code here 
      echo '<script>window.location.href="index.html"</script>'; 
     } else { 

      echo '<script>window.location.href="index.html?error=1"</script>'; 
     } 


     } 
    } 


    ?> 
+0

谢谢Mahesh!我真的想知道如何如果没有JQuery,那么我也可以尝试一下这条路线。 – stone

+0

好的,让我发布而不用jquery答案 –

+0

更新后,试试上面的编辑代码,让我知道你是否有任何问题谢谢 –