2017-07-03 97 views
0

我使用php創建比薩訂購網站。現在我想回顯通過表單中的URL傳遞的變量。我知道如何檢索這些數據:<?php echo $_GET["numpizzas"]; ?>。但我不知道將其添加到我的html表單字段的正確方法。任何幫助深表感謝使用PHP變量填充輸入字段

<?php 

echo 
'<form action="pizza.php" method="post"> 
<h1>Thanks for Ordering. Please submit your delivery info.</h1> 
<label>Name:</label> <input type="text" name="name"> 
<label>Address:</label> <input type="text" name="address"> 
<label>Phone:</label> <input type="text" name="phone"> 
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" > 
//Money field does not populate with number, I just see <?php echo $_GET[ 
<label>Feedback:</label> <input type="text" name="feedback"> 
<input type="submit" value="Submit"> 
</form>'; 

?> 
<?php echo $_GET["numpizzas"]; ?> 

我也試過存放在變量$howmanypizzas = $_GET["numpizzas"]; ?>的整數,但它仍然沒有顯示爲字段值。

+0

是的頁面是PHP擴展 – st4rgut

回答

1

<?php echo $_GET["numpizzas"]; ?>不僅檢索數據。 echo還輸出到HTML響應(屏幕)

,因爲你是媒體鏈接通過你的HTML有迴音,你可以這樣做:

​​

說明:回聲是臨危字符串和輸出功能它以html。 因此,通過連接運算符.,您可以將$_GET["numpizzas"]變量作爲字符串注入到html中,並將其傳遞給echo函數,並將其輸出到瀏覽器。 解決這個問題的另一種方法是隻在需要處理邏輯的地方調用PHP,就像@pavithra的答案一樣,這也是可行的。

0
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" /> 

,但我不知道爲什麼你明白這是爲獲取variable.hope這個工程

<form action="pizza.php" method="post"> 
    <h1>Thanks for Ordering. Please submit your delivery info.</h1> 
    <label>Name:</label> <input type="text" name="name"> 
    <label>Address:</label> <input type="text" name="address"> 
    <label>Phone:</label> <input type="text" name="phone"> 
    <label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" /> 

    <label>Feedback:</label> <input type="text" name="feedback"> 
    <input type="submit" value="Submit"> 
</form> 
1

您已經在迴應並試圖在裏面迴應。您需要連接與你呼應字符串您的變量,看PHP Strings

echo 
'<form action="pizza.php" method="post"> 
<h1>Thanks for Ordering. Please submit your delivery info.</h1> 
<label>Name:</label> <input type="text" name="name"> 
<label>Address:</label> <input type="text" name="address"> 
<label>Phone:</label> <input type="text" name="phone"> 
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '"> 
<label>Feedback:</label> <input type="text" name="feedback"> 
<input type="submit" value="Submit"> 
</form>'; 

您也可以考慮Heredoc語法。