如何在php中完成表單驗證之後設置彈出確認對話框?如果用戶點擊提交按鈕,那麼它應該驗證用戶輸入,最後它會詢問確認信息,如「你想保存嗎?」。用OK和取消按鈕,那麼只有它應該將數據保存到數據庫中。 請參考下面的代碼。如何在保存之前和單頁中的表單驗證之後設置PHP確認對話框
<?php
error_reporting(~E_NOTICE);
$name_err='';
$name='';
$type_err='';
$type='';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit'])) {
$e = true;
if (empty($_POST['name'])) {
$name_err = "Please enter the Product Name";
$e = false;
} else {
$name = $_POST['name'];
// check if name only contains letters and whitespace
}
if (empty($_POST['type'])||$_POST['type']=="select") {
$type_err = "Please select the Product Type";
$e = false;
} else {
$type = $_POST['type'];
}
$con = mysqli_connect("localhost", "root", "", "csr");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
}
//final check whether it is true the input will process
if($e)
{
mysqli_select_db($con, "csr");
mysqli_query($con, "insert into products(employee_id, name, type, brand, model, url, conditions, about, listedon, image_path, reserved_price, Purchased_price, buy_now_price, sale_end_date)
values('$eid','$name','$type','$brand','$model','None','$condition','$about','$listedon','$imgName','$reserved_price','$Purchased_price','$buy_now_price','$sale_end_date')") or die('Could not connect: ' . mysql_error());
header('Location:/sellproduct/index/');
mysqli_close($con);
}
}
?>
<form action=" " method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">
<p>
<input type="hidden" name="employee_id" value="" id="employee_id" />
</p>
<p>
<label for="name" class="required">Product Name</label>
<span class="field">
<input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>
</p>
<p>
<label for="type" class="required">Product Type</label>
<span class="field">
<select name="type" id="type" minlength="2" maxlength="60">
<option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : ""); ?> selected disabled >Select</option>
<option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
<option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
<form action=" " method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">
<p>
<input type="hidden" name="employee_id" value="" id="employee_id" />
</p>
<p>
<label for="name" class="required">Product Name</label>
<span class="field">
<input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>
</p>
<p>
<label for="type" class="required">Product Type</label>
<span class="field">
<select name="type" id="type" minlength="2" maxlength="60">
<option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : ""); ?> selected disabled >Select</option>
<option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
<option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
<form action=" " method="post" class="stdform stdform2" autocomplete="off" enctype="multipart/form-data">
<p>
<input type="hidden" name="employee_id" value="" id="employee_id" />
</p>
<p>
<label for="name" class="required">Product Name</label>
<span class="field">
<input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>
</p>
<p>
<label for="type" class="required">Product Type</label>
<span class="field">
<select name="type" id="type" minlength="2" maxlength="60">
<option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : ""); ?> selected disabled >Select</option>
<option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
<option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
</select><br><span class="error"><?php echo $type_err;?></span></span> </p>
<p class="stdformbutton">
<input type="submit" class="bluishBtn button_small" name="submit" value="SAVE" id="submit">
<button name="cancel" id="cancel" type="button" class="greyishBtn button_small">Cancel</button> </p>
</form>
請幫幫我。
感謝您的及時回覆,它不詢問任何確認框,也沒有輸入信息。 –
我剛糾正了代碼。由於該函數也被稱爲確認,因此出現了錯誤。我只是嘗試過,現在它應該可以工作:-) – saperlipopette
非常感謝你.....祝你有美好的一天! –