2013-07-06 38 views
0

我正在嘗試使用PHP創建的表單提交結果來更改現有類的文本。樣式現有類(PHP回聲)

<h5 class="when-text"> Form not submitted </h5> 

存在於我的index.php文件的body標籤下。

在我的php標籤中我有我的表單提交代碼,它收集用戶的數據。在提交之後,我想回應結果並將h5類「未提交表單」更改爲「已提交表單」,同時保持已鎖定在課程中的相同樣式/定位。

我試着使用:似乎

echo '<h5 class="when-text"> Form submitted! </h5>'; 

這不工作。它在網站頂部回顯數據,最終創建另一個標題。

任何幫助,將不勝感激。

回答

2

您可以使用$ _ SERVER [「REQUEST_METHOD」]在你的代碼中切換行爲。

if ($_SERVER["REQUEST_METHOD"] == "POST"): 
    ?><h5 class="when-text"> Form submitted</h5><?php 
else: 
    ?><h5 class="when-text"> Form not submitted</h5><?php 
endif; 

對於這種方法的工作,你將不得不形式的方法設置爲POST

1

把你的形式隱藏input

<form action="" method="post"> 
-------- 
-------- 
<input type="hidden" name="process" value="1" /> 
</form> 

之後,而不是在呼應你的PHP文件,使用下面的代碼在HTML文件,以顯示h5

<?php if (isset($_POST['process']) && ($_POST['process'] == 1)): ?> 
<h5 class="when-text"> Form submitted! </h5> 
<?php else: ?> 
<h5 class="when-text"> Form not submitted </h5> 
<?php endif; ?> 

如果你的表格是由用戶提交的,$_POST['process']將1

0

您可以更好地使用jquery了點。當表單已成功提交了下面的代碼添加到它:

$(".when-text").html("Form submitted"); 

確保您包括jQuery庫做這個