2015-03-31 153 views
-1

我試圖在HTML文本輸入字段的值中顯示來自PHP變量的值。目前提交後的文本輸入字段僅打印PHP腳本的內容。該腳本稱爲submit_type.phpPHP變量和HTML文本輸入值

<?php 
    if(isset($_POST['submit'])) { 
      $wire_type = $_POST['wire_type_option']; 
      header("Location: ./guest.html"); 
    } 
    else { 
      echo "loop"; 
    } 
?> 

我已經嘗試從問題Using a PHP variable in a text input value = statement 如見於guest.html線39的解決方案,但它似乎沒有工作

<input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" /> 

主要index.html頁面上的提交按鈕將您重定向到guest.html(幾乎與index.html文件相同,除了第39行,其中值已設置)

僅供參考,以下是訪客。 html

<!DOCTYPE html> 
<html>Current 
    <head> 
      <title> Thermocouple Database</title> 
      <link rel="stylesheet" type="text/css" href="main.css"> 
    </head> 

    <body> 
      <section> 
        <h1> Login to upload a calibration</h1> 
        <form> 
          <div class='login'><label for="username"> Username: </label></div> 
          <input type="text" name="username"> 
          <div class='pass'><label for="password"> Password: </label></div> 
          <input type="text" name="password"> 
          <input type="submit" value="Submit"> 
          <br> <br> <br> <br> 
        </form> 
      </section> 
      <section> 
        <h1> Which thermocouple type do I have? </h1> 
        <label> My wire looks like: </label> 
        <form method="post" action="submit_type.php"> 
        <select name='wire_type_option'> 
          <option value="J-type"> J-type </option> 
          <option value="K-type"> K-type </option> 
          <option value="T-type"> T-type </option> 
          <option value="E-type"> E-type </option> 
        </select> 
        <input type="submit" name="submit" value="Submit wire type"> 
        </form> 
        <!-- <form method="get" action="action.php"> --> 
        <div id="results"> 
          Result: <label name="result_label"> </label> 
          <div id="result_table"> 
            <label> Type: </label> 
            <input type="text" name="type" value="<?php echo htmlspecialchars($wire_type); ?>" /> 
            <label> Temperature Range: </label> 
            <input type="text" name="min_temp"> 
            <label> - </label> 
            <input type="text" name="max_temp"> 
            <br> <br> <br> 
            <label> Published calibration </label> 
            <br> <br> 
            <label> Voltage: </label> 
            <input type="text" name="in_voltage"> 
            <label> Calibrated Temperature: </label> 
            <input type="text" name="out_temp"> 
            <br> <br> 
            <label> Temperature: </label> 
            <input type="text" name="in_temp"> 
            <label> Calibrated Voltage: </label> 
            <input type="text" name="out_voltage"> 
          </div> 
        </div> 
        <!-- <input type="submit" value="Submit"> --> 
        <!-- </form> --> 
      </section> 
    </body> 

</html> 

我在這裏錯過了什麼?我對web開發仍然陌生,所以任何和所有的幫助表示讚賞!

+0

在打開PHP標記 (例如'<?php error_reporting(E_ALL);)後立即向文件頂部添加錯誤報告。 ini_set('display_errors',1);'然後你的代碼的其餘部分,看看它是否產生任何東西。 – 2015-03-31 03:44:57

+1

您的頁面具有html擴展名,將其更改爲php,以便在將頁面發送到瀏覽器之前執行php代碼。 – jeff 2015-03-31 03:45:30

+1

*「主index.html上的提交按鈕」* - 您是否指示Apache將'.html'文件視爲PHP?如果沒有,那麼你不能運行PHP指令。將它重命名爲'.php'或告訴Apache將它們視爲PHP。 guest.html和任何其他可能試圖運行PHP指令的'.html'文件也是如此。 – 2015-03-31 03:45:41

回答

0
$wire_type = $_POST['wire_type_option']; // assignment operator 
header("Location: ./guest.html"); // redirecting to guest.html 

,如果你想顯示$ wire_type你需要將其添加到會話變量或需要創建$ _GET要求另一頁,雖然我不建議使用$ _SESSION變量。你需要

修正了做 爲$ _GET

header("Location: ./guest.php?wi_type=".$wire_type); or header("Location: ./guest.php?wi_type=".$_POST['wire_type_option']); 

如何在guests.php訪問WI_TYPE

echo $_GET['wi_type']; 

修正了$_SESSION

$_SESSION['wire_type'] = $_POST['wire_type_option'] 
header("Location: ./guest.php") 
上guest.php

echo $_SESSION['wire_type']; 

如果你知道你會需要很多wire_type多次在不同的頁面與$_SESSION

去 - 更新

session_start(); put it on the top 
if(isset($_POST['submit'])) { 
     $_SESSION['wire_type'] = $_POST['wire_type_option']; 
     header("Location: ./guest.php"); 
} 
else { 
     echo "loop"; 
} 

你必須有.PHP分機與session_start();在頂部爲您客人檔案。

+0

我在guest.php中試過」/>但文本字段仍然爲空 – user2227821 2015-03-31 16:36:23