Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include in JavaScript is not working

Tags:

javascript

php

I've JavaScript and I want to run a include script in the JavaScript . I tried the following statement:

outputEl.innerHTML = '<?php include "./change/ud1.php";?>';

When I run the script I get no response.

Does someone know why I didn't get any response and how I can fix it?

I tried to replace the PHP include with an html iframe. That worked but that is not the best solution for me.

like image 824
John Avatar asked Jul 02 '17 07:07

John


People also ask

Why PHP include is not working?

You need to try the path of functions. php within the system and not its url. Do you have console access? If so just find out what directory is the file in and include it using the full path.

Can I include PHP in Javascript?

You can't include server side PHP in your client side javascript, you will have to port it over to javascript. If you wish, you can use php. js, which ports all PHP functions over to javascript.

What does include () do in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Can I include PHP in HTML?

As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).


12 Answers

No Need to use javascript in this case

you can use response directly where you want

<div id="outputEl"><?php include "./change/ud1.php";?></div>
like image 105
Sharad Kale Avatar answered Oct 10 '22 23:10

Sharad Kale


You can't run php inside a .js file. As I understand it, you're going to need to fetch the content of the php file from your js file.

Here's a bare-bones implementation.

var xhr = new XMLHttpRequest();

xhr.onload = function () {
    outputEl.innerHTML = this.response;
};

xhr.open('GET', './change/ud1.php', true); // you may need to correct the url to be relative to the js file
xhr.send();

It might be easier and more robust to use jquery, in which case (once you've included the jquery library) you can just use something like this

$(outputEl).load("change/ud1.php");
like image 45
Chris Lear Avatar answered Oct 10 '22 23:10

Chris Lear


  • php is a server-side language.
  • JavaScript is client side language.

so php can only execute on your server and JavaScript executes on user's browser.

there is no way in which php code going to run in JavaScript (browser). so you need to use ajax like this.

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        // server sent data successfully in this.responseText variable
        document.getElementById("output1").innerHTML = this.responseText;
    }
};

//Request data from a server
xhttp.open("GET", "path/to/php/include-script.php", true);
xhttp.send();

Learn more about ajax here.

like image 40
Maulik Avatar answered Oct 10 '22 21:10

Maulik


You can also use jquery to load the content of the php file in your specified html element.

 $.get("path/to/yourfile.php", function(data, status){  
    outputEl.innerHTML = data;
});
like image 45
AJM Avatar answered Oct 10 '22 21:10

AJM


Simply Use

<script type="text/javascript" src="filename.php"></script>

Also pass this header("Content-type: text/javascript"); on top of the php file.

like image 35
Aman Agarwal Avatar answered Oct 10 '22 22:10

Aman Agarwal


This is not right approach. You should take html into a php variable and then use that PHP variable into JavaScript. If you are using MVC then its quite easy. I posted answer without using MVC :

$html = file_get_contents('path/to/YOUR/FILE.php');

In Javascript

var html = '<?php echo $html; ?>';
like image 40
Von Avatar answered Oct 10 '22 23:10

Von


make single quotes to double quotes and try

like image 41
nobody Avatar answered Oct 10 '22 22:10

nobody


You can't do this directly. If you want to replace the contents in this manner you have following two options

  1. send AJAX request to that file and then that file will send the entire html as the data parameter of success function and then you can use that data wherever you want.

  2. Or else you have to redirect to that page and you want some data to be sent to that file, then you can have as a get request.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    
    <script>    
        $.ajax({
           type: "POST",
           url: "<?php include "./change/ud1.php"; ?>",  //ensure URL is correct
           data: { }, //send some data if you want to that file or else remove this line
           dataType: "html",
           success: function(data) {
               if($.trim(data)) {
                    $('#outputEl').html(data);
               } else {
                    alert("Something went wrong!!");
               }
           }, error: function() {
               alert("ERROR!");
           }
       });
    </script>

And you can perform step 2 as

&lt;script&gt;
    window.location.href = "./change/ud1.php";  //And pass any parameters to that file if you wish as a GET request
&lt;/script&gt;

Let me know if you require any further help

like image 25
Amaan Iqbal Avatar answered Oct 10 '22 22:10

Amaan Iqbal


Two ways :

First:

 <script>
      document.write('<?php echo include_once "./change/ud1.php";?>');
   </script>

Second:

<script type="text/javascript" src="./change/ud1.php"></script>
like image 30
Minar Mnr Avatar answered Oct 10 '22 22:10

Minar Mnr


Try this

$("#output1").load("change/ud1.php");
like image 37
Jawahar Sharma Avatar answered Oct 10 '22 22:10

Jawahar Sharma


The PHP code is rendered on the server, with Javascript you are on the client and you can not render PHP if you are not on the server.

like image 22
Oscar Pérez Avatar answered Oct 10 '22 23:10

Oscar Pérez


You can't do this because PHP code render before JavaScript. There are many alternative ways to do this:

if outputEl is a ID of a div element:

1- Without jQuery/JavaScript

<div id="outputEl"><?php include "./change/ud1.php";?></div>

2- Use jQuery load function

$("#outputEl").load('./change/ud1.php');

3- With JavaScript function

window.onload = function() {
  if (XMLHttpRequest)
    var x = new XMLHttpRequest();
  else
    var x = new ActiveXObject("Microsoft.XMLHTTP");
  x.open("GET", "./change/ud1.php", true);
  x.send();
  x.onreadystatechange = function() {
    if (x.readyState == 4) {
      if (x.status == 200)
        outputEl.innerHTML = x.responseText;
      else
        outputEl.innerHTML = "Error loading document";
    }
  }
}
like image 43
Govind Samrow Avatar answered Oct 10 '22 21:10

Govind Samrow