[英]Displaying messages at top of the form in PHP
我想根據開機自檢結果在頂部ID“ MessageError”和“ MessageOK”上顯示消息。 例:
<p id="MessageError"></p>
<p id="MessageOK"></p>
<form name="Form" method="post" action="<?php $_SERVER[ 'PHP_SELF' ]; ?>" enctype="multipart/form-data" accept-charset="UTF-8" id="Form">
<input type="text" name="test" value="" /> <input type="submit" name="Submit" value="" />
</form>
<?php
if ( isset ( $POST[ 'Submit' ] ) ) {
if ( $_POST[ 'test' ] ) {
// Echo message at "MessageOK
}
else {
// "Echo message at "MessageError"
}
}
?>
任何幫助將不勝感激。
謝謝。
將代碼移到表格上方,以在表格上方打印錯誤消息。 另外,您可以即時創建段落標簽,以避免浪費:
<?php
if(isset($_POST['submit'])){
if($_POST['test'])echo("<p id='MessageOk'>There was an Error</p>");
else echo("<p id='MessageError'>There was no error</p>");
}
?>
如果您對使用PHP向預先創建的div添加內容的做法一無所知,可以建議使用PHP創建輸入,例如:
<?php
$test = $_POST['test'];
echo("<input type='hidden' id='test' value='$test' />");
?>
然后使用JavaScript追加數據:
if(document.getElementById('test').value){
document.getElementById('MessageOk').innerHTML = 'No Error';
}
else{
document.getElementById('MessageError').innerHTML = 'Error ??';
}
將您的php代碼移到表單上,將echo消息分配給變量,然后使用<?php echo $variable; ?>
<?php echo $variable; ?>
在適當的位置打印消息...
確保在您的post變量中包含_。
$_POST[]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.