2015-11-04 90 views
0

嗨,我是PHP新手,对我来说一切都非常混乱。我试图做的和失败的是在页面上点击一个按钮时在屏幕上显示一个警报。用PHP显示jQuery提醒

我的代码signInFunction.php:

<?php 
if(isset($_POST['signIn'])){ 
    if(isset($_POST['username']) and isset($_POST['password'])){ 
     $username = $_POST['username']; 
     $password = $_POST['password']; 

     ?> 
     <script type="text/javascript">alert("test");</script> 
     <?php 
     echo "username is: ".$username." and password is: ". $password; 
    } 
    else{ 
     echo "Please enter username and password"; 
    } 
} 
else{ 
    echo "error button"; 
} 
?> 
在index.php文件

看起来是这样的:

<?php include "includes/header.php" ?> 
    <?php include "includes/navbar.php" ?> 
    <?php include "includes/carousel.php"?> 
    <?php include "includes/marketing.php"?> 
    <?php include "includes/features.php"?> 
    <?php include "includes/hidden.php"?> 
    <?php include "includes/footer.php" ?> 
    <?php include "includes/functions/signinFunction.php"?> 

的问题是,我管理底部显示的数据,但未能让警报出现..我错过了什么?

+0

[回荡在php jquery的警报弹出]的可能的复制(http://stackoverflow.com/questions/16532338/echoing-a-jquery-alert-popup-in-php) – swidmann

+0

@swidmann OP正在使用问题中描述的方法之一。不是重复的imho。 –

+0

您是否检查页面源代码?你看到你的警报功能吗?浏览器报告你一些JS错误或什么? – MilanG

回答

2
<form action="" method="post" > 
    <input name='username' type='text' value=''/> 
    <input name='password' type='text' value='' /> 
    <input type='button' name='signIn' value='Sign In' /> 
</form> 
<?php 
if(isset($_POST['signIn'])){ 
    if(isset($_POST['username']) and isset($_POST['password'])){ 
     $username = $_POST['username']; 
     $password = $_POST['password']; 

     ?> 
     <script type="text/javascript">alert("test");</script> 
     <?php 
     echo "username is: ".$username." and password is: ". $password; 
    } 
    else{ 
     echo "Please enter username and password"; 
    } 
} 
else{ 
    echo "error button"; 
} 
?> 

当U点击按钮符号那么警报应该出现

+0

是的,这是有效的,但为什么它不适用于我上面的方法? – Licentia

+0

,因为你没有提交任何形式,我想,看看这个:) http://www.w3schools.com/php/php_forms.asp –

-1
echo '<script language="javascript">'; 
echo 'alert("alert")'; 
echo '</script>'; 
+0

你可以简单地使用上面的代码来执行警报在PHP片段。 –

+0

这也没有工作 – Licentia