[英]How do I POST when a dropdown is selected then popup a jquery overlay filed with data from a sql database?
I'm trying to create a jquery overlay displaying results from a sql database. 我正在尝试创建一个jQuery叠加,以显示来自SQL数据库的结果。 The results come from a select statement with php that was populated from the information received from an html select dropdown.
结果来自带有php的select语句,该语句是从html select下拉列表中接收的信息填充的。 Once the user has confirmed they've read, I want to update the database to reflect this and allow for the user to continue completing the original form.
用户确认已阅读后,我想更新数据库以反映此情况,并允许用户继续填写原始表格。
I can't figure out how to submit the original form, then pop up the overlay, submit the form on the overlay, but keep the original page allowing it to be completed. 我不知道如何提交原始表单,然后弹出叠加层,在叠加层上提交表单,但是保留原始页面以使其完成。
So this: 所以这:
<form action="myformpage.php" method="post" id="myform">
<select id="username" onchange="this.form.submit()">
<option value='01'>User 1</option>
<option value='02'>User 2</option>
</select>
<select id="location">
<option value='north'>North</option>
<option value='south'>South</option>
</select>
<input id="subbut" type="submit" value="SUBMIT" style="display: none;" />
</form>
jquery: jQuery的:
<script type="text/javascript">
$(document).ready(function () {
$("#popup").overlay();
$("#confirm").click(function() {
$("#popup").overlay().close();
var dataString = 'confirmread=Y';
$.ajax({
type: "POST",
url: "read_message.php?",
data: dataString,
success: function() {
$('#location').show();
}
});
});
</script>
Oddly the submit button never works when style is set to none. 奇怪的是,将样式设置为无时,提交按钮将永远无法使用。 (Side question: Is this because it was never visible to the browser at load?)
(旁问:这是因为浏览器在加载时从不可见吗?)
You'll want to do an ajax post and generate the popup (probably via jQuery UI) with a successful post: 您将要进行ajax发布,并通过成功发布来生成弹出窗口(可能通过jQuery UI):
html: 的HTML:
<select id="username">
<option value='01'>User 1</option>
<option value='02'>User 2</option>
</select>
jquery: jQuery的:
$("#username").change( function() {
$.post( "myformpage.php", {username: $(this).val()}, function( data ) {
// Create popup here
});
});
your "myformpage.php" can take care of the database stuff and return whatever you want in your popup. 您的“ myformpage.php”可以处理数据库内容,并在弹出窗口中返回您想要的任何内容。
You don't even have to check for it if you want to do it like this. 如果要这样做,您甚至不必检查它。
<?php
//your connection to db goes here //您与数据库的连接在这里
$message = "SELECT `message` FROM......";
$test="true"
?>
<script type="text/javascript">
function checkit()
{
alert("<?php $message; ?>");
}
</script>
<?php
using a redirect 使用重定向
?> <script> window.location.href="your page.php"; </script>
using css to hide & show 使用CSS隐藏和显示
$var="display:none;"; if($test=="true") { $var=""; } ?>
HTML 的HTML
<div style="<?php echo $var; ?>" name="original form" > your original form goes here </div>
You need ajax for this, is easy with jQuery. 为此,您需要ajax,使用jQuery很容易。
with jQuery you need to do something like this: 使用jQuery,您需要执行以下操作:
<script>
$(function(){
$("#username").change(function(){
$.post("myformpage.php",{ value : $(this).val(), function(data){
alert(data);
});
});
});
</script>
You can't style the default alert, you need to make your own dialog or use a library that can do that, like jQuery ui. 您无法设置默认警报的样式,需要创建自己的对话框或使用可以执行此操作的库,例如jQuery ui。
Yes IE have problems when you submit a form from javascript without a submit button but i recommend you to dont use that way to submit because this will do a postback that you dont need. 是的,当您从没有提交按钮的javascript提交表单时,IE会出现问题,但是我建议您不要使用这种方式提交,因为这将执行您不需要的回发。
Try something like this: 尝试这样的事情:
<script>
$(function(){
$("#username").change(function(){
$.post("retrievedata.php",{ username : $(this).val() },function(data){
$("#popup").html(data);
$("#popup").overlay();
});
});
$("#confirm").click(function() {
$("#popup").overlay().close();
var dataString = 'Y';
$.post("read_message.php",{confirmread:dataString},function(){
$('#location').show();
});
});
});
<script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.