0

I have an edit-in-place (jEditable) dropdown list of categories. One of the options is "New Category".

If a user selects "New Category" and submits the form using Ajax I want PHP to trigger a jQuery function which opens an overlay dialog box and prompts the user for input.

How can I get PHP to trigger the jQuery function?

PHP Code...

function update()
{
    $id = $this->input->post('id');
    $value = $this->input->post('value');

    $data = array('category_id' => $value );
    $this->db->where('id', $id);
    $this->db->update('transactions', $data);

    $category = new Category();
    $category->where(array('id' => $value));
    $category->get();

    if ($value == 0) {
        // Trigger jQuery function
    } else {
        echo $category->name;
    }

}

The jQuery function I want to trigger (It's currently triggered via a button)...

    //===== UI dialog =====//
$( "#new-cat-input" ).dialog({
    autoOpen: false,
    modal: true,
    buttons: {
        Ok: function() {
            $( this ).dialog( "close" );
        }
    }
});

$( "#open-new-cat" ).click(function() {
    $( "#new-cat-input" ).dialog( "open" );
    return false;
}); 
ginsberg
  • 51
  • 1
  • 7
  • You have omitted the operative piece of code - namely the javascript function that makes the ajax call and, most importantly, handles the response. – Beetroot-Beetroot Feb 19 '12 at 01:18

2 Answers2

2

You can't triggure something on the client-side with server-side code like php however jQuery has a success method that executes after an ajax call has been made.

$.ajax({
  url:"someurl.php",
  success: function()
  {
     $('yourSelector').yourfunction("");
  }
});

Additionally you can pass form data to php pages and return html/javascript or jason and either inject it to the page or parse/run the code.
heres some usefull links
AJAX documentation: http://api.jquery.com/jQuery.ajax/
w3cschool example with autocomplete text field: http://www.w3schools.com/php/php_ajax_php.asp

Yamiko
  • 5,303
  • 5
  • 30
  • 52
0

How to call a JavaScript function from PHP?

I recommend reading this questions answer, then return the string (or echo to out)

<script type="text/javascript">$( "#new-cat-input" ).dialog( "open" );</script>

at the appropriate place

Actually, I retract my answer as this will lead to either not knowing when the script is executed or to having to eval this script to execute it.

Stick with sending an ajax-request and handling the response as suggested by others.

maybe you need to distinguish between calls that return entire pages and calls that just return some data or page fragements (which you can append to divs)

Community
  • 1
  • 1
mindandmedia
  • 6,800
  • 1
  • 24
  • 33