1

I want to call the server function from client side via AJAX.

index.php:

<?php 
?>
<html>
<head>
...
 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
...
        console.log(position); // there IS a value! good!
        console.log(result);   // there IS a value! good!
  jQuery.ajax({
        type: "POST",
        url: 'crud.php',
        data: {functionname: 'insertLocation', arguments: [position, result]}, 
         success:function(data) {
        alert(data); 
         }
    });

crud.php:

<?php 
    $position = $_POST["position"]; //NOTHING!
    $result = $_POST["result"];     //NOTHING!
    include ("insert.php");
    switch($_POST["functionname"]){ 
        case 'insertLocation': 
            insertLocation($position,$result);
            break;      
    }   
 ?>

insert.php

<?php 
function insertLocation($position,$result){
...
}
?>

I am losing the value when passing it to the server side. I am able to log the value from JS , but then when I am logging in php there is null (or empty string?). Also query to Database works but no value is inserted.

I am beginner with web programming so I apologise in advance for bad smells, etc.

1
  • I think its because you're sending variables 'position' and 'result' in the array 'arguments'. am I wrong? Commented Sep 24, 2015 at 17:56

2 Answers 2

2

Yes, $_POST has your variables, but they are located in the array $_POST['arguments'] :

$_POST['arguments'][0] == position
$_POST['arguments'][1] == result

If you want to be able to do $result = $_POST["result"] you must change the params in your AJAX request to

...
data: {functionname: 'insertLocation', position: position, result: result}, 
...
Sign up to request clarification or add additional context in comments.

Comments

0
data: {
    functionname: 'insertLocation', 
    position: position,
    result: result
}

1 Comment

In general, good answers on SO explain the code they use. What would this code do?

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.