我有一个表单,我想在对话框中向用户显示他们的输入数据,并使用 PHP 一次单击将其保存在 SQL 中。问题是我一次只能调用一个(对话框或 PHP)。由于对话框需要 anchor
标签
<a href="#" value="Save" onclick="submit()">Save</a>
<script>
document.forms["myform"].reset();
$(function(){
$("#dialog").dialog({
autoOpen:false,
modal:true,
width:400,
height:300,
buttons:{
"OK": function(){
$(this).dialog("close");
alert("Saved");
},
"Cancel":function(){
$(this).dialog("close");
}
}
});
});
function submit(){
$("#location").html($("#pac-input").val());
$("#type").html($("input:radio[name='sex']:checked").val());
$("#open").html($("#open").val());
$("#close").html($("#close").val());
$("#review").html($("#review").val());
$("#rating").html($("input:radio[name='rating']:checked").val());
$("#dialog").dialog("open");
}
</script>
PHP 需要提交按钮来存储值
<input type="submit" name="submit">
<?php
$address= $_POST['address'];
$type = $_POST['sex'];
$open = $_POST['open'];
$close = $_POST['close'];
$review = $_POST['review'];
$rating = $_POST['rating'];
$dbname = "addtoilet";
// Create connection
$conn = mysqli_connect("localhost:3306", "root","",$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
echo "Connected successfully";
}
$sql = "INSERT INTO add (address, type, open, close)
VALUES ('$address', '$type', '$open', '$close')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
最佳答案
在dialog的ok函数里面写submit函数
<a href="#" value="Save" onclick="submit()">Save</a>
<script>
document.forms["myform"].reset();
$(function(){
$("#dialog").dialog({
autoOpen:false,
modal:true,
width:400,
height:300,
buttons:{
"OK": function(){
$("#your_form_id").submit();// also you can call ajax here if you don't want to reload the page
$(this).dialog("close");
alert("Saved");
},
"Cancel":function(){
$(this).dialog("close");
}
}
});
});
function submit(){
$("#location").html($("#pac-input").val());
$("#type").html($("input:radio[name='sex']:checked").val());
$("#open").html($("#open").val());
$("#close").html($("#close").val());
$("#review").html($("#review").val());
$("#rating").html($("input:radio[name='rating']:checked").val());
$("#dialog").dialog("open");
}
</script>
关于javascript - 一键调用PHP和对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30342218/