如何从数据库中追加表?

时间:2015-03-24 05:35:36

标签: jquery ajax codeigniter

视图功能。

   function detil_table2()
    {
    var id_wo       =   $('#id_wo').val();
    var base_url    =   $("#base_url").val();
        $.ajax({
                    type: 'GET',
                    url: ""+ base_url +"jsonLive.php?query="+"select *,(select nama_bahan from reff_bahan where detil_wo.id_bahan = reff_bahan.id_bahan limit 1) as bahan,(select nama_laminating from reff_laminating where detil_wo.id_laminating = reff_laminating.id_laminating limit 1) as laminating from detil_wo where id_wo='"+ id_wo +"' order by id_detil asc "+"&jsoncallback=?",
                    dataType:"jsonp",
                    crossDomain:true,
                    cache:false,
                    success:function(data){
                        var squ=data.statusQuery;
                        var tro=data.totalRows;
            alert(tro);
                        if(tro==0){
                        }else{ 
                            $.each(data.data,function(i,data){ 
    $('#thetable').append('<thead><tr><th>No</th><th>Bahan</th><th>Laminating</th><th>Ukuran</th><th>Jumlah</th><th>Finishing</th><th>Catatan</th><th>File </th><th>ID Detail</th></tr></thead><tbody><tr><td>XX</td><td>' + data.bahan + '</td><td>' + data.laminating + '</td><td>' + data.ukuran + '</td><td>' + data.jumlah + '</td><td>' + data.finishing + '</td><td>' + data.file_upload + '</td><td>' + data.id_detil + '</td></tr></tbody>');
                                }
                            });
                        }

它不会跑,甚至没有警觉。

这是 js文件

$(document).ready(function() {
$(".detail_wo").click(function() {
var id_wo           = $("#id_wo").val();
var id_detil        = $("#id_detil").val();
var id_bahan        = $("#id_bahan").val();
var id_laminating   = $("#id_laminating").val();
var ukuran1         = $("#ukuran1").val();
var ukuran2         = $("#ukuran2").val();
var jumlah          = $("#jumlah").val();
var finishing       = $("#finishing").val();
var keterangan      = $("#keterangan").val();
var file_upload     = $("#file_upload").val();
var url_detil       = $("#url_detil").val();
var base_url        = $("#base_url").val();


var dataString = 'id_wo='+ id_wo +'&id_detil='+ id_detil + '&id_bahan=' + id_bahan + '&id_laminating=' + id_laminating + '&ukuran1=' + ukuran1 + '&ukuran2=' + ukuran2 + '&jumlah=' + jumlah + '&finishing=' + finishing + '&keterangan=' + keterangan + '&file_upload=' + file_upload;

    if(id_bahan==''|| id_laminating=='' || ukuran1==''|| ukuran2=='')
{
//document.write(base_url);
$('#success2').fadeOut(200).hide();
$('#error2').fadeOut(200).show().delay(2000).fadeOut(200);
}
else
{
$.ajax({
type: "POST",
//url: "order/detail_wo",
url: url_detil,
data: dataString,
success: function(){
//document.write(dataString);
detil_table2();
$('#success2').fadeIn(200).show().delay(2000).fadeOut(200);
$('#error2').fadeOut(200).hide();
$('#id_bahan,#id_laminating,#ukuran1, #ukuran2, #jumlah, #finishing, #keterangan, #file_upload').val('');
}
});
}
return false;
});
});

我正在尝试在提交表单时更新表而不刷新 数据添加到数据库但没有成功警报。

我的代码有问题吗?

编辑:

<?php
include "connect_.php";

$query=$_REQUEST['query'];
$table=explode(" ",$query);

$arrayJson = array();

$q=mysql_query("$query") or die(''.$_GET['jsoncallback'].'({"data":[],"statusQuery":'.json_encode("0").',"totalRows":'.json_encode("0").'})');

$status=1;

if($table[0]=="select"){
    $jum=mysql_num_rows($q);

    while($row=mysql_fetch_assoc($q)){
        $arrayJson[]=$row;
    }

}else{
    $jum=1;
}

//JSON-encode and return
print $_GET['jsoncallback'].'({"data":'.json_encode($arrayJson).',"statusQuery":'.json_encode("$status").',"totalRows":'.json_encode("$jum").'})';

添加了jsonLive.php

2 个答案:

答案 0 :(得分:0)

<强> jquery的

<html>
    <head>        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>        
        <script type="text/javascript">

            $(document).ready(function () {
                $.ajax({
                    type: "GET",
                    url: "phpfile.php",
                    dataType: "json",
                    success: function (data) {
                        $.each(data, function (idx, obj) {                          
                            var Status = 'Active';
                            if (obj.status == '0')
                            {
                                Status = 'Inactive';
                            }
                            $('#get_table > tbody').append('<tr><td>' + obj.user_id + '</td><td>' + obj.user_name + '</td><td>' + Status + '</td></tr>')

                        });
                    }
                });
            });
        </script>

    </head>
    <body>

        <table id="get_table"  border="1">
            <thead>
                <tr>
                    <th align=center>Id</th>
                    <th align=center>Name</th>
                    <th align=center>Status</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

    </body>
</html>

<强> phpfile.php

<?php

$host = "localhost";
$user = "root";
$password ="";
$database= "databasename";

$con = mysqli_connect($host , $user , $password);
$database_connect = mysqli_select_db($con, $database);

$result = mysqli_query($con, "select Id as user_id,Name as user_name,Status as status from users");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);

echo json_encode($data);

?>

答案 1 :(得分:0)

试一试。你只是错过了一些关闭你的功能

function detil_table2()
{
var id_wo       =   $('#id_wo').val();
var base_url    =   $("#base_url").val();
    $.ajax({
                type: 'GET',
                url: ""+ base_url +"jsonLive.php?query="+"select *,(select nama_bahan from reff_bahan where detil_wo.id_bahan = reff_bahan.id_bahan limit 1) as bahan,(select nama_laminating from reff_laminating where detil_wo.id_laminating = reff_laminating.id_laminating limit 1) as laminating from detil_wo where id_wo='"+ id_wo +"' order by id_detil asc "+"&jsoncallback=?",
                dataType:"jsonp",
                crossDomain:true,
                cache:false,
                success:function(data)
                {
                    var squ=data.statusQuery;
                    var tro=data.totalRows;
        alert(tro);
                    if(tro==0){
                    }else{ 
                        $.each(data.data,function(i,data){ 
$('#thetable').append('<thead><tr><th>No</th><th>Bahan</th><th>Laminating</th><th>Ukuran</th><th>Jumlah</th><th>Finishing</th><th>Catatan</th><th>File </th><th>ID Detail</th></tr></thead><tbody><tr><td>XX</td><td>' + data.bahan + '</td><td>' + data.laminating + '</td><td>' + data.ukuran + '</td><td>' + data.jumlah + '</td><td>' + data.finishing + '</td><td>' + data.file_upload + '</td><td>' + data.id_detil + '</td></tr></tbody>');

                        });
                    }

                }
            });

}