[英]How can I confirm something in JavaScript and then use
我有一个表单,当您提交它时,会显示一条JavaScript消息。
$message = "Want to insert?";
echo "<script type='text/javascript'>window.confirm('$message');</script>";
我想做的是,如果该人单击“确定”,它将在数据库中插入值,否则将其取消。
$titulo = $_POST['titulo'];
$mensagem = $_POST['mensagem'];
$ano = $_POST['ano'];
$mes = $_POST['mes'];
$dia = $_POST['dia'];
$link = " ";
使用<form>
标记,并处理onsubmit事件。 他在消息框上单击“确定”后,提交表单。
提交表单后,在服务器端(PHP)中编写代码以获取数据(通过GET / POST或发送方式)并插入表中。
简而言之, 首先执行PHP ,然后执行客户端JavaScript。 如果要使其具有交互性,则必须使用AJAX,这将允许您发送由JavaScript控制的PHP命令。
如果我是你,我会这样做:
if (confirm("Are you sure?"))
$.post("path/to/php.php", {data: you_need_to_send}, function (response) {
if (response == "OK")
alert("All Done!");
});
else
alert("You cancelled the insertion.");
注意: 在这里,
$.post()
是AJAX POST方法的jQuery实现。 为了便于解释,我给出了这个。 请不要因为回答JavaScript问题的jQuery方法而对我大喊大叫。
更新资料
您可以使用表单的onsubmit
来实现这一点。 确保在事件内给您return
:
<form method="post" action="http://example.com/" onsubmit="return confirm('Are you sure?');"> <input /> <input type="submit" value="Send" /> </form>
运行上面的代码片段并签出。
您需要使用请求将数据传递到服务器。 由于您已经使用过PHP,因此无法使用JavaScript直接在数据库中进行写入
但是在发送请求之前先确认用户,或者可以在发送请求之前在JavaScript代码中使用此用户。
if (confirm('Your Message')) {
// User click OK
// Send your data to server using a Request
} else {
// User click cancel
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.