【问题标题】:To display the records retrieved from database into a dialog box将从数据库检索到的记录显示到对话框中
【发布时间】:2016-06-17 12:17:50
【问题描述】:

我有一个接受数字输入的对话框。我想将此数字发送到数据库并检索与该数字对应的名称并将其显示到另一个对话框中。我的代码在检索数据之前工作正常,但我没有不知道如何将它显示到另一个对话框中。我创建了一个小提琴https://jsfiddle.net/payalsuthar/rdtpo7e3/ 这是dataretrieve.php-

 <?php
    include('db.php');
    $num = $_POST['num'];
    $sql = "SELECT `no`,`name` FROM `table` WHERE `no` = $num ";
     if($result=mysqli_query($conn,$sql)){ 
    //i dont know how to display the values retrieved here into my dialog2(you can check my dialog2)
   }
 ?>

HTML代码-

<form action="dataretrieve.php" method="POST">
<div id="dialog1" title="div1" style="display:none"> 
Enter the num:<input type="text" name="num" id="num" />
</div>
</form>
<button id="clickMe">Click Me</button>
<div id="dialog2" title="div1" style="display:none"> 
num:<input type="text" name="number" id="number" />

JavaScript 代码-

$("#clickMe").click(function(){

$('#dialog1').dialog({
buttons:{
"yes":function(){
$(this).dialog('close');
callback(true);
},
"no":function(){
$(this).dialog('close');
}
}
});
function callback(value){
if(value){
$('#dialog2').dialog({
buttons:{
"ok":function(){
$(this).dialog('close');
},
"cancel":function(){
$(this).dialog('close');
}
}
});
}
}
});

【问题讨论】:

  • 如果要在同一个页面上显示结果而不需要重新加载,必须使用ajax (http://api.jquery.com/jquery.ajax/)

标签: javascript php jquery html dialog


【解决方案1】:

您的代码将如下所示(未经测试):

jQuery Demo

PHP:使用echo将数据发送回javascript端

<?php
    include('db.php');
    $num = $_POST['number'];
    $sql = "SELECT `no`,`name` FROM `table` WHERE `no` = $num ";
    if($result=mysqli_query($conn,$sql)){ 
        $out = $result['yourDBfieldname'];
        echo $out; //THIS sends it back to javascript side
    }
?>

HTML:无需使用&lt;form&gt; 构造——AJAX 就是您所需要的

<div id="dialog1"> 
    Enter the num:<input type="text" name="num" id="num" />
    <button id="submit1">Submit</button>
</div>
<button id="clickMe">Click Me</button>

JavaScript/jQuery:

var num, nam;
$("#clickMe").click(function(){
    $('#dialog1').dialog('open');
});
$("#submit1").click(function(){
    $('#dialog1').dialog('close');
    num = $('#num').val();
    $.ajax({
        type: 'post',
         url: 'dataretrieve.php',
        data: 'number=' +num,
        success: function(recd){
            //if (recd.length) alert(recd);
            $('#dialog1').html('<input id="name" type="text" value="'+recd+'" /><button id="submit2">Submit</button>');
            $('#dialog1').dialog('open');
        }
    });
});
//Must use .on() because content injected after DOM initially rendered
$(document).on('click', '#submit2', function(){
    $('#dialog1').dialog('close');
    nam = $('#name').val();
    alert('Num: '+num+ '    Name: ' + nam);
});


//Initialize the jQuery dialog - makes the #dialog1 div hidden
$('#dialog1').dialog({
    autoOpen:false,
    modal:true,
    buttons:{
        "Close":function(){
            $(this).dialog('close');
        }
    }
});

注意:

不需要两个对话框。您可以通过.html() 方法更改#dialog1 div 中的内容来重复使用一个对话框。

【讨论】:

  • ,你能告诉我'd'代表什么--- if (d.length) alert(recd);
  • 抱歉,这是一个错误。我通常使用d 作为从 PHP 端接收到的数据的 var 名称。为了使代码更明显,我在上面的示例中将其重命名为rcvd,但忘记更改该行。该行也应该被注释掉——我把它留在那里是为了演示我经常使用的一种方便的故障排除技术。 代码已更新。
  • 非常感谢...它对我有用...请告诉我如何从数据库中检索多个值,因为现在它只是检索名称。我尝试将日期设为好吧,但它在同一个输入框中显示了两个值,以及如何在单击对话框@gibberish 的“确定”按钮时将编辑后的值保存在数据库中
  • 现在没有时间回答,所以为了获得最佳/最快的答案,请将其作为一个新问题提出。您将有数十人阅读该问题并立即尝试提供帮助,而不是等待我有时间。 祝您项目顺利 感谢您的勾选和投票。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2014-04-14
  • 2018-02-26
  • 2018-10-30
  • 2016-10-21
  • 2013-01-19
相关资源
最近更新 更多