我正在创建一个表单,当某些字段没有填写或填写正确时,应该创建一个javascript警报。 我希望能够将我放入php变量的错误消息显示在javascript警告窗口中。
I'm making a form that is supposed to create a javascript alert when some fields aren't filled out or filled out properly. I want to be able to take the error messages I've put in a php variable and display them in the javascript alert window.
以下代码不起作用:
function died($error) { echo '<script type="text/javascript"> alert('.$error.')</script>'; die(); }如何添加 $ error中包含的字符串在两个script字符串之间,以便它作为javascript警报正确输出?
How can I add the string contained in $error between the two "script" strings so it will output properly as a javascript alert?
谢谢!
推荐答案您只会忘记JavaScript提醒所需的报价。
You only forgot quotations that are required for the JavaScript alert.
如果您将'hello'传递给该函数,您当前的代码将创建警报:
If you passed 'hello' to the function, your current code would create alert as:
alert(hello)而不是:
alert("hello")因此,请将您的行更改为以下内容(两个双倍)在连接$ error之前和之后添加引号:
Therefore, change your line to the following (two double quotes are added before and after concatenating $error):
echo '<script type="text/javascript">alert("'.$error.'");</script>';您可以使用您的功能:
died('error on whatever');更多推荐
使用内置php变量的php创建javascript警报?
发布评论