阻止表单提交空输入?

时间:2012-12-01 20:27:45

标签: php javascript jquery html css

  

可能重复:
  How to prevent submitting the HTML form’s input field value if it empty

<?php

include '../core/init.php';
if(isset($_POST['nt']) && isset($_POST['ntb'])){
mysql_query("INSERT INTO `post` (`myid`, `text`) VALUES ('".$_SESSION['id']."', '".mysql_real_escape_string($_POST['nt'])."')");
}
?>

<iframe style="display:none" name="submissiontarget" id="submissiontarget"></iframe>
<form method="post" target="submissiontarget" action = "../ajax/post.php" name="form">
<input type="textbox" name="nt" class="postwall" id="nt"  placeholder="Post on your wall" rows="7" wrap="physical" cols="40" onfocus="if(this.value=='Post On Your Wall')this.value='';" onblur="if(this.value=='')this.value='Post On Your Wall';" />
<input type="submit" style="display:none;" name='ntb' id='ntb'  class="Buttonchat"     value="Post" />
</form>

我希望能够这样做,所以如果文本框是空白的话,它说'他们在你的墙上贴了一个错误,请再试一次'。

1 个答案:

答案 0 :(得分:0)

if(isset($_POST['nt']) && isset($_POST['ntb'])) {
    …
} else {
    echo "There was an error posting on your wall, please try again.";
}

在客户端捕获空输入提交,如副本中所示,可能是一个额外的功能,但你不会绕过服务器端。