php_begins Posted February 4, 2012 Share Posted February 4, 2012 I am a beginner in the ZEND framework. I am passing a variable through ajax query like this $.ajax({ method: "GET", url: "/filename/fetch-client-data.php", dataType: 'json', // and so on I need to get the variable passed by the form. I dont know how to use the $_GET['varaible name'] from the form. Here is what I am trying in the controller function public function fetchClientDataAction() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(TRUE); $this->get('variablename')=$variable_name; } Can someone point me in the correct direction Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/ Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 $this->getPost(key) if i recall correctly... I have used $this->_getParam on some project. haha. check out the zend.controller.request.html Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314518 Share on other sites More sharing options...
gizmola Posted February 4, 2012 Share Posted February 4, 2012 In your controller, you get your "GET" variables via $var = $this->_request->getQuery('key'); It's unclear from your code what the key is for the data you're passing. Typically however, you'd use the post method, and get the post variables like this: $var = $this->_request->getPost('key'); Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314526 Share on other sites More sharing options...
php_begins Posted February 4, 2012 Author Share Posted February 4, 2012 I am using jquery.ajax to fetch remianing form elements when i click the fetch button. so i need to use something like $leadid=$_GET['leadid'] in the controller. I shall try the method above you suggested. This is how my view function looks like now: <script type="text/javascript"> $(document).ready(function() { function myrequest(e) { var leadid = $('#leadid').val(); $.ajax({ method: "GET", url: "/pdp/fetch-client-data.php", dataType: 'json', cache: false, data: { leadid: leadid }, success: function( responseObject ) { alert('success'); $('#clientname').val( responseObject.clientname ); $('#state').val(responseObject.state); /* once you've gotten your ajax to work, then go through and replace these dummy vals with responseObject.whatever */ }, failure: function() { alert('fail'); } }); } $('#fetchFields').click(function(e) { e.preventDefault(); myrequest(); }); }); [code]<form action ='<?php echo $SERVER['PHP_SELF']; ?>' method='post'> <tr> <td> <label for="leadid">Lead id: </label> <input type="text" name="leadid" id="leadid"> <button id="fetchFields">Fetch</button> </td> </tr> <tr> <td> <label for="clientname">Client Name: </label> <input type="text" name="clientname" id="clientname"> </td> </tr> <tr> <td> <label for="state">State: </label> <input type="text" name="state" id="state"> </td> </tr> </form> Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314529 Share on other sites More sharing options...
php_begins Posted February 4, 2012 Author Share Posted February 4, 2012 when i click on the fetch button the page gets refreshed and i get nothin! Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314552 Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 Debug the request via firebug and see what the request returns. Also showing the php function which you're directing the call would be helpful. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314557 Share on other sites More sharing options...
php_begins Posted February 4, 2012 Author Share Posted February 4, 2012 This is my php code: public function fetchClientDataAction() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(TRUE); $leadid=$this->get['lead_id']; $lead_query=$this->db->query(" SELECT client_name,state FROM `pdp_client_info` WHERE lead_id='$leadid' "); $this->view->lead_query=$lead_query->fetchALL(); //$this->view->lead-count=count($lead_query); if(count($lead_query) > 0) echo json_encode($lead_query); } Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314559 Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 $leadid=$this->get['lead_id']; Do you have a get array on your class? Or are you trying the get the passed variable to that? If I trust gizmola on this one, the code should be: $leadid=$this->_request->getQuery('lead_id'); also: if(count($lead_query) > 0) is a syntax error, missing a ')'. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314563 Share on other sites More sharing options...
php_begins Posted February 4, 2012 Author Share Posted February 4, 2012 Thanks a lot. That solved one part of the problem. When I click fetch, the page doesnt refresh and i get a success alert. However it does not fill the remaining form fields. So i am not sure if the php code is getting the correct results and returning it back in proper json format. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314566 Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 You could easily view that with firebug, but for the sake of simplicity you can loop through the object and check if it contains anything. $.each(responseObject, function(i,v)) { alert('index: '+i+' has value: '+v); } replace the success alert with the each loop and you will see if the script has returned the right values. And shouldn't db execute return a resource rather than array? If it does you might want to return $lead_query->fetchAll() instead of the query result itself. Just throwing ideas around here, you might want to consult zend documentation on the database functions. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314571 Share on other sites More sharing options...
php_begins Posted February 4, 2012 Author Share Posted February 4, 2012 Thanks you have been of great help. I ll dig deeper and come back with the solution. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314572 Share on other sites More sharing options...
php_begins Posted February 5, 2012 Author Share Posted February 5, 2012 This is the output of json encode. I wonder why I am not able to retrieve the following data in my form fields! [{"client_name":"Steve Harris","state":"TX"}] Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314879 Share on other sites More sharing options...
php_begins Posted February 5, 2012 Author Share Posted February 5, 2012 oh is it because of the brackets []. is there a way i can remove it and echo? Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1314885 Share on other sites More sharing options...
noXstyle Posted February 6, 2012 Share Posted February 6, 2012 Can you debug the part of the code that is being json encoded? if you have two dimensional array, i.e. array(array('name'=>x,'status'=>x)), you will have to access it differently. You probably can access this kind of data using responseObject[0].name or responseObject[0].status but i think it would be more convenient to get a simple array to the json_encode. In that case it would work in a way you're using it now. Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1315103 Share on other sites More sharing options...
php_begins Posted February 6, 2012 Author Share Posted February 6, 2012 For a quick fix. I tried the following and worked. echo str_replace( array( '[', ']'), '', json_encode($this->view->lead_query)); Quote Link to comment https://forums.phpfreaks.com/topic/256407-_get-syntax-in-zend/#findComment-1315106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.