-2

I am recieving data form controller in ajax. Then I should assign it to php variable but without using cookies. How to do it? My code:

$.ajax({
        url: '/controller/action',
        type: 'post',
        data: {
            id: id,
        },
        success: function(data){
            if (data) {
                //here I should assign data to php variable
            }
            if (!data) {
                console.log('no data');
            }
        }

     });
2
  • 2
    You can't assign a JS variable to a PHP variable, JS is client side, PHP is server side, you can assign a PHP value to JS by echoing the variable into the JS (e.g. var myvar = '<?= $phpvar; ?>';), but not the other way around Commented Jul 3, 2018 at 11:56
  • 2
    Possible duplicate of How can I pass variables from JavaScript to PHP? Commented Jul 3, 2018 at 12:27

2 Answers 2

1

try using this demo code

<script>
if (data) {
    var a =data;
    <?php $abc = "<script>document.write(a)</script>"?>   
}
if (!data) {
    console.log('no data');
}
</script>
<?php
   echo $abc;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

try it bro and than tell @SamSwift웃
0

you can create a link with JavaScript that includes a GET variable. ones clicked php gets involved

code: http://example.org/index.php?q1=blablabla

Php code:

if(isset($_GET['q1']) {$ff = $_GET['q1']}

Comments

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.