2013-08-26 56 views
2

我正在使用下面的代碼爲html表單(它有兩種形式)我可以在第一個和第二個表單提交後保留textarea字段。但我在這裏面臨的問題是下拉菜單選擇。提交後使用php保存表單數據在字段後面

代碼:

<html> 
<body> 

<div class="emcsaninfo-symcli-main"> 
    <form id="form1" name="form1" action=" " method="post" > 



     <div class="input">Your Name</div> 
     <div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div> 


     <div class="input">Your Place</div> 
     <div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div> 

     <div class="input-quest">Graduation Status</div> 
     <div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div> 

<div class="submit"> 
     <input id="first_submit" type="submit" name="first_submit" value="first_submit" /> 
</div> 

</form> 


<?php 

if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'])) 
{ 

$myname = $_POST['myname']; 
$myplace = $_POST['myplace']; 
$graduation = $_POST['graduation']; 

?> 


<form id="form2" name="form2" action=" " method="post" > 
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>"> 


<?php 

echo "My name is $myname and I am from $myplace, and I am $graduation"; 

?> 

</textarea> 

<input id="submit1" type="submit" name="name_field" value="submit1" /> 
<input id="submit2" type="submit" name="place_field" value="submit2" /> 

<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/> 
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /> 
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" /> 
</form> 

<?php 

function name() 
{ 
    echo $_POST["output_textarea"]; 
} 
if(isset($_POST['name_field'])) 
{ 
    name(); 
} 



function place() 
{ 
    echo $_POST["output_textarea"]; 
} 
if(isset($_POST['place_field'])) 
{ 
    place(); 
} 

} 

?> 


</div>  

</html> 
</body> 

例如,如果我把name = Johnplace : UK並選擇爲graduate畢業的地位,它將會給我第一種形式輸出在我output textarea

My name is John and I am from UK, and I am Graduate 

我有兩個單獨提交按鈕的第二種形式,使用我正在做的其他功能的幫助output textarea 如果我按任何第二個按鈕,我能夠記住p輸入我的名字和地點區域,但不保留下拉選擇。所以它只會顯示一個類似提交submit1submit2

My name is John and I am from UK, and I am 

在此之後,

  1. 我如何才能讓下拉選擇也與輸出文本區
  2. 我將能夠只顯示第二個表單提交後的output_textarea內容不保留第一個表單數據?

PHP FIDDLE

+0

使用會話嘗試 –

回答

3

你有邏輯的隱藏輸入錯誤的「畢業」的元素。
這是你在53-55行。 55號線沒有迴音,而不是它有一個$graduation = $_POST['graduation'];這將不利於你:

<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/> 
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /> 
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" /> 

的而不是,這個代碼應工作:

<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/> 
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /> 
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />