【问题标题】:Input and output values for php into the browser?php 在浏览器中的输入和输出值?
【发布时间】: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


【解决方案1】:

与 Javascript 不同,php 完全不同! Javascript的功能可以通过onclick、onload等属性在HTML中访问,而php的功能则大不相同,只能通过php代码访问。首先,您需要使用方法创建一个舞会,然后发送到同一页面(在您的情况下),然后运行您应用的条件,最后像我在其中所做的那样回显结果。

<!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 method="get" action="<?php $_SERVER['PHP_SELF']; ?>">
            <input type="text" value="" id="textField" name="number">
            <input type="submit" value="Enter" class="button" name="submit">
        </form>


        <?php


        if (isset($_GET['submit']))
            $number = isset($_GET['number']) ? $_GET['number'] : '';

        if ($number == 1) {
            $returntext =  "The day of the week is Monday";
        } else if ($number == 2) {
            $returntext =  "The day of the week is Tuesday";
        } else if ($number == 3) {
            $returntext =  "The day of the week is Wednesday";
        } else if ($number == 4) {
            $returntext =  "The day of the week is Thursday";
        } else if ($number == 5) {
            $returntext =  "The day of the week is Friday";
        } else if ($number == 6) {
            $returntext =  "The day of the week is Saturday";
        } else if ($number == 7) {
            $returntext =  "The day of the week is Sunday";
        } else {
            $returntext =  ("Wrong input, try again!");
        }

        ?>

        <div id="valueReturned">
            <p id="returnText"><?php echo $returntext; ?><p>
        </div>
        <a href="index4.html" id="jButton">
            <h3> Next Exercise </h3>
        </a>

    </div></section>

希望,您必须在合适的服务器上运行这些 php 代码!好样的!

【讨论】:

  • 完美!我似乎无法理解我的学校如何没有提供有关如何做到这一点的教程,因为它是必不可少的。只是在循环中通过我,让我自己弄清楚......虽然我猜当你想到它时,那就是编程!只是希望学校给我更多的指导,因为我浪费了一整天试图弄清楚如何做到这一点。谢谢你,我的朋友。
  • 没关系...但是..你没有浪费一天!相反,您会派出一天对此进行研究。回想一下,那天你学到了什么,学到了多少! ?
【解决方案2】:

下面的代码应该可以帮助你理解。

<?php
$response = "";
if(isset($_POST) && isset($_POST["mytext"])): /// check if user submitted form

$num = $_POST["mytext"];  //put form mytext variable.. retrieved from html <input name="mytext".... 


    if(is_numeric($num)):

switch($num):

        case "1":
            $response = "Monday";
        break;
        case "2":
            $response = "Tuesday";
        break;
        case "3":
            $response = "Wednesday";
        break;
        case "4":
            $response = "Thursday";
        break;
        case "5":
            $response = "Friday";
        break;
        case "6":
            $response = "Saturday";
        break;
        case "7":
            $response = "Sunday";
        break;
        default:
            $response = "Invalid number";

endswitch;

    else:

    $response = "Invalid number";

    endif;


endif;


?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>

<!--     
    http://phpfiddle.org
-->

<script type="text/javascript">
    
</script>

<style type="text/css">
    
</style>
</head>

<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" action="" method="post">

    <label>Text</label> <input type="text" value="" size="30" maxlength="100" name="mytext" id="" /><br /><br />
   
    <br />
   
    <button id="mysubmit" type="submit">Submit</button><br /><br />
    <?php if (isset($_POST)): ?>
    <div style="background-color:red;color:white;"><?php echo $response; ?></div>
    <?php endif; ?>
    </div>

</form>
</div>

<?php

?>

</body>
</html>


【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多