如何使用 PHP 脚本添加 javascript 警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6871435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add a javascript alert with a PHP script?
提问by sasori
let's say i have an input form, wherein, it's use for an email address. this form is part of a long form so i use an alert box instead when other inputs got errors....my question now is, if I checked the email input string via php, if it has been taken , how will I put the message like e.g "this email has been taken" in an alert box if i am using PHP to check it from backend ?..i want an alert box, since I use it with the other input boxes that don't need a backend check e.g
假设我有一个输入表单,其中,它用于电子邮件地址。此表单是长表单的一部分,因此当其他输入出现错误时,我使用警报框代替....我现在的问题是,如果我通过 php 检查电子邮件输入字符串,如果它已被采用,我将如何放置如果我使用 PHP 从后端检查它,则在警告框中显示例如“此电子邮件已被接收”之类的消息?...我想要一个警告框,因为我将它与其他不需要后端检查的输入框一起使用例如
alert("$errormessage");
回答by genesis
PHP is server-side language. you can output it to user with
PHP 是服务器端语言。您可以将其输出给用户
echo "<script>alert('".mysql_real_escape_string($errormessage)."');</script>";
or you can write your own function
或者你可以编写自己的函数
function alert($a){
echo "<script>alert('".mysql_real_escape_string($a)."');</script>";
}
alert("test");
回答by Pehmolelu
You can echo any kind of javascript with PHP. The question is however, are you sure you want to do this? You can also do like this:
您可以使用 PHP 回显任何类型的 javascript。然而,问题是,您确定要这样做吗?你也可以这样做:
<?php if(emailExists): ?>
<script>alert('Email exists!')</script>
<?php endif; ?>
But you could also use jQuery and Ajax requests to check if email exists with ajax when user has typed the email.
但是您也可以使用 jQuery 和 Ajax 请求来检查用户输入电子邮件时是否存在带有 ajax 的电子邮件。
回答by Paula George
echo '<script>alert("ssss");</script>' ;
just put it within echo it will work that way
echo '<script>alert("ssss");</script>' ;
只要把它放在 echo 中,它就会这样工作