Return a JavaScript variable and PHP content - AJAX

0

I want a script in ajax that on the page I return the content to write in a div and a variable to use in the link of tag "A".

<a href=" escrever a variável aqui Y ">volta</a>
<br>
<div> escrever a conteudo aqui x </div>

In php I put very basic

<?php
    $abrir = $_GET['dir'];

    //Y é a variavel que quero que escreva no link da tag A atualizando ele
    $Y =  $abrir . 'link_pasta/';

    //Conteudo que quero que escreva na div
    $X = '<p>conteúdo</p>';
?>

The script has to be triggered by sending variables, for example: <a href=index.php?dir=/pasta/">

    
asked by anonymous 20.08.2018 / 00:09

1 answer

1

Well, there are n solutions. Here's an example (requires jQuery):

html file:

<a href="#" id="lin">voltar</a>
<br>
<div id="divcont"></div>


<br><br>
<button id="teste">get</button>

<script  type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script><script>$("#teste").click(
		function(){
			$.get( "meuphp.php?dir=exemplo", function( data ) {
			  var obj = JSON.parse(data);
			  $("#lin").attr("href",obj.link);
			  $("#divcont").html(obj.div);
			});
		}
	);
	
</script>

php file:

<?php
$abrir = $_GET['dir'];
$aux = array();
$aux['link'] =  $abrir . 'link_pasta/';
$aux['div'] = '<p>conteúdo</p>';
echo json_encode($aux);
?>
    
20.08.2018 / 13:24