JavaScript随机消息
JavaScript Random Message
我需要在我的 PHP 代码中成功匹配时创建一条随机消息,但我不确定如何使用 JavaScript 完成此操作。在我的代码中,PHP 从文件中提取用户名和密码,然后检查用户输入的用户名和密码是否匹配。如果是这样,他们将获得访问权限,并且表格会消失以显示一条消息(我正试图弄清楚该怎么做)。如果不是,则拒绝访问并保留表单以供他们重试。
我已尝试将代码更改为 onload
、onclick
和 addEventListener
,但没有任何显示。我还尝试将 HTML 标签更改为 element
和 p
。这不可能吗?我不认为 onclick
会很好,因为无论哪种方式都单击了提交按钮,但我尝试了它以查看是否可以正常工作。
PHP
//NO ERRORS AVAILABLE AND THE BUTTON HAS BEEN PUSHED
if (!$error && isset($_POST['submit']))
{
echo '<div class = "output">
<h1 class = "success">'.$msg.'</h1>
<p id="welcomeDisplay" onload="newWelcome()"></p>
<p id="welcomemsg"></p>
</div>';
}
JavaScript
var welcome =
[
"Success feels so good!",
"You made it!",
"You are worthy!",
"Open Sesame",
"Welcome to the dark side!",
"Hola!",
"So glad you are back!",
"Hello, sunshine!",
"Howdy, partner",
"what's kickin', little chicken?",
"Peek-a-boo!",
"Ahoy, matey!",
"Aloha!",
"Bonjour!",
"Hallo!",
"Ciao!",
"Konnichiwa!"
];
/*-----------------------------------------------------------------*/
function newWelcome()
{
var randomNumber = Math.floor(Math.random() * (welcome.length));
document.getElementById('welcomemsg').innerHTML = welcome[randomNumber];
}
如果 php 脚本处于根级别,您可以尝试将类似的内容添加到 scripts.js
文件中,但您可能需要对其进行调整以解决 [=15] 中没有脚本名称的问题=] 或路径(如果它在子文件夹中)。
document.addEventListener('DOMContentLoaded',()=>{
let arr=location.href.split('/');
let page=arr.pop();
let path=arr.pop();
/*
You might need to use the path variable too!
*/
if( page=='index.php' ){
const welcome=[
"Success feels so good!",
"You made it!",
"You are worthy!",
"Open Sesame",
"Welcome to the dark side!",
"Hola!",
"So glad you are back!",
"Hello, sunshine!",
"Howdy, partner",
"what's kickin', little chicken?",
"Peek-a-boo!",
"Ahoy, matey!",
"Aloha!",
"Bonjour!",
"Hallo!",
"Ciao!",
"Konnichiwa!"
];
let msg=welcome[Math.floor(Math.random() * (welcome.length))];
document.getElementById('welcomemsg').innerHTML=msg
}
})
我需要在我的 PHP 代码中成功匹配时创建一条随机消息,但我不确定如何使用 JavaScript 完成此操作。在我的代码中,PHP 从文件中提取用户名和密码,然后检查用户输入的用户名和密码是否匹配。如果是这样,他们将获得访问权限,并且表格会消失以显示一条消息(我正试图弄清楚该怎么做)。如果不是,则拒绝访问并保留表单以供他们重试。
我已尝试将代码更改为 onload
、onclick
和 addEventListener
,但没有任何显示。我还尝试将 HTML 标签更改为 element
和 p
。这不可能吗?我不认为 onclick
会很好,因为无论哪种方式都单击了提交按钮,但我尝试了它以查看是否可以正常工作。
PHP
//NO ERRORS AVAILABLE AND THE BUTTON HAS BEEN PUSHED
if (!$error && isset($_POST['submit']))
{
echo '<div class = "output">
<h1 class = "success">'.$msg.'</h1>
<p id="welcomeDisplay" onload="newWelcome()"></p>
<p id="welcomemsg"></p>
</div>';
}
JavaScript
var welcome =
[
"Success feels so good!",
"You made it!",
"You are worthy!",
"Open Sesame",
"Welcome to the dark side!",
"Hola!",
"So glad you are back!",
"Hello, sunshine!",
"Howdy, partner",
"what's kickin', little chicken?",
"Peek-a-boo!",
"Ahoy, matey!",
"Aloha!",
"Bonjour!",
"Hallo!",
"Ciao!",
"Konnichiwa!"
];
/*-----------------------------------------------------------------*/
function newWelcome()
{
var randomNumber = Math.floor(Math.random() * (welcome.length));
document.getElementById('welcomemsg').innerHTML = welcome[randomNumber];
}
如果 php 脚本处于根级别,您可以尝试将类似的内容添加到 scripts.js
文件中,但您可能需要对其进行调整以解决 [=15] 中没有脚本名称的问题=] 或路径(如果它在子文件夹中)。
document.addEventListener('DOMContentLoaded',()=>{
let arr=location.href.split('/');
let page=arr.pop();
let path=arr.pop();
/*
You might need to use the path variable too!
*/
if( page=='index.php' ){
const welcome=[
"Success feels so good!",
"You made it!",
"You are worthy!",
"Open Sesame",
"Welcome to the dark side!",
"Hola!",
"So glad you are back!",
"Hello, sunshine!",
"Howdy, partner",
"what's kickin', little chicken?",
"Peek-a-boo!",
"Ahoy, matey!",
"Aloha!",
"Bonjour!",
"Hallo!",
"Ciao!",
"Konnichiwa!"
];
let msg=welcome[Math.floor(Math.random() * (welcome.length))];
document.getElementById('welcomemsg').innerHTML=msg
}
})