0

I am trying to pass a single variable from javascript to PHP using jQuery AJAX.

Javascript:

$(document).ready(function() {
    $('.ok').on('click', function(e){

        var value = 5;

        $.ajax({
            url: 'tabulka.php',
            type: "POST",
            data: {x : value},
            success: function(){
                alert(value);
            }
        });
    });
});

PHP:

if($_SERVER['REQUEST_METHOD']){
    $value = $_POST['x'];
    echo $value;
}

I get the alert from ajax with the expected value but in PHP I get this error: Notice: Undefined index: value in C:\xampp\htdocs\test\tabulka.php on line 71 When I uncomment the dataType: 'json' I don't even get the alert from AJAX.

2
  • Alert print right value. Commented Dec 12, 2016 at 18:42
  • The error is coming from some other part of your PHP code that you haven't shared with us, where ['value'] is being used as an array index. Commented Dec 27, 2016 at 4:21

3 Answers 3

1

Instead of this

data: ({value : value}),

Try using this,

 data: "value="+value,

And you are not at all passing "OK" but you are receiving it. If you want to pass OK also as a parameter try this.

data: "value="+value+"&OK=Somevalue",

That should work.

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

6 Comments

@JanMaděra : Check my updated answer. You might have tried data: "value="+value, but then you might not have got the error but a blank response. But when you tried this data: ({value : value}), you got an error. Am I right ?
I don't want to pass OK just the value. I got error in both cases.
If you don't want to pass OK just remove this && isset($_POST['OK'])
That just check if i click OK button. That have nothing to do with the ajax I think. I make some edit to code. But that didn't help.
Yeah, that has nothing to do with the AJAX but think, you are using it in the if condition with AND. If you don't pass any values for "OK" it will not go inside the if. Do you have any .htaccess file used ?
|
1

Your JavaScript

data: {'value' : value}

notice the single quote and no parenthesis

Your PHP:

If(isset($_POST['value'])){
   $val = $_POST['value'];
}

Your JavaScript was formatted incorrectly. data is an object, or an associative array in other words.

Your PHP code has unnecessary string comparison. Whenever a post is made to a PHP script, you will access them using $_POST array.

For instance, if you posted this via AJAX

data:{ 'name' : 'jake', 'age' : 1024}

You would access them in PHP by

  $name = $_POST['name']
    $age = $_POST['age']

Also, it is wise to use PHP isset to verify that a post variable exists before trying to access them. That way you will not have an exception if expected post data is not.

1 Comment

Didn't change anything for me. The problem must by somewhere else. Ajax pass the data right but the SERVER don't recognize the 'x' index.
0

check following line

var value = $(this).find('td:first').html();

set some hard coded value for 'value' variable like below

var value='some value';

if above works then check the value you assigned using $(this).find('td:first').html()

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.