【发布时间】:2020-06-29 17:51:23
【问题描述】:
我刚开始在学校学习 php 模块,两个月前完成了 Javascript,我熟悉了很多元素,但有点生疏。基本上我用 html 设置了一个表单,并添加了一些 css 以使其漂亮。学校让我们做一些通用的练习。我现在如何使用 Javascript 来获取值等
document.getElementById("textBox").value;
并通过我创建的 div 返回输出
document.getElementById("return").innerHTML = ... ;
老实说,我不知道如何用 php 做到这一点,我相信这很容易,但我一生都无法在网上找到解决方案,而且学校的视频非常模糊。
练习器是“创建一个向用户询问数字 (1-7) 并返回与输入的数字相关联的星期几的应用程序。您必须验证数字”
这是我目前的代码......
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>PHP exercise 1</title>
<link rel="stylesheet" type="text/css" href="./styling1.css"/>
</head>
<body>
<!--E X C E R S I S E 3 !-->
<section id="allContent">
<section>
<button class="hide" id="clic"> <h4> E X E R C I C E 1 </h4></button>
</section>
<div id="container" class="hidden">
<div id="Barbox"><p class="pClass"> Create an application that ask the user for number (1-7) and return the day of the week associate with the number entered. You must validate the number</p>
</div>
<div id="background"></div>
<div class="ball"></div><div id="ball2"></div>
<div id="titleBox">
<p id="titlePRG">Enter a number!</p>
</div>
<form>
<input type="text" value="<?php echo $number = 1 ; ?>" id="textField">
<input type="button" value="Enter" class="button" onclick="return_day()">
</form>
<div id="valueReturned">
<p id="returnText"><p>
</div>
<a href="index4.html" id="jButton"><h3> Next Exercise </h3></a>
</div>
</section>
<?php
function return_day(){
$number = 1 ;
if ($number == 1){
echo "The day of the week is Monday";
}
else if ($number == 2){
echo "The day of the week is Tuesday";
}
else if ($number == 3){
echo "The day of the week is Wednesday";
}
else if ($number == 4){
echo "The day of the week is Thursday";
}
else if ($number == 5){
echo "The day of the week is Friday";
}
else if ($number == 6){
echo "The day of the week is Saturday";
}
else if ($number == 7){
echo "The day of the week is Sunday";
}
else{
echo ("Wrong input, try again!");
}
}//end of function
?>
这就是浏览器的样子,如果它有任何区别,所以你理解我的问题
答案应该显示在底部的白色框中,即
【问题讨论】:
-
我将帮助您缩小研究范围,在您的情况下,我要问的第一个问题是“我如何在 Php 和 Javascript 之间进行通信或在两种编程语言之间传递变量?其次,return_day( ) 函数是 PHP 函数而不是 javascript,我认为您应该尝试将其包装到 标记中。更好的方法是将函数设为 javascript 并且仅动态使用 Php 变量。
-
您要么必须回发表单然后回显响应,要么使用 Ajax 执行类似的任务但不刷新整个页面。如果你学校的教程不好的话,还有数以百万计的关于 PHP 和 HTML 的在线免费教程。
-
澄清一下,我根本不打算使用任何 Javascript,只使用 php .....只是用它作为比较来解释我想要实现的目标。当用户在输入框中输入 1 时,我想在我创建的 div 中返回答案“星期一”。所以只是试图通过我的 html 传递我的变量
标签: php if-statement user-input