【问题标题】:prevent form submit with empty input? [duplicate]防止表单提交空输入? [复制]
【发布时间】:2012-11-19 17:58:00
【问题描述】:

可能重复:
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>

我希望能够这样做,因此如果文本框为空白,它会显示“他们在您的墙上发帖时出错,请重试”。

【问题讨论】:

标签: php javascript jquery html css


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

在客户端捕获空输入提交,如副本中所示,可能是一个附加功能,但您不会绕过在服务器端执行此操作。

【讨论】:

    猜你喜欢
    • 2012-08-07
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2013-07-25
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多