Jump to content

Calling a jQuery function from PHP, NOT the other way around.


gergy008

Recommended Posts

When ever I try to google this I always get it the other way round "Calling PHP from jQuery" which is now what I need.

 

Basically I want, When the script starts, For PHP to check if an array is empty and if it's not call a jQuery function (This would be the jQuery UI Dialog box).

 

If you can help me here or even post a link to a resource then I will kiss ya. No homo.

 

Thanks in advance!

  • 2 weeks later...

I work in a PHP MVC environment and I do a ton of javascript.

 

The easiest path I've found is having a <body onload="$onload"> on my view files. Basically, when I a specific script is run, if there is a defined onload javascript in my php file, the view file will render the onload tag to the body.

 

My view works like so.

<body <?php if(!empty($onLoad)) { echo 'onLoad="'.$onLoad.'"'; }?>>

 

Then the controller would look like this.

function add()
{
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		redirect('record/add');
	}
	else
	{
		//set onLoad
		$data['onLoad'] = 'PO.add();'; // this is the javascript method I want to fire when the doc is ready

		//load the view
		$data['main_content'] = "record/add";
		$this->load->view('includes/template', $data);
	}
}

 

Might be overkill for what you are looking to accomplish... but its a different approach.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.