Jump to content

$_GET syntax in ZEND


php_begins

Recommended Posts

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

Link to comment
Share on other sites

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'); 

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

$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 ')'.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.