在浏览器中输入和输出 php 的值?

Input and output values for php into the browser?

我刚开始学习 php 的模块,两个月前完成 Javascript 我熟悉很多元素,但有点生疏。基本上我用 html 设置了一个表单,并添加了一些 css 以使其漂亮。学校让我们做一些通用的练习。我现在如何通过 Javascript

使用它来获取值等

document.getElementById("textBox").value;

并 return 通过我 div 的输出,我用

创建

document.getElementById("return").innerHTML = ...;

老实说,我不知道如何用 php 做到这一点,我相信这真的很简单,但我无法在网上找到解决方案,而且学校的视频非常模糊。

练习者是“创建一个应用程序,要求用户输入数字 (1-7) 并且 return 星期几与输入的数字相关联。您必须验证数字”

这是我目前的代码....

<!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
$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>


与Javascript不同,php完全不同! Javascript的功能在HTML中可以通过onclick、onload等属性访问,而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代码!干得好!