我有一个form.php文件,当我提交表单时,该文件会重定向到sendformdata.php文件。但是,当没有填写正确的字段时,我无法让sendformdata.php文件在表单中显示错误消息。
我只是被重定向到包含文件db_connect.php,它显示为空白页面。如何让页面保留在form.php上并在html中显示错误消息?这是我当前的sendformdata.php文件:
<form class="form-horizontal" role="form" action="handleform/sendformdata.php" method="POST">
<legend></legend>
<div class="form-group">
<label for="lokotitle" class="col-sm-2">Loko Title</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="lokotitle" id="lokotitle" placeholder="Title">
<span id="notitle"></span>
</div>
这是form.php的一部分,我试图在以下位置显示错误消息:
string Medicine_Name = dataGridView1.Rows[e.RowIndex].Cells["Medicine_Name"].Value.ToString();
string Dealer_name = dataGridView1.Rows[e.RowIndex].Cells["Dealer_name"].Value.ToString();
int Availability =Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells["Availability"].Value);
if (dataGridView1.IsCurrentRowDirty)
{
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connectionString;
string cmd1 = "insert into Medicine_Available_Detail(Medicine_Name,Dealer_name,Availability) values(@Medicine_Name,@Dealer_name,@Availability)";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Medicine_Name",Medicine_Name);
cmd.Parameters.AddWithValue("@Dealer_name", Dealer_name);
cmd.Parameters.AddWithValue("@Availability", Availability);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
提前感谢您的帮助!
答案 0 :(得分:1)
我建议你同时使用html5 required
属性来强制用户输入数据。但请记住,用户可以绕过前端验证(通过检查元素),因此后端验证也是必要的。
使用必需的属性,如:
<input type="text" required />
你可以试试这个
if(empty($lokotitle)) {
$error_message = "Please input a Loko title.";
echo "<script>
document.getElementById('#notitle').value='".$error_message."';".
"</script>";
exit;
}
如果这可以解决您的问题,请告诉我