1

I am using an ajax call which is as follows

       var ID=$(this).attr('id');
       var input=$("#input_"+ID).val();
       var dataString = {id: ID, value: input}; 
        $("#span_"+ID).html(input);

        if(input.length>0)
        {

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

How can I get the data in my php function edit_ajax() which is inside worker_app I use post but the array come to be empty

Thanks

2
  • array come to be empty what do you mean? when you access that page with the browser? Commented Mar 29, 2012 at 11:14
  • so you don't understand ajax. that data is being sent to php app when you call it in js - why do you think it must be in your php script after you load the page? get youself a book, or read one of all-the-same questions here. Commented Mar 29, 2012 at 11:19

4 Answers 4

4

Add dataType: 'json',

var ID=$(this).attr('id');
       var input=$("#input_"+ID).val();
       var dataString = {id: ID, value: input}; 
        $("#span_"+ID).html(input);

        if(input.length>0)
        {

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

and in worker_app.php get id and value by

  $id=$_POST['id'];
  $value=$_POST['value'];
  edit_ajax($id,$value);

function edit_ajax($id,$value){
      $sql="update from ..........";
}

Is this what u want?

Sign up to request clarification or add additional context in comments.

3 Comments

and what is that edit_ajax function? do $_print($_POST); to see the posted value in worker_app.php and see what it gives
span in $("#span_"+ID).html(span); does not sense anything..what is span? it will show undefined as it is not defined anywhere
my edit_ajax function is out side the class worker_app and i ma calling it with an instance of the class
1

Have you tried setting the dataType in your ajax call, like this:

$.ajax({
  type:"POST",
  url: "/apps/worker_app.php",
  data: dataString,
  cache: false,
  success:function(html) {
  },
  dataType:"json"
});

Also, you could use a tool like firebug to see so the data is passed correctly in your ajax request.

Here is an example:

file1.php:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javscript"></script>
<script type="text/javascript">
$(document).ready(function() {
    // catch submit event for your form
    $('#form1').submit(function() {
       // serialize data from form
       var data = $(this).serialize();
       $.ajax({
         type="POST",
         url:"file2.php",
         data:data,
         success:function(resp) {
            // output response
            $('#output').html(resp);
         }
       });
       return false;
    });
]);
</script>
</head>
<body>
  <div id="output"></div>
  <form method="POST" id="form1">
    <input type="text" name="name" />
    <input type="submit" value="send" />
  </form>
</body>
</html>  

file2.php:

 <?php
    if(isset($_POST['name'])) {
      header('Content-type: text/html');
      echo '<p>Response From Server - Your name is: '.$_POST['name'].'</p>';
    }
 ?>

Comments

0

in /apps/worker_app.php get those posted values

$id=$_POST['id'];
$value=$_POST['value'];

and now use the values with your php function

function edit_ajax($id,$value)
{
   echo $id;
   echo $value;
}

echo $return=edit_ajax($id,$value);

now you can get the values in the current page as

success: function(html)
{
     $("#span_"+ID).html(span);
}

Comments

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

on success, the argument function is receiving should be same used with

$("#span_"+ID).html(msg);

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.