How to insert page . php inside an HTML element with JS

Asked

Viewed 1,264 times

1

Hello, I’m calling a page .php with the method load, his problem is that when I give include into something that should stay this way include './dir/arquivo.php'; after the load loads the page include is obliged to stay this way to work ../dir/arquivo.php, in addition to some elements not working.

But when I only use the include direct, no JS to call, works perfectly, I would like a way to make the JS(or even PHP) execute include 'arquivo.php'(or any form that does not remain bugado) when the button is pressed.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script type="text/javascript">   
$(document).ready(function(){
    $("#button").click(function(){
        $("#div").load('arquivo.php');
    });
});
</script>
<input type="button" id="button">
<div id="div"></div>

Example of code that does not work:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script type="text/javascript">   
$(function() 
{
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

var upload = $('form').ajaxForm(
{
    beforeSend: function() 
    {
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        //aumenta barra de progresso
    },
    complete: function(xhr) 
    {
    }
});
});
</script>

<form enctype="multipart/form-data" method="post" id="formSendTorrent"    name="formSendTorrent">
    <input type="file" multiple name="inputfileSendTorrent[]"       id="inputfileSendTorrent"> 
    <input type="submit" name="submitSendTorrent" value="Enviar">
</form>
<?php 
if(isset($_POST['submitSendTorrent']))
{      
header('Location: index.php');
} 
?>
  • What content do you have in this file that you want to give include ? is something too extensive or not enough? Because you could create a file. PHP that returns a JSON and makes a request in AJAX and assembles the content in your HTML file

  • In this file is JS, HTML and PHP, an entire page that I call inside a DIV. I changed the question.

  • Theoretically this right. "In addition to some elements not working" it arrives to load the file then ? or load but without all functions ?

  • For example, I have a code where when you upload a file JS takes the percentage and then it should continue to run PHP, this code works perfectly, but when I use the load it does not run the PHP below.

  • 1

    Could you describe a little more about the problem?

  • @Matheus The last code I put in the question, when I use include 'arquivo.php' it loads JS(loading upload bar), HTML and PHP(leading to the home page), but when I load this same arquivo.php with the method .load of JS it can only run JS and HTML, PHP ends up not running.

  • @Lucascarezia, are you running this HTML+JS on a Webserver? If not, it may be the reason you don’t run php on . load...

  • I went through the same problem and solved including the file that should be included in php direct in the index. So all the elements loaded by . load() will work. I found the answer https://stackoverflow.com/questions/30092127/jquery-load-and-php-include

Show 3 more comments

1 answer

1

It is not possible to include a php file inside the javascript, because it works on the user’s local machine, while php works on the server, which is interpreted and sent to the browser in html format.

One solution to your problem is to use ajax. ajax can send information to php running on the server without reloading the screen, making it possible to perform functions that would not be possible locally.

Take a look at this article explaining the basics of it: https://www.w3schools.com/xml/ajax_intro.asp

AJAX is a Veloper’s Dream, because you can:

• Update a web page without Reloading the page Request data from a server • after the page has Loaded Receive data from a server • after the page has Loaded Send data to a server - in the background

Ajax is the developers' dream because you can: Refresh a page without reloading it with server data. Receive data after page loading. Send data in the background after page loading.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.