Load different files with .load ()

1

What I want to achieve is by js - jquery and / or ajax generate a switch similar to PHP using buttons in <nav .

That is:

     $(document).ready(function(){
        $(".click").click(function(){
        $("#box").load("/file.php");
     });
    });
<div id="box">

</div>
<a href="#" class="click">Cargar</a>

This would load file.php into element with id #box when clicking on item with class .click

What I want to achieve:

<a href="#" onclick="loadFiles()" id="1"></a>
<a href="#" onclick="loadFiles()" id="2"></a>
<a href="#" onclick="loadFiles()" id="3"></a>
<div id="contenido-dinamico"></div>

When onclick="loadFiles" runs, load X file according to id="file(numero)" But with condition that when loading 1, 2 and 3 are not displayed.

Is this possible?

    
asked by Juan 03.02.2018 в 02:28
source

3 answers

1

You could pass the "id" as the parameter of the loadFiles () javascript function that you defined in the description code of your question.

To ensure that the dynamic div shows only the information you want, you need to use .empty () to "clean up" the previous div content. strong> and place the new information that will show the div .

Finally you pass the "id" by post or get to the file you want to upload within div (in your case "file.php" strong>) so that when you upload the file selections according to the "id" the information that you will display in your dynamic div .

Here is the code that covers all the previous explanation:

<div id="box"></div> 

<a href="#" name="dinamico" id="1">menu 1</a> 
<a href="#" name="dinamico" id="2">menu 2</a> 
<a href="#" name="dinamico" id="3">menu 3</a> 

<script> 
$(document).ready(function(){ 
$('a[name="dinamico"]').click(function(){ 
id=$(this).prop('id'); 
$("#box").empty(); 
$("#box").load("/file.php", {id:id}); 
}); 
}); 
</script>

For file.php

<?php
$id=$_POST['id'];

if($id=="1"){
// Aquí va información 1 para div dinámico
}
elseif($id=="2"){
// Aquí va información 2 para div dinámico que es distinta a 1
}
else{
// Aquí va información 3 para div dinámico distinta a 1 y 2
}

?>
    
answered by 03.02.2018 / 18:45
source
0

With jquery you could propose the following:

 $(document).ready(function(){
    $(".click").click(function(){
        var id = $(this).data("id")
        $("#box").load("/"+id+".php");
    });
});

The html:

<a href="#" class="click" id="1">Archivo 1</a>
<a href="#" class="click" id="2">Archivo 2</a>
<a href="#" class="click" id="3">Archivo 3</a>
<div id="box">
</div>

If you want to use a function, you can do it like this:

function loadfile(el){
     var id = $(el).data("id")
     $("#box").load("/"+id+".php");
 }

The html:

<a href="#" onclick="loadfile(this)" id="1">Archivo 1</a>
<a href="#" onclick="loadfile(this)" id="2">Archivo 2</a>
<a href="#" onclick="loadfile(this)" id="3">Archivo 3</a>
<div id="box">
</div>

I hope it works for you.

    
answered by 03.02.2018 в 02:57
0

Yes, this is possible, you can call the function loadFile with the parameter id so it looks something like this:

<a href="#" onclick="loadFiles(1)" id="1"></a>

So that when you click, only the file is loaded to the id you want:

function loadFiles(id){
    $("#box").load("/file.php?id="+id);
}

Then in the file file.php you must show the information you want according to the value of the variable id

This is a way to do it to show the file file.php in the element div box

    
answered by 03.02.2018 в 03:05