phalcon and jquery ajax

11,176

If you navigate to /xml/posalji you will see that the whole layout with your echoed string appears. You will need to disable the view for that particular action:

public function posaljiAction()
{
    $this->view->disable();

    $data = "Ja se zovem Nedim Omerbegovic";
    echo json_encode($data);
}

If you want to access any control that is displayed on the view via jQuery, all you need to know is its id. So if you pass in the Tag::textField() parameters like these:

$options = array(
    'some_field_name',
    'id'    => 'some_id',
    'class' => 'some_class',
    'size'  => '10',
);

echo Tag::textField($options);

you can then access its value from jQuery like this

$('some_id').val()

References:

jQuery val()

Phalcon Tag Helpers

Share:
11,176
Nedimo
Author by

Nedimo

Updated on June 08, 2022

Comments

  • Nedimo
    Nedimo almost 2 years

    I am using Phalcon PHP framework. First I have problem to get value of Tag::textField in jquery. I am interested in accessing a value of Tag::textField from jquery. Second question is how to call a controller's method using AJAX. I am trying to call method posalji() from my XML controller on a button click, and it's not warking. Here is my controller method I am trying to call:

    public function posaljiAction()
    {
        $data = "Ja se zovem Nedim Omerbegovic";
        echo json_encode($data);
    }
    

    And here is my view (pretraga.phtml):

    <?php
        use Phalcon\Tag;
    ?>
    <?php
        echo Phalcon\Tag::javascriptInclude("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false);
        echo Phalcon\Tag::javascriptInclude("javascript/jquery.js");
    ?>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#dugme").click(function(){
                    $.ajax({
                        url: "<?php echo $this->url->get("xml/posalji") ?>",
                        type:'POST',
                        dataType: 'json',
                        success: function(data) {
                            alert(data);
                        }
                    });     
                });
            });
        </script>
    
        <h2>Primjer penosa varijable iz controllera to view-a</h2>
        <?php echo Tag::form("xml/pretraga"); ?>
        <p>
            <label for="name">Naslov</label>
            <?php echo Tag::textField("naslov") ?>
        </p>
        <p>
            <?php echo Tag::submitButton("prikazi") ?>
        </p>
    </form>
    
    
    <form>
        <label for="text">Unesi:</label>
        <p>
            <input type="text" id="unos"/>
        </p>
        <input type="submit" id="dugme" value="Prikazi"/>
    </form>
    

    Thanks in advance.