Jump to content

Equal javascript variable to php echo?


ava_barbilla

Recommended Posts

Well, my test.php file has this code:

 

 

<?phpecho(rand(10,100));?>

 

Once javascript runs on my localhost and performs the Ajax request it returns this code in between the div tag with id "demo":

 

 

<!--?phpecho(rand(10,100));?-->

 

Basically, when I right click on the page and click "Inspect Element" this appears (this is my Source Code):

 

 

<div class="entry-content"><p><br><br><br> <script type="text/javascript" src="http://localhost:8888/.../javascript/test.js"></script><br><br></p><div id="demo"><!--?phpecho(rand(10,100));?--></div><p><br></p></div>

 

I put in "..." in the script path to shorten the URL.

Link to comment
Share on other sites

Javascript is working exactly as it should.

 

It sounds like PHP is not being parsed. Open test.php right in your browser and press Control+U to see actual the source code.

  • Like 1
Link to comment
Share on other sites

This is a problem. PHP isn't being parsed. Your server is just sending the code straight to the browser without running it.

 

What the browser should get when you open that page is a number, not PHP code.

  • Like 1
Link to comment
Share on other sites

No, Wordpress doesn't have anything to do with it unless you're actually loading Wordpress code inside test.php

 

What is the complete URL in the browser 's address bar when you open test.php?

Link to comment
Share on other sites

Unfortunately nothing's showing up in your posts.

 

The file does have a .php extension so I don't know why the PHP isn't being parsed. Is this the URL you're using in your Javascript code?

http://localhost:8888/test-site/wp-content/my-php-files/test.php
  • Like 1
Link to comment
Share on other sites

I use localhost if not any page would not be loaded. I was not sure which url you needed so I provided both. My Ports are as follows:

 

Apache Port: 8888

Nginx Port: 7888

MySQL Port: 8889

 

My PHP version is 5.6.2 and my Webserver is Apache. Hope this helps, as perhaps this problem ay have to do with the ports. What would you suggest?

Link to comment
Share on other sites

The Javascript is working as it is supposed to. The server just seems to not be executing the PHP code. I can't think of any reason why it's not working.

 

Try making a new PHP file test2.php in the same directory and open it in the browser. Make it simple:

<?php echo 'Hello, World!'; ?>
  • Like 1
Link to comment
Share on other sites

Yes, I tried that already. I tried renaming the file and other tricks as well. How can I upload pictures here? Maybe I can send some screenshots so that you can see how my localhost looks like and some other screenshots which may be of use furthermore. Moreover, when I click on "Inspect Element", go to the network tab and click on refresh I can see all the movement that happens. One of them is the the Ajax Request with the type "POST" and Status 200 (OK). I have been doing some research but sadly I found nothing useful regarding this subject. Does it have to do with the fact that I use Caching Plugins? I use W3 Total Cache in order to cache browser data, minify scripts, etc. I also use an Autoptimize tool that optimizes HTML, CSS, and Javascript but I doubt that this last tool has anything do with as all other scripts work, and in this case scenario the script seems to be working fine. To be honest this is a mistery to me.

Link to comment
Share on other sites

I added some html to the php file and everything loads except the php file (php appeared as a comment). Then, I came up with the idea to compare the php file I created with the other php files on my local server. I identified a little difference. The other php files do not close the php, i.e. <?php echo 'Hello World'; without putting "?>" at the end, and now it works. My php file looks like this:

 

 

<?phpecho 'Hello World';

 

I also simplified my script, making my structure look like follows:

 

 

<div id="demo"><div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><script>$(document).ready( function() {$('#demo').load('http://localhost:8888/test-site/wp-content/my-php-files/test3.php');});</script>

 

I used JQuery .load() property as it is simpler than the normal script (same principle). The only thing I have to do now is to apply a setIntervl to load the function after a specific amount of time and I am finished. Thanks for the support Ingolme. I learned a lot today with your help.

Link to comment
Share on other sites

While the closing ?> tag is not required for PHP to function, having it there should not actually change the functionality of the program either.

 

I am completely confused as to what your server is doing.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...