0

I am trying to send a JS variable to a PHP file but it does not seem to be working. In the console its showing an error as an show.php file. I am not able to understand What I am doing wrong.

function details(id) {
  var id = id;
  //   alert(id);
  $.ajax({
    type: 'POST',
    url: 'show.php',
    data: id,
    success: function(data) {
      alert("hi");
    }
  });
}
<button onclick="details(<?php echo $id ; ?>)" class="btn btn-rounded btn-primary">Details</button> 

show.php:

<?php 
  if (isset($_POST['id']))
  {
    $uid = $_POST['id'];
    echo json_encode($uid);
  }
?>
2
  • its showing no error in console but just display show.php under console list @RoryMcCrossan Commented Feb 25, 2020 at 11:18
  • what type of error you are getting in console, can you show? Commented Feb 25, 2020 at 11:28

2 Answers 2

2

Write data in json object see code below

function details(id)
    {
        var id = id;
     //   alert(id);
        $.ajax({
            type: 'POST',
            url: 'show.php',
            data:  {id:id},
            success: function(data)
            {
             alert("hi");
            }
        });
    } 
Sign up to request clarification or add additional context in comments.

1 Comment

Remove the quotes from 'id'
1

Check your network tab and check the sending parameters list . You have to mention datatype json

Try this

function details(id)
    {
        var id = id;
     //   alert(id);
        $.ajax({
            type: 'POST',
            url: 'show.php',
            dataType: 'json',
            data:  {'id':id},
            success: function(data)
            {
             alert("hi");
            }
        });
    } 

In your show.php

<?php 
  if (isset($_POST['id']))
  {
    $uid = $_POST['id'];
    echo json_encode($uid);
  }
?>

7 Comments

what error you are getting ? kindly check your show.php path . is it correct?
Ok I got it the error is in directory location. both the files are in subfolders. but the url fetches from the mainfolder. @WEX
yes I checked the code is fine just need to make sure file show.php file path is correct,
its showing error : Undefined variable: uid in C:\xampp\htdocs\ecgdash2\show.php on line 15
I updated my answer kindly have a look I posted the code for show.php i tried at my localhost its working fine for me. Let me know if you are getting still error or show me your show.php file code
|

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.