Chamando funções em PHP com jQuery

index.html

<html>
<head>
  <link rel="stylesheet" type="text/css" href="estilo.css" />
  <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
  <script type="text/javascript" src="functions.js"></script>
</head>
  <body>

    <label id="carrega" name="carrega">Carregar</label><br />

    <select id="nome" name="nome">
      <option value="#">Selecione</option>
      <option value="Nickollas">Nickollas</option>
      <option value="Carvalho">Carvalho</option>
    </select>

  </body>
</html>

functions.js

$(function() {
  $("#carrega").click(function(){
    var id = $(this).attr("id");
    $.get("functions.php?func=oi", function(dadosRetornados){
    $("label[for=" + id + "]").remove();
    $("#" + id).after("<label for=" + id + ">" + dadosRetornados + "</label>");
    $("label[for=" + id + "]").css("color","red");
    });
  });

  $("#nome").change(function(){
    var id = $(this).attr("id");
    $.get("functions.php?func=nome&nome=" + $(this).val(), function(dadosRetornados){
    $("label[for=" + id + "]").remove();
    $("#" + id).after("<label for=" + id + ">" + dadosRetornados + "</label>");
    $("label[for=" + id + "]").css("color","blue");
    });
  });
});

functions.php

<?php

switch($_REQUEST['func']) {
  case 'oi':
    $output = oi();
    break;
  case 'nome':
    $output = nome();
    break;
}

echo $output;

function oi(){
  echo "Oiiiii";
}

function nome(){
  echo 'Selecionado: ', $_REQUEST['nome'];
}

?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s