Thursday, October 21, 2010

Access Controller variable in tpl file in Zend Framework


How to access Controller variable in tpl file in Zend Framework

In controller assign the variable name and it's value as listed below:

Controller file name EditController.php
---------------------------
//$this->view->assign('VARIABLE-NAME', 'VARIABLE-VALUE');
$this->view->assign('listing_id_for_view', $listing_id_for_view);


index.tpl:
---------------
 Access here as listed below:
<?php echo "Your listing id is : ".$this->listing_id_for_view ; ?>




Wednesday, October 13, 2010

Post entire form and return JSON data from PHP


Here is the entire code that will post entire PHP
form to the target page. The target page will receive
the post data and return the result in the format of JSON data.

Here are the two things one is it will send entire form data,
so you not need to mention every control name with parameter in Ajax.
second one is the target page which receive the data, is return output as
JSON Format.

First you need to download the latest jquery from the jquery site and then copy and
paste the below code and run it.

===========================================================
[1] email-form.php
------------------------------------
<script type="text/javascript" src="jquery-1.6.js"></script>
<form id="emailForm">
<p>Name:<br />
<input id="nName" type="text" name="nName"/></p>
<p>E-mail:><br />
<input id="email" type="text" name="email"/></p>
<p><input type="submit" value="Submit" /></p>
<div id="msg"></div>
</form>
<script type="text/javascript">
$(function(){
    $("#emailForm").submit(function(){
        dataString = $("#emailForm").serialize();
        $.ajax({
        type: "POST",
        url: "email-form-process.php",
        data: dataString,
        dataType: "json",
        timeout: 5000,
        error: function (xhr, err) {
                $("#msg").html(xhr.responseText + xhr.readyState + xhr.status);
            },
        success: function(data) {
            $("#msg").html("Hi " + data.nName + ", your email is " + data.email);
        }
    });
    return false;           
});
});
</script>

===========================================================
[2] email-form-process.php
------------------------------------
<?php
$retArr = array();
$retArr["nName"] = $_POST['nName'];
$retArr["email"] = $_POST['email'];
echo json_encode($retArr);
?>

===========================================================