Welcome Guest, Not a member yet? Register   Sign In
[Newbie] Need help to display results.
#1

Hi i am new in using codeigniter ihave this codes

This is my model
Code:
function select_qnum_query(){

        //SELECT qnum FROM tblkiosk WHERE transaction = "landtax"
        $this->db->SELECT('qnum');
        $this->db->FROM('tblkiosk');
        $this->db->WHERE("transaction", "landtax");
        $query = $this->db->get();

        return $query->result();

this is my controller
Code:
function select_qnum(){
    $qnumber = $this->json_model->select_qnum_query();
    echo json_encode($qnumber);
    }

and this is my view
Code:
<script src ="<?php echo base_url("assets/js/jquery-3.4.1.min.js"); ?>"></script>
<html>
testing
<br>
<button>Payment</button>
<div>
<h5>Your Priority number: </h5>
<span id = "qnum"></span>
</div>
</html>
<script>
$("button").click(function(){
        $.ajax({
            url: "<?php echo base_url() ?>json_controller/select_qnum",
            type: "GET",
            dataType: 'json',
            success: function(data){
                $('#qnum').html(data);
            },
            error: function(){
                alert('Error....');
            }
        });


    });
</script>
it does not display any output. please help me
Reply
#2

(This post was last modified: 03-10-2020, 03:08 PM by tweenietomatoes.)

PHP Code:
<?php echo base_url() ?>

missing ; so make it base_url();

or use

PHP Code:
<?=base_url()?>
pro novice
Reply
#3

Your model uses result() that does not exis on $query object. 
You should use getResult() instead. I also simplified your code a bit: 
PHP Code:
function select_qnum_query()
{
    
$qb $this->builder();
    $query $qb->select('qnum')->where("transaction""landtax")->get();
    
    
return $query->getResult();


You could also benefit from AP Response Trait in your controller to get rid of echo, https://codeigniter4.github.io/userguide...onses.html
Reply
#4

(This post was last modified: 03-10-2020, 04:50 PM by tweenietomatoes.)

return $qb->select('qnum')->where("transaction", "landtax")->get()->getResult();
pro novice
Reply
#5

(This post was last modified: 03-11-2020, 10:25 AM by jvandemerwe.)

Code:
<script>
$("button").click(function(){

Are you sure that you should not have a ready in your JavaScript?

Code:
$(document).ready(function () {
 
    $("button").click(function(e) {
      e.preventDefault();
     
      ... etc ...

    });

});

You could put a var_dump('<pre>', $queryResult, '</pre>') in your model to see if you have any records. Even when using Ajax the browser will show the result for test.
Reply
#6

(03-10-2020, 03:40 PM)zahhar Wrote: Your model uses result() that does not exis on $query object. 
You should use getResult() instead. I also simplified your code a bit: 
PHP Code:
function select_qnum_query()
{
    $qb $this->builder();
    $query $qb->select('qnum')->where("transaction""landtax")->get();
    
    
return $query->getResult();


You could also benefit from AP Response Trait in your controller to get rid of echo, https://codeigniter4.github.io/userguide...onses.html

Still it doesn't run. Does it need ->from('tblkiosk')? I tried to put it but still no result. :(
Reply
#7

(03-11-2020, 10:23 AM)jvandemerwe Wrote:
Code:
<script>
$("button").click(function(){

Are you sure that you should not have a ready in your JavaScript?

Code:
$(document).ready(function () {
 
    $("button").click(function(e) {
      e.preventDefault();
    
      ... etc ...

    });

});

You could put a var_dump('<pre>', $queryResult, '</pre>') in your model to see if you have any records. Even when using Ajax the browser will show the result for test.
Still no result. Sad
Reply
#8

Where are your <head> and <body> tags your JavaScript should go just above your </body></html>

unless it is called first in your html then the scripts need to be in the <head></head> tags.

How does jQuery know which button is being clicked?

Code:
<button id="btnSubmit">Payment</button>

$('#btnSubmit').click(function(){
    $.ajax({
        url: "<?php echo base_url() ?>json_controller/select_qnum",
        type: "GET",
        dataType: 'json',
        success: function(data){
            $('#qnum').html(data);
        },
        error: function(){
            alert('Error....');
        }
    });
});
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB