0

I'd like to render a partial as a response to an ajax request in my controller action. My goal is to echo a Twitter Bootstrap Alert when ajax request is successful.

Here is my action (redigeraLottningAction):

if($this->getRequest()->isXmlHttpRequest()){
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->layout->disableLayout();
    echo $this->view->partial('partial/alert-ajax.phtml', array('type' => 'success', 'msg' => 'Lyckat! Lottningen är nu sparad.'));
}

And my partial (alert-ajax.phtml):

<div id="alert-msg" class="alert alert-<?= $this->type?> fade in">
    <a class="close" data-dismiss="alert" href="#">×</a>
    <p><?= $this->msg?></p>
</div>

The problem is that the php scripts in the partial is outputed as text. This is what it looks like in the browser with variables not rendered:

<div id="alert-msg" class="alert alert-<?= $this->type?> fade in">
    <a class="close" data-dismiss="alert" href="#">×</a>
    <p><?= $this->msg?></p>
</div>

And the jquery part:

$(document).ready(function(){

    $('#lottning-spara').click(function () {
        $.ajax({
            type: "POST",
            url: '/turnering/redigera-lottning',
            cache: false,
            data: {id: getParam("turnering"), klass_id: getParam("klass"), type: "spara", lottning: $('#lottning_str').val()},
            dataType: 'html',
            success: function(msg){
                $('#alert-div').html(msg);
            },
            error: function(){
                $('#alert-div').html('Error!');
            }
        });
    });
});

Any ideas why the php in partial is not rendered??

Edits are bold.

Problem Solved. The alert-ajax.phtml file was a different charset type.

adamnyberg
  • 58
  • 1
  • 8
  • are you sure it goes to partial/alert-ajax.phtml page? – Zuber Surya Jun 21 '12 at 12:57
  • @Zuber Surya Yes it's definitely that page. – adamnyberg Jun 21 '12 at 13:03
  • you should try @Ronn0 code and try giving extra comments to check – Zuber Surya Jun 21 '12 at 13:18
  • Done that. The problem is that the php scripts in the partial is outputed as text. – adamnyberg Jun 21 '12 at 13:28
  • try commenting out `$this->_helper->viewRenderer->setNoRender();` and see what happens, this maybe part of the issue. – RockyFord Jun 21 '12 at 13:35
  • I thought the same thing but thats just gives me the view rendered again + this code from the partial: <�div id="alert-msg" class="alert alert-<�?php echo $this->type; ?> fade in"> <�a class="close" data-dismiss="alert" href="#">�<�/a> <�p><�?php echo $this->msg; ?><�/p> <�/div> – adamnyberg Jun 21 '12 at 13:39
  • There's a part of the problem i guess, there seems to be something wrong with your charset. Try paste it in notepad and copy+paste it again. It sounds strange, but maybe that'll solve te problem. – Ronn0 Jun 21 '12 at 13:56
  • I installed a new editor and noticed that the charset was on auto. Changed charset and created a new file and now it working lika a charm. I'm new here so should I do anything whit this question now when the problem is solved? – adamnyberg Jun 21 '12 at 14:16
  • @adamnyberg you should write an answer with what the issue was, then mark resolved with your answer – Sara Fuerst Jun 15 '16 at 20:18

2 Answers2

1

What if you try this:

<div id="alert-msg" class="alert alert-<?php echo $this->type; ?> fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<p><?php echo $this->msg; ?></p>
</div>

if($this->getRequest()->isXmlHttpRequest()){
    $this->view->assign(array('type' => 'success', 'msg' => 'Lyckat! Lottningen är nu sparad.'));
    echo $this->view->render('partial/alert-ajax.phtml');
    exit;
}
Ronn0
  • 2,249
  • 2
  • 21
  • 36
  • What about this: Assign all vars and do this: echo $this->view->render('partial/alert-ajax.phtml'); – Ronn0 Jun 21 '12 at 13:03
  • Delete: $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); And exit; after the ->render(); – Ronn0 Jun 21 '12 at 13:36
  • I highly appreciate the time your time but the same thing happens. – adamnyberg Jun 21 '12 at 13:52
0

Problem Solved. The alert-ajax.phtml file was a different charset type.

adamnyberg
  • 58
  • 1
  • 8