javascript - 如何从我的 php 文件调用位于我的 html 文件中的 javascript 函数?

标签 javascript php html

在我的 index.html 中我有:

<head>
  <link rel="stylesheet" href="css/messi.min.css" />
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="js/messi.min.js"></script>
  <script type="text/javascript">
    function failureFunction(){
      new Messi('There was an error sending your message.', {title: 'Failure'});
    };      
    function successFunction(){
      new Messi('Success sending email', {title: 'Success'});
    };
  </script>
</head>
<body>
  <form class="form" id="form1" method="post" action="contactengine.php">
  ...
  </form>
</body>

然后在我的 contactengine.php 中我有:

$success = mail($EmailTo, $Subject, $Body, $Headers);

// redirect to success page 
if ($success){
  echo "<script type='text/javascript'>successFunction();</script>"; 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
 echo "<script type='text/javascript'>failureFunction();</script>";
 print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}

不过,我的 echo ... 不会调用 successFunction()。我知道 $success var 是真的,因为如果我在那里发出警报,它会调用警报,而且我知道我的 successFunction() 有效,因为我可以从我的 index.html 文件没有问题。梅西图书馆位于here供引用。

如何从我的 php 文件中正确调用 successFuntion()

最佳答案

或许您可以将您的 JS 函数移动到一个 javascript 文件中,并将该文件包含在您需要调用这些函数的 PHP 或 HTML 页面中。

js/customMessi.js

function failureFunction(){
  new Messi('There was an error sending your message.', {title: 'Failure'});
};      
function successFunction(){
  new Messi('Success sending email', {title: 'Success'});
};

在您的 HTML 文件中:

<head>
 <link rel="stylesheet" href="css/messi.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">   </script>
 <script src="js/messi.min.js"></script>
 <script src="js/customMessi.js"></script>
 </head>
<body>
 <form class="form" id="form1" method="post" action="contactengine.php">
   ...
  </form>
</body>

并且在您的 contactengine.php 文件中:

echo "<script type='text/javascript' src='js/customMessi.js'></script>";
...
$success = mail($EmailTo, $Subject, $Body, $Headers);

// redirect to success page 
if ($success){
  echo "<script type='text/javascript'>successFunction();</script>"; 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
 echo "<script type='text/javascript'>failureFunction();</script>";
 print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}

关于javascript - 如何从我的 php 文件调用位于我的 html 文件中的 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903012/

相关文章:

javascript - 将 Google Slide 作为 PDF 附加,并在通过电子邮件发送时替换占位符文本

javascript - Angular 翻译的配置未被拾取

javascript - 水平滚动 DIV 内的语义 UI 下拉菜单

php - 从 MYSQL 填充后如何将选择添加到第一个数据

php - 使用 PHP 和 Mysql 查找每种数据组合(不常见的一种)的统计数据

html - CSS 选择多个标签

javascript - 表 td 内的 json2htmled jQuery 移动按钮看起来很旧,在按钮()/刷新后新按钮中有旧按钮

php - 关于php下归并排序的问题

javascript - 找出html block 中最宽字的宽度

html - 将子菜单定位在父列表项的正下方