0

I am calling a php function using ajax call but its not working. I have a table with some rows when I click on any column of a row it becomes editable with its value but when I edit the value and click on another column or enter the changed value is not displayed again. Actually I am doing an ajax call where I change the data of the column in my table but its not calling that php function.

My script is as follows

<script type="text/javascript" >
$(document).ready(function()
{
    $(".edit_tr").click(function()
    {
        var ID=$(this).attr('id');
        $("#span_"+ID).hide();
        $("#input_"+ID).show();
    }).change(function()
    {
        var ID=$(this).attr('id');
        var input=$("#input_"+ID).val();
        var dataString = 'id='+ ID +'&data='+input;
        $("#span_"+ID).html('<img src="load.gif" />'); // Loading image

        if(input.length>0)
        {

            $.ajax({
                type: "POST",
                url: "worker_app::edit_ajax()",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $("#span_"+ID).html(span);
                }
            });
        }
        else
        {
            alert('Enter something.');
        }

    });

    // Edit input box click action
    $(".editbox").mouseup(function()
    {
        return false
    });

    // Outside click action
    $(document).mouseup(function()
    {
        $(".editbox").hide();
        $(".text").show();
    });

});

The HTML table looks like this

<tbody>
  <tr id="{IDWORKERS}" class="edit_tr">
    <td class="edit_td">
      <span id="span_{IDWORKERS}" class="text">{FIRM}</span>
      <input type="text" value="{FIRM}" class="editbox" id="input_{IDWORKERS}" />
    </td> 
  </tr>
</tbody>

And the php function is inside apps folder in a file called wroker_app.php

public function edit_ajax(){
    ///echo "<pre>";
    ///print_r($_POST);
    //echo "</pre>";
   // sql to update the database goes here
    echo 'I am here';
}

Any ideas?

Thanks in advance

2
  • I don't know nothing happens :( I am new to ajax how can I check the ajax call ? Commented Mar 29, 2012 at 9:44
  • alert(html) inside success and tell us the result Commented Mar 29, 2012 at 9:48

3 Answers 3

2

You can not call specific functions using the request alone. You need to tell your script that edit_ajax is supposed to be executed.

So change your url to worker_app.php, listen for the request using a (e.g) get variable like ?[function].

if (isset($_GET['edit_ajax']) && function_exists($_GET['edit_ajax']))
     edit_ajax();
Sign up to request clarification or add additional context in comments.

3 Comments

It seems that you try to call the edit_ajax() by requesting the url /worker_app::edit_ajax.php. This is not possible. You can not call functions using the request url alone.
then what can I do to call a function ?
Call url: '/worker_app.php?edit_ajax'. In your worker_app.php file, listen for the call by the snippet above.
1

You cant call a php function like this. You can only call a php file where you can decide which function should be executed. A simple example:

$.ajax({
 type: "POST",
 url: "/apps/worker_app.php",
 data: dataString,
 cache: false,
 success: function(html) {
   $("#span_"+ID).html(span);
 }
});

And in your apps/worker_app.php:

<?php 

function edit_ajax(){
  echo 'I am here';
}

// You can put some logic before this line to decide which function to call based on the request
edit_ajax();

14 Comments

You can write it before the function i've just copied your code and added the call.
I write edit_ajax(); just before my function and it is giving me a syntax error :(
Paste the error message here, but the place of the call can't be the problem
I set my url in the ajax call like this url: "/apps/worker_app.php", in my apps/worker_app file i have my method edit_ajax()and just before the start of this function I have edit_ajax(); The error it gives is Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\personal\apps\worker_app.php on line 549
Oh than you cant simply call edit_ajax() you have to call it on an instance of the class (or call the static way if it's a static function)
|
0

url: "worker_app::edit_ajax.php"
Where is this url referrs to.

check this reference jquery .ajax

If you want to call function use try to post any specific post field to that file.
After use below in that file

 if($_POST['field']!="") { 
      requredfunction () {
           // your code run here
       } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.