PHP如何回显Js Custom警报

时间:2014-04-02 21:35:57

标签: javascript php jquery echo

你好我从互联网上获取了一个自定义警报,并希望在我的php回应警报中实现,已经尝试了几种方法,但我找不到正确的语法。请帮忙

我想我需要将“echo”传递给javascript并在PHP中调用“echo”时调用该函数,但我没有运气

  <script>
  function CustomAlert(){
this.render = function(){
    var winW =  window.innerWidth;
    var winH = window.innerHeight;
    var dialogoverlay = document.getElementById('dialogoverlay');
    var dialogbox = document.getElementById('dialogbox');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH+"px";
    dialogbox.style.left = "20px";
    dialogbox.style.top = "70px";
    dialogbox.style.display = "block";
    document.getElementById('dialogboxhead').innerHTML = "Login Processado com Sucesso";
    document.getElementById('dialogboxbody').innerHTML = "Seja Bem Vindo";
    document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';


}
this.ok = function(){
    document.getElementById('dialogbox').style.display = "none";
    document.getElementById('dialogoverlay').style.display = "none";

}
}
var Alert = new CustomAlert();

</script>

和php:

   <?php
  $email=$_POST['email'];
  $senha=$_POST['senha'];
  $sql = mysql_query("SELECT email, senha FROM clientes WHERE email = '$email' and senha = '$senha'") or die(mysql_error());
  $row = mysql_num_rows($sql);
  if($row > 0 ) {
session_start();
$_SESSION['email']=$_POST['email'];
$_SESSION['senha']=$_POST['senha'];
echo '<script>';
echo 'Alert.render("Login efetuado com sucesso")';
echo '</script>';
echo "<script>loginsucessfully()</script>";


    } else {
echo "Usuario ou senha invalidos";
echo "<script>loginfailed()</script>";
    }

   ?>

2 个答案:

答案 0 :(得分:2)

将变量从PHP传递给Javascript:

var something = <?php echo $something; ?>;

作为旁注,PHP标签中的分号是不必要的,因为它只有一行代码;这只是一种风格问题。

答案 1 :(得分:1)

要执行此操作,您可以编写提交表单的javascript函数 - 或者您可以使用ajax / jquery。 jQuery.post

或许这可以帮到你。

<script type="text/javascript">
  var foo = '<?php echo $foo ?>';
</script>