簡體   English   中英

將 php 的 output 值輸入瀏覽器?

[英]Input and output values for php into the browser?

我剛開始在 php 上學習我的模塊,兩個月前完成了 Javascript 我熟悉很多元素,但有點生疏。 基本上,我用 html 設置了一個表格,並添加了一點 css 使它漂亮。 學校讓我們做一些通用的練習。 我現在如何使用 Javascript 來獲取值等

document.getElementById("textBox").value;

並通過我創建的 div 返回 output

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
$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 中通過使用 ZC0BB2196426022E8AEDZFA 來訪問,加載。 等等,屬性。 但是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 代碼! 好走!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM