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!

Link to comment
Share on other sites

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

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.