-1

## This My test.php file ##

 //here I am getting the variable value

<?PHP
 $name = $_POST('variable');
 echo $name;
?>

## This my script ##

 //avariable value passing to test .php file

<script type ="text/javascript">
  var name = "jani";
  $.ajax({
          type: 'POST',
          url: 'test/test.php',
          data: {'variable': name},
          });
</script>

###** I am getting this error **###

 Fatal error: Function name must be a string in 
                 D:\xampp\htdocs\test\test.php on line 2
3
  • Check console while you are making ajax call, are there any values passing? Commented Aug 11, 2017 at 5:28
  • no values are passing in console Commented Aug 11, 2017 at 5:31
  • 5
    Possible duplicate of jQuery send string as POST parameters Commented Aug 11, 2017 at 5:33

2 Answers 2

1

Change your test.php code use square bracket not round for super global variable as below,

<?php
if (isset($_POST['variable'])) {
    $name = $_POST['variable'];
    echo $name;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

$name = $_POST['variable']; echo $name; but i am getting undefined variable error
@Jani I have updated my answer. check it
0

Try this it will definitely work: My PHP File:

<script>
var name = "name";
var url = "test/test.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: {'name': name},
           success: function (data) {
            console.log(date);
           }
});
</script>

Where ajax request recieves:

//here I am getting the variable value

<?PHP
 $name = $_POST['name'];
 echo $name;
?>

After Your latest comment I have added js fiddle link before running it open console and POST besides headers section you can see data has been posted. Js Fiddle Demo Link

5 Comments

Undefined index: name in D:\xampp\htdocs\test\test.php on line 5 getting this error
check console this time while sending ajax request is it sending parameters?
Undefined index: name in D:\xampp\htdocs\test\test.php
have you checked Console? What parameters have you sending?
I have updated the answer and added the link, you can see it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.