Javascript 如何在HTML中调用PHP函数

Javascript 如何在HTML中调用PHP函数,javascript,php,html,twitter-bootstrap,Javascript,Php,Html,Twitter Bootstrap,我有一个功能齐全的基于Twitter引导的HTML页面 为了添加联系人表单(在这种情况下是在模式窗口中),我在文件中实现了一些PHP代码。 在将我的文件保存为index.php而不是index.html之后,对于要在服务器上解释的php,只显示背景,所有内容都消失了 在主div启动之前,我的代码开始部分(包括有问题的PHP部分): <!DOCTYPE html> <html lang="fr"> <html>

我有一个功能齐全的基于Twitter引导的HTML页面

为了添加联系人表单(在这种情况下是在模式窗口中),我在文件中实现了一些PHP代码。 在将我的文件保存为index.php而不是index.html之后,对于要在服务器上解释的php,只显示背景,所有内容都消失了

在主div启动之前,我的代码开始部分(包括有问题的PHP部分):

        <!DOCTYPE html>
        <html lang="fr">
        <html>
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Manena.fr | Originaux, uniques et faits main, des cadeaux pour toutes les occasions</title>

            <!-- Bootstrap -->
            <link href="./css/bootstrap.min.css" rel="stylesheet">
            <link href="./css/custom.css"rel="stylesheet" >
            <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
            <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
            <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
            <![endif]-->

            <link href="http://fonts.googleapis.com/css?family=Abel|Open+Sans:400,600" rel="stylesheet" type="text/css">
            <link href="http://fonts.googleapis.com/css?family=Paprika" rel="stylesheet" type="text/css">
        </head>
        <body>

        <?php 

            include('success.php');

            if(isset($_POST) && isset($_POST['nom']) && isset($_POST['email']) && isset($_POST['message'])){

            if(!empty($_POST['nom']) && !empty($_POST['email']) && !empty($_POST['message'])){
                $destinataire = "contact@site.com";
                $sujet = "Demande de contact";
                $message .= "Nom : ".$_POST['nom']."\r\n";
                $message .= "Adresse email : ".$_POST['email']."\r\n";
                $message .= "Message : ".$_POST['message']."\r\n";
                $entete = 'From: '.$_POST['email']."\r\n".
                'Reply-To: '.$_POST['email']."\r\n".
                'X-Mailer: PHP/'.phpversion();

            if (mail($destinataire,$sujet,$message,$entete)){
                showSuccess();
                } else {

                echo "Une erreur est survenue lors de l'envoi du formulaire par email";

                }
            }
            }
        ?>

HTML CODE

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <!-- Modal Privacy-->
    <script type="text/javascript">
        $("#modalPrivacy").click(function(event){
        $("#modalPrivacy2").modal('show');
        });
    </script>
    <!-- Modal Contact-->
    <script type="text/javascript">
        $("#modalContact").click(function(event){
        $("#modalContact2").modal('show');
        });
    </script>

</body>
</html>

Manena.fr|Originaux,uniques et faits main,des cadeaux pour toutes les placements

您的
success.php
文件是否有错误的引号,例如:

回声'

$(“modalContact2”).modal(\'hide\)

$(“modalSuccess”).modal(\'show\)

您在echo中为.modal('show')使用单引号。这些单引号结束了echo语句。将这些引号转义为.modal(\'show\')


希望这有帮助。

我刚刚修改了你的代码。你肯定有一些js错误

function showSuccess() 
            {
                echo "<script>$('#modalContact2').modal('hide');</script>";
                echo "<script type='text/javascript'>
                        $('#modalSuccess').modal('show');
                      </script>";
            }
函数showSuccess()
{
echo“$('modalContact2').modal('hide');”;
回声“
$('modalSuccess').modal('show');
";
}

您放置了});在$('#modalsucces').modal('show')之后;只关闭大括号而不打开的行。我删除了它。我用alert检查了它及其工作情况。

用这个替换success.php代码

函数showSuccess(){

echo'$(“#modalContact2”).modal(\'hide\');
回声'
$(“modalSuccess”).modal(\'show\);
});
';
}


希望您的主文件ext是php而不是html。请验证。

我没有看到您的标题中提到的任何引导脚本文件。您是否看到任何PHP错误?尝试启用PHP错误报告。很可能存在某种PHP错误/警告。放置
显示错误(1)
错误报告(E_ALL)在页面顶部(当然在php标记内)。让我们知道你的想法see@giorgio我添加了显示错误(1);和错误报告(E_ALL);谢谢你的更正,以后可能会有用,但它并没有解决我当前的显示问题…谢谢你的帮助,你的答案加入了myusuf的答案,它肯定会对模式有所帮助,但我的页面仍然没有显示。我想你有js问题。请使用firebug或类似工具检查以有效识别js问题。第27行中未定义与$(“#modalContact2”)。modal('hide');在success.phpIt中,表示jquery未包含在此页面中,或者jquery可能与mootools或其他javascript库发生冲突。如果发生冲突,请参阅感谢发布,正如在原始帖子中所指出的,我的文件显然是index.php,而不是index.html。
function showSuccess() 
            {
                echo "<script>$('#modalContact2').modal('hide');</script>";
                echo "<script type='text/javascript'>
                        $('#modalSuccess').modal('show');
                      </script>";
            }
echo '<script>$("#modalContact2").modal(\'hide\');</script>';

echo '<script type="text/javascript">
    $("#modalSuccess").modal(\'show\');
    });
</script>';