PHP in JavaScript not working

I have JavaScript, and I want to run an include script in JavaScript. I tried the following statement:

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

When I run the script, I get no response.

Does anyone know why I have not received any answer and how can I fix it?

I tried replacing the PHP include html iframe. It worked, but it is not the best solution for me.

+8
javascript php
source share
13 answers

No need to use javascript in this case

you can use the answer right where you want

 <div id="outputEl"><?php include "./change/ud1.php";?></div> 
+11
source share

You cannot run php inside a .js file. As far as I understand, you will need to get the contents of the php file from your js file.

It is implemented bare bone.

 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 can be easier and more reliable to use jquery, and in this case (after turning on the jquery library) you can just use something like this

 $(outputEl).load("change/ud1.php"); 
+7
source share
  • php is a server language.
  • JavaScript is a client language.

therefore php can only be executed on your server , and JavaScript is executed in the user's browser.

There is no way to run PHP code 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(); 

Read more about ajax here .

+4
source share

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

  $.get("path/to/yourfile.php", function(data, status){ outputEl.innerHTML = data; }); 
+3
source share

Just use

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

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

+2
source share

This is the wrong approach. You have to take html into the php variable and then use this PHP variable in JavaScript. If you use MVC, then this is pretty easy. I sent a response without using MVC:

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

In javascript

 var html = '<?php echo $html; ?>'; 
+2
source share

You cannot do this directly. If you want to replace the contents this way, you have the following two options:

  • send an AJAX request to this file, and then this file will send all html as a parameter parameter to the success function, and then you can use this data wherever you want.

  • Or you should redirect to this page and want some data to be sent to this file, then you can receive a request for receipt.

      <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 follow 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 need more help.

+2
source share

make single quotes for double quotes and try

+2
source share

Two ways:

At first:

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

Second:

 <script type="text/javascript" src="./change/ud1.php"></script> 
+1
source share

Try

 $("#output1").load("change/ud1.php"); 
+1
source share

The PHP code is displayed on the server, with Javascript that you are on the client, and you cannot display PHP if you are not on the server.

+1
source share

You cannot do this because PHP outputs the code before JavaScript . There are many alternative ways to do this:

if outputEl is the identifier of the 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"; } } } 
+1
source share

this will work too. put this code in your .php, you can put the script in your .php

 <script type="text/javascript"> outputEl.innerHTML = "<?php include './change/ud1.php';?>"; </script> 
-one
source share

All Articles