0

I am getting the values from HTML form through JavaScript and I am trying to call the PHP functions which takes arguments through AJAX. But I am not able to get it done. How can I do this?

PHP code:

<?php
function updateJbdesc($jobdesc,$loginid) 
{
    // some code
}

?>

Ajax Code:

$.ajax({ url: 'allfunctions.php',
         data: {action: 'updateJbdesc'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

How can I pass arguments to PHP function through AJAX?

Dharman
  • 30,962
  • 25
  • 85
  • 135
CJAY
  • 6,989
  • 18
  • 64
  • 106

1 Answers1

0

You need to add some code to get the $_POST values and use these to call the function.

$val0 = $_POST[ 'val0' ];
$val1 = $_POST[ 'val1' ];
$functionId = $_POST[  'functionid' ];

$result = -1;
if( $functionId == 0 ) $result = somefunction( $val0, $val1 );
else $result = someotherfunction( $val0, $val1 );

# echo this to return result to ajax
echo $result; 

You can pass a value via ajax to tell PHP which function you want:

$.ajax({ url: "myurl.php",
    data: { val0: "val0", val1: "val1", functionid: "identifier" }
}).done( function( result ) { alert( result ); });
ethrbunny
  • 10,379
  • 9
  • 69
  • 131