javascript - 在 Sweet alert 2 中传递 PHP 变量

标签 javascript php sweetalert sweetalert2

我想在 sweet alert 中显示 PHP 变量但无法找到解决方案,我浏览了 sweet alert 2 的文档但找不到任何东西。 谁能帮帮我?

这是我的代码

$name = "xyz";

//sweetalert code
echo "<script>";
echo 'setTimeout(function () { swal("Greetings $name!","Thank you for adding your business details.<br>Our admin team will review the same and will publish shortly..!","success");';
echo '}, 100);</script>';

最佳答案

只需要切换引号:

echo "setTimeout(function () { swal('Greetings $name!','Thank you for adding your business details.<br>Our admin team will review the same and will publish shortly..!','success');'";

或者,另一种选择,在字符串中连接变量:

echo 'setTimeout(function () { swal("Greetings ' . $name . '!","Thank you for adding your business details.<br>Our admin team will review the same and will publish shortly..!","success");';

当你在 echo 中包含一个变量时,你需要使用 "

了解"' 之间的区别 SO Answer


关于javascript - 在 Sweet alert 2 中传递 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46113359/

相关文章:

javascript - 将 JavaScript 函数参数转换为 php 变量

php - 如何使用 jquery 调用 php Controller 方法?

javascript - Sweet Alert with form JS (3 input)

javascript - SweetAlert2, .then() - 不立即更新 DOM

javascript - 使用 Javascript/CSS 展开/折叠 div 时交换三 Angular 形图标

javascript - 为什么 cancelledPromise 模式被认为比 React 中的 isMounted() "antipattern"更好?

php - 将txt文件读入二维数组

jQuery sweetalert2 函数没有

javascript - 使用lazyload的优势?

javascript - 如何同时异步触发多个 API 调用?