1

I'm trying to get a JS variable to display on a page using PHP. The variable is pulled from the data-id in the script that is inserted on a page:

<script src="/embed-styles.js" data-id="connection" id="view-reviews"></script>

This is the code that is pulling the id:

var scripts = document.getElementById('view-reviews');

if (scripts.getAttribute('src') == '/embed-styles.js') {
    var dataId = scripts.getAttribute('data-id');
    console.log(dataId);

    jQuery.ajax({
       url: '/test.php',
       data:{dataId:dataId},
       success:function(data){
         alert(data);
       }
    })
}

Inside test.php I have:

$dataId=$_GET['dataId'];
echo "dataid:".$dataId."<br>";

I'm trying to include test.php in another php file. When the success alert pops up, it shows the data id but when the data id is echoed on the page, nothing shows. I've tried making it a global variable, alternated between get and post, and tried using json.

I'm very new to this so I apologize if this is an easy fix. I've been looking all over trying to find the answer but haven't found anything that helps. It could be that I'm not using the right words or that this isn't even possible. Any help or direction would be greatly appreciated.

3
  • "When the success alert pops up, it shows the data id but when the data id is echoed on the page, nothing shows." - I don't get what you mean by this. The alert() popup is showing what test.php echoes to the output. If that's working, then what isn't working? Commented Aug 17, 2017 at 18:10
  • I was misunderstanding how everything worked. What I was looking to do was echo out the test.php to the page. I thought that since it was showing in the alert, it would also show on the page. Commented Aug 17, 2017 at 18:27
  • Oh, then in that case what you'd be looking to do in the success function is modify some content in the page. It could be as simple as something like: $('#someElement).html(data); where someElement is the id of an element on the page where you want to insert the returned content. Commented Aug 17, 2017 at 18:29

1 Answer 1

1

Your code is working as you want. You just don't understand it. When you make an Ajax call to test.php, the data passed back to your success function is the data that is "echoed" by test.php

The part you echo in your test.php won't print to your console or your page, if you were hoping to achieve that. The thing you echo will be passed back to the request you made, and that is passed in your success function will is using alert and displaying the data passed to it.

So your program is working!

To see it for yourself, change the line in test.php to

echo "hello world";

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

8 Comments

Thank you for the response! So what I need to do is change the success function into something that will show it on the page, is that correct? I found this but I wasn't sure if it was the correct route because I don't want it to echo into a div, I just want it to be a php variable: stackoverflow.com/questions/34014264/… Is it possible to store the data id as a variable in the php file? If so, I will keep digging. Thanks again for the response!
what is your goal with the variable? Usually with ajax requests, you try to show more content on the page without reloading the page, and that is done by adding elements/div/html to your page in the success function. What are you trying to achieve here?
you can also save it to $_SESSION variable that will persist in your application on all pages. php.net/manual/en/book.session.php
Maybe I'm wrong altogether here then. The end goal is to use that data-id to pass into a database query. I would have users insert the script with the data-id on their site, the ajax would get the data-id, send it to a php variable, then I would use that php variable to know what database to use. Which is why I'd need it to be a variable and not added to an element on the page.
so when you make the request to test.php, you have the data id what you want to use for the query. You can execute all your database related stuff in that request. You don't have to put it all in one test.php file. You can make a different .php file and make an internal redirect to that using the variable from the test.php file. Does that sound something that you want?
|

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.