使用内置php变量的php创建javascript警报?

编程入门 行业动态 更新时间:2024-10-11 15:19:22
本文介绍了使用内置php变量的php创建javascript警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个表单,当某些字段没有填写或填写正确时,应该创建一个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警报?

本文发布于:2023-11-27 17:51:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   变量   php   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!