0

I have to access values from post variable in php class function in jquery. this is my function in php class:

function myFunction(){
  $id = $_POST[$this->id];
  $name=  $_POST[$this->name];
  echo id;
  echo name;
} 

I can get value for id and name I want to access id and name properties in jquery?how can I do this i'm trying like this

$( document ).ready(function() {
  var id =$id;
  var name=$name;
  alert(id);
  alert(name);
});

how can I access these in jquery

2 Answers 2

1

You can only do this on the page load, since PHP only is accessible when the browser makes a request to the server. But you can have something like:

index.php

<?php
  $id = $_POST[$this->id]; // 123
  $name = $_POST[$this->name]; // John
?>
<html>
<head></head>
<script>
  $(document).ready(function() {
    var id = '<?=$id;?>';
    var name = '<?=$name;?>';

    console.log(id); // 123
    console.log(name); // John
  });
</script>
</html>

<?=$id;?> and <?=$name;?> will literally echo out the variables into the JavaScript on page load. If you need this values to be updated from a PHP script, you'll need to use an AJAX call.

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

Comments

1

Jquery and PHP are different type of language.

You need do like below to pass the value

$( document ).ready(function() { 
    var id = '<?php echo $_POST[$this->id]; ?>';
    var name= '<?php echo $_POST[$this->name]; ?>';
    alert(id);
    alert(name);
});

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.