php
2013-07-06 44 views -1 likes 
-1

嗨,我有這個和工作正常,除了以下問題。會話的使用保留到PHP中的文本框中的值

echo "<form method='post' action=''>"; 
    echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">"; 
    echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
    echo "</form>"; 
<A submit button here to send the form data> 

的問題是,如果我有這樣John在文本框中的值,當我提出我保留原有用戶在文本框中鍵入單詞Jonn形式。但是,如果用戶輸入John Carter,當表單被提交時,它只保留John。換句話說,文本僅限於文本之間的第一個空格。我如何保留整個文本?

編輯

<?php 
    if(isset($_POST['sendone'])) 
    { 
    echo "Hello"; 
    echo "<form method='post' action=''>"; 
    echo " <input type='text' name='txt1' id='txt1'>"; 
    echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
    echo "</form>"; 
    } 
    if(isset($_POST['sendtwo'])) 
    { 
    if($_POST['txt1']=='') 
    { 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 
     echo " <input type='text' name='txt1' id='txt1'>"; 
     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Empty"; 
    } 
    else 
    { 
     $_SESSION['txt1'] = $_POST['txt1']; 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 
     echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">"; 
     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Hit success!";  
    } 
    } 
    ?> 
+0

你能告訴我們PHP代碼如何在會話中存儲嗎? –

+0

@Atul我編輯了我的問題,你會看到完整的代碼 –

+0

什麼是sendone和sendtwo你有嵌套的形式? –

回答

1

您需要包括圍繞價值的單引號的value= attribue時echo荷蘭國際集團的_SESSION["txt1"],否則由此產生的HTML是無效的..這樣的:

<?php 
    if(isset($_POST['sendone'])) 
    { 
    echo "Hello"; 
    echo "<form method='post' action=''>"; 
    echo " <input type='text' name='txt1' id='txt1'>"; 
    echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
    echo "</form>"; 
    } 
    if(isset($_POST['sendtwo'])) 
    { 
    if($_POST['txt1']=='') 
    { 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 
     echo " <input type='text' name='txt1' id='txt1'>"; 
     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Empty"; 
    } 
    else 
    { 
     $_SESSION['txt1'] = $_POST['txt1']; 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 

     // check out this line here: 
     echo " <input type='text' name='txt1' id='txt1' value='" . htmlspecialchars($_SESSION['txt1']) . "'>"; 

     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Hit success!";  
    } 
    } 
?> 

但是,它的值得一提的是,它在性能上更好(可讀性更強),以儘可能使用純HTML,並且只需使用echo作爲您希望包含的變量。

+0

確實!你是對的。 –

+0

您還需要使用'htmlspecialchars()' –

+0

轉義輸出謝謝@Jack,已更新! – msturdy

0

檢查這個代碼。

<form name="" method="post"> 
<input type="text" name="txt1"> 
<input type="submit" name="sendtwo"> 
<input type="submit" name="sendone"> 
</form> 

<?php 
    if(isset($_POST['sendone'])) 
    { 
    echo "Hello"; 
    echo "<form method='post' action=''>"; 
    echo " <input type='text' name='txt1' id='txt1'>"; 
    echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
    echo "</form>"; 
    } 
    if(isset($_POST['sendtwo'])) 
    { 
    if($_POST['txt1']=='') 
    { 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 
     echo " <input type='text' name='txt1' id='txt1'>"; 
     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Empty"; 
    } 
    else 
    { 
     $_SESSION['txt1'] = $_POST['txt1']; 
     echo "Hello"; 
     echo "<form method='post' action=''>"; 

     // check out this line here: 
     echo " <input type='text' name='txt1' id='txt1' value='" . $_SESSION['txt1'] . "'>"; 

     echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>"; 
     echo "</form>"; 
     echo "Hit success!";  
    } 
    } 
?> 
+0

你可以發佈從哪個代碼爲sendone和sendtwo生成值 – 2013-07-06 05:38:36

+0

假設表單只有一個提交按鈕。表單2的內容在我編輯的問題中。感謝您的期待。 –

+0

您正在使用sendtwo和sendone即必須有兩個提交按鈕形式之一.. – 2013-07-06 06:01:58

相關問題