0

I am having problem sending/reading ajax variable.

Ajax code is as below:

$.ajax({
    url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
    success: function(data) {
        $("#ClientID").html(data);
    }
});

I have tried to read it in another php file like this:

$InvoiceType = $_REQUEST['it'];
//$InvoiceType = $_POST['it'];
//$InvoiceType = $_GET['it'];

But none of above works. The variable $InvoiceType always stays empty.

What is the problem?

AsgarAli
  • 2,201
  • 1
  • 20
  • 32
gdolenc
  • 301
  • 1
  • 2
  • 17

4 Answers4

0
$.ajax({
      type: "GET",
      data: {it: "1"}, 
      url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
      success: function(data) {
         $("#ClientID").html(data);
      }
});

try this

devpro
  • 16,184
  • 3
  • 27
  • 38
0

The best way is to use POST method like this : $.ajax({ method: "POST", url: ""/wp-content/themes/canvas-child/get-clients-dropdown.php", data: { it: 1, param2: "value2" } })

You can get your value in $_POST['it']

Fabien
  • 548
  • 7
  • 18
0

Please try it with full url of file with get_template_directory_uri().

$.ajax({
    type: 'GET',
    url: '<?php echo get_template_directory_uri(); ?>/get-clients-dropdown.php',
    data: {it: 1},
    success: function(data) {
            $("#ClientID").html(data);
          }
     });
Jalpa
  • 697
  • 3
  • 13
0

You have to use it like this:

$.ajax({
    type: "POST",
    url: "/wp-content/themes/canvas-child/get-clients-dropdown.php?it=1",
    success: function(data) {
        $("#ClientID").html(data);
    }
});

Then in PHP File use this:

$InvoiceType = $_POST['it'];
Atif Tariq
  • 2,650
  • 27
  • 34