我是php,mysql和javascript的新手。最近我一直在为每个按钮创建一个弹出窗口。当我将代码放在表单标记中时,代码可以正常工作,但它是添加和删除的常规弹出窗口。我想要一个确认弹出窗口来添加和删除。我尝试将代码放在输入标签上,但代码没有运行。我做错了什么?
HTML
<form action="da.php" method="post">
name:<input type="text" name="input1">
<br/>
<input onSubmit="if(!confirm('Submit?')){return false;}" name="add1" type="submit" value="Submit" />
<input onSubmit="if(!confirm('Delete?')){return false;}" name="del1" type="submit" value="Delete" />
</form>
</body>
PHP
define('db_name', 'demo');
define('db_user', 'root');
define('db_password', 'pw');
define('db_host', 'localhost');
$link = mysql_connect (db_host, db_user, db_password);
if (!$link) {
die('could not connect: '. mysql_error());
}
$db_selected = mysql_select_db(db_name, $link);
if (!$db_selected) {
die('can\'t use' . db_name . ': ' . mysql_error());
}
$value = $_POST['input1'];
if (isset($_POST['add1'])) {
$sql = "insert into demo (name) values ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
}
elseif (isset($_POST['del1'])) {
$sql = "delete from demo where name = ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
}
mysql_close();
header( 'Location: localhost://da-form.html') ;
?>
答案 0 :(得分:0)
您可以在javascript中使用window.confirm来获取用户的确认。 http://www.w3schools.com/jsref/met_win_confirm.asp
答案 1 :(得分:0)
尝试onclick
而不是onSubmit
onSubmit
通常用于表单标记
答案 2 :(得分:0)
试试这个
<form action="da.php" method="post">
name:<input type="text" name="input1">
<br/>
<input onClick="return confirm('Submit?');" name="add1" type="submit" value="Submit" />
<input onClick="return confirm('Delete?');" name="del1" type="submit" value="Delete" />
</form>