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?

4
  • 1
    where you are calling the function updateJbdesc()??? putting it as action doesnt call it automatically.. Commented Mar 12, 2015 at 12:54
  • 1
    You can't call a php function through ajax directly. Commented Mar 12, 2015 at 12:54
  • i think u are using wordpress Ajax Commented Mar 12, 2015 at 12:55
  • You don't really pass arguments. You pass data via POST(in your case). Simply retrieve the POST value in the function you're calling via the AJAX. Commented Mar 12, 2015 at 12:57

1 Answer 1

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 ); });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.