【问题标题】:How to Call Ajax from HTML/JS to start PHP Function?如何从 HTML/JS 调用 Ajax 来启动 PHP 函数?
【发布时间】:2017-04-01 22:08:33
【问题描述】:

我正在编写一个代码,该代码将在提交表单时显示警报,然后重定向回主页。如果提交了一个空表单,我想显示一个错误警报。我意识到我不能从 onclick 调用 PHP,而我需要调用一个 JS 函数来启动我的 PHP 函数。谁能告诉我我的 PHP 代码是否在正确的轨道上,以及如何初始化 AJAX 函数?尝试在没有 JQuery 的情况下执行此操作。谢谢!

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>';

}

}


?>

【问题讨论】:

  • 对于初学者,您的 PHP 中有语法错误。在你正在做的两个位置window.location.replace。您需要在整个字符串的外部使用 " 并在内部使用 ' 作为引号,或者转义 HTML 文件周围的那些单引号。
  • 这个函数startajax()在哪里?还是你想让我们为你写?
  • 我还没有写它,我对编码完全陌生,我主要想知道我的 php 中需要更改什么,但感谢所有帮助!

标签: javascript php ajax forms submit


【解决方案1】:

您可以使用 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>';
        }


        }
    }


    ?>

【讨论】:

  • 谢谢马赫什!我真的很想知道如何在没有 JQuery 的情况下做到这一点,但我也可以尝试这条路线。
  • 好吧,让我在没有 jquery 答案的情况下发帖
  • 已更新,请尝试上面的编辑代码,如果您有任何问题,请告诉我。谢谢
  • 好的,您也可以将此答案标记为有帮助,以便对其他人也有帮助。谢谢
  • 嗨,斯通,也请点赞。以便它在搜索结果中排在第一位,面临相同问题的用户将轻松获得解决方案。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-03-31
  • 2023-04-04
  • 1970-01-01
  • 2011-11-10
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多