如何在Jquery Easy UI中使用javascript函数调用php函数


How to call php function with javascript function in Jquery Easy UI

我在对话框中创建自定义自动编号时遇到问题 jquery easy ui..

我在控制器中有这样的功能。

public function getCode()
{
    if(!isset($_POST))  
        show_404();
    $query = "SELECT MAX(kode_barang) as maxID FROM tb_barang";
    $hasil = mysql_query($query);
    $data  = mysql_fetch_array($hasil);
    $idMax = $data['maxID'];
    $noUrut = (int) substr($idMax, 1, 4);
    $noUrut++;
    $newID = "B" . sprintf("%04s", $noUrut);
    $queryData  = $query->row_array();
    $phpVar     = array( "STATUS" => $newID); 
    echo json_encode ($phpVar) ;    
}

我从视图中调用该函数,像这样的javascript函数。

<script> function makeAjaxCall()
{ 
    $.ajax({ 
        type: "post", 
        data: $('#form').serialize(), 
        url: "http://192.168.0.77/ci_jquery/barang/getCode", 
        cache: false,   
        success: function(json){        
        var obj = jQuery.parseJSON(json); 
        var r = obj['STATUS'];
        document.forms["form"]["kode_barang"].value = r;
    } 
});
} 
</script> 

这是我的对话形式。

<div id="dialog-form" class="easyui-dialog" style="width:300px; height:400px; padding: 10px 20px" closed="true" buttons="#dialog-buttons">
    <form id="form" method="post" novalidate>
        <table border="0">
            <tr>
                <div class="form-item">
                <td width="100"><label for="type">Kode Barang </font></td>
                <td><input type="text" name="kode_barang"/></td>
            </tr>
</div>

感谢您的回复和关注。

我假设你的getCode()函数失败了。您将$query用作对象,但它只是一个字符串。

$query = "SELECT MAX(kode_barang) as maxID FROM tb_barang";
$queryData  = $query->row_array();

您应该会收到一个错误,指出您正在非对象上调用row_array()。您应该删除此行,因为它甚至没有被使用。

在将 PHP 代码与任何其他脚本/函数一起使用之前,您至少应该对其进行测试。这是除了在开发环境中在 PHP 中打开错误报告以显示此类内容之外。

试试这个:

You can do this by using jquery Ajax in your javascript function.
Set url parameter in ajax with file and function name.

-

谢谢