Load page via load with problem

Asked

Viewed 376 times

2

I want to load a page inside a div via load passing a variable that will define the contents of the page to load.

My code:

index php.

<!DOCTYPE html>
<html>
<head>
<script
  type="text/javascript"
  src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>
<script>
$(document).ready(function(){
    $('.carrega').click(function(){
            $('#aqui').load('pagina.php?variavel='+$('.carrega').val());
        });
    });
</script>
<?php
$arr = array('azul', 'branco', 'cinza');

for ($i = 0; $i < count($arr); $i++) {
    $temp=$arr[$i];
    echo "<button class='carrega' value='$temp'>$temp</button>";
}
?>
<div id="aqui"></div>
</html>

php page.

<?php
$variavel=$_GET['variavel'];
echo $variavel;

?>

The page loads correctly, but only the first variable is passed through the URL. I don’t know what I’m doing wrong, could someone give me a hand?

1 answer

2


Yes.

When you put

$('.carrega').val() in the parameter it will take the first element that contains this class. In your case it will always be blue.

Place $(this).val(), that it will catch what you are clicking.

  • 1

    Thank you very much!!!

Browser other questions tagged

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