开发者

Build php response for ajax

开发者 https://www.devze.com 2023-04-03 23:20 出处:网络
I\'m having a logical problem with my script. The point would be to get a some rows formatted in a table but the header should not be repeated and under all the items should be outputted and than as v

I'm having a logical problem with my script. The point would be to get a some rows formatted in a table but the header should not be repeated and under all the items should be outputted and than as variable passed to ajax . But I don't see how to solve this.

 function abc()
   {
      global $mainframe;

      $db      =& JFactory::getDBO();

      // Check for request forgeries
        if(isset($this->message)){
                $this->display('message');
   }
                // custom: generate token for ajax request
                        $ajax_token = JHTML::_( 'ajax.token' );
                            // custom end

//        JRequest::checkToken( 'get' ) or jexit( 'Invalid Token' );



        $letter_raw = JRequest::getVar('val');
        $letter = substr($letter_raw, -1);
        $response = '<div class="no-rec">not found</div>';
        $html = '';
        if (!empty($letter)) {
        $query = " SELECT * FROM #__glossary WHERE substr(tterm,1,1) LIKE '$letter%'";

        $db->setQuery( $quer开发者_开发技巧y );

        $rows = $db->loadObjectList();


        if (count($rows)) {

        $header='<table class="stripeMe"><tbody><thead><tr><th>Begriff</th><th>Definition</th></tr></thead><tr>';

        foreach($rows as $key => $row) {

        $body='<td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr></tbody></table>';

        }

        $response = $header.$body;


        }

        $html = $response;

        echo $html;




        }



   }


What exactly is the problem? :)

You probably should not make it a function, since I guess you are just going to load the content in with AJAX?

And you should ADD to the string not override it in each row.

UPDATED, FIXED HTML ERRORS

    if (count($rows)) {
    // CREATE TABLE AND HEAD
    $body = '<table class="stripeMe"><thead><tr><th>Begriff</th><th>Definition</th></tr></thead>';

    // TBODY FOR REPEAT INSIDE
    $body .= '<tbody>'
    foreach($rows as $key => $row) {
    $body .= '<tr><td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr>';
    }
    $body .= '</tbody></table>';

    $response = $body;
    }

    $html = $response;

    echo $html;


Well if you are passing the data back as HTML than this will work so your jquery would be:

$('#holderdiv').load('abc.php');

If you are using Something like .post .ajax .get, then you will need to decide what format to pass the data back with so if it's JSON then you will need to format it as such and make sure that your jQuery is told to expect such a response. I can give more help if you can be specific about your situation and what problems you are having.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号