Jump to content
Fórum Script Brasil
  • 0

Retornar valores com Ajax + PHP


buthy

Question

Olá pessoal!

Queria saber se alguém poderia me ajudar na seguinte questão:

Possuo um <select> que lista categorias, no evento onChange dele eu pegaria o valor que está no value e passaria pelo método GET para um arquivo .php. Até aí tudo bem...

No meu arquivo .php eu faria um select no banco de dados filtrando os dados pelo id da categoria.

Aí que vem a dúvida: como eu retornaria os valores encontrados pelo select?

Além disso, possuo outras dúvidas:

- Como eu deixaria dados sendo exibidos por padrão, caso nenhuma categoria tenha sido selecionada ainda?

- Teria como no evento onchange alterar a url para arquivo.php?id=value? Isso para que quando alguém compartilhasse a url o visitante caísse na categoria selecionada no id da url...

- Como eu faria para exibir um "carregando"?

Segue o meu código:

<script type="text/javascript">
        $(function () {
                $("#categorias").selectbox({
                        onChange: function (val, inst) {
                                $.ajax({
                                        type: "GET",
                                        data: {id_categoria: val},
                                        url: "portfolio_ajax.php",
                                        success: function (data) {
                                                $("ul#lista").html(data);
                                        }
                                });
                        },
                        effect: "slide"
                });
        });
        $(function() {
        /* initiate plugin */
        $("div.holder").jPages({
                containerID: "lista",
                perPage      : 2,
                first       : "PRIMEIRA",
                previous    : "ANTERIOR",
                next        : "PRÓXIMA",
                last        : "ÚLTIMA"
        });
        });
</script>

<section id="portfolio">

                                <div class="fleft _30 mbottom_30">
                                        <select name="categorias" id="categorias" tabindex="1">
                                                <option value="0">-- Selecione uma categoria --</option>
                                                <?php
                                                
                                                $sql = mysql_query("SELECT * FROM portfolio_categoria ORDER BY titulo_categoria ASC");
                                                while ($cat = mysql_fetch_array($sql)){

                                                ?>

                                                        <option value="<?php echo $cat['id_categoria']; ?>"><?php echo $cat['titulo_categoria']; ?></option>
                                                
                                                <?php

                                                }

                                                ?>
                                        </select>
                                </div>

                                <div class="paginacao fright right _60 mbottom_40">
                                        <div class="holder"></div>
                                </div>

                                <div class="clear"></div>

                                <ul id="lista">

                                        <?php

                                        $cont = 1;
                                        $sql = mysql_query("SELECT * FROM portfolio ORDER BY id_portfolio DESC");
                                        if (mysql_num_rows($sql) == 0){
                                                echo '<p>Até o momento nenhum trabalho foi cadastrado em nosso portfólio.</p>';
                                        }else{
                                                while ($row = mysql_fetch_array($sql)){
                                                        $sql_categoria = mysql_query("SELECT * FROM portfolio_categoria WHERE id_categoria = ".$row['id_categoria']." LIMIT 1");
                                                        $categoria = mysql_fetch_array($sql_categoria);
                                        ?>
                                                                
                                                        <li class="<?php if ($cont % 2 == 0){ echo 'fright'; }else{ echo 'fleft'; } ?>">

                                                                <a href="index.php?pag=detalhes-portfolio&id=<?php echo $row['id_portfolio']; ?>">
                                                                        
                                                                        <div class="p_5">

                                                                                <figure class="img fleft">
                                                                                        
                                                                                        <div class="detalhe"></div>

                                                                                        <div class="mais"></div>

                                                                                        <img src="uploads/portfolio/sacola.png" alt="" />

                                                                                </figure>

                                                                                <div class="categoria">
                                                                                        <?php echo $categoria['titulo_categoria']; ?>
                                                                                </div>

                                                                                <div class="dados fright">

                                                                                        <h2><?php echo $row['titulo_portfolio']; ?></h2>

                                                                                        <article class="texto mtop_5">

                                                                                                <?php echo $row['descricao_portfolio']; ?>

                                                                                        </article>

                                                                                </div>

                                                                        </div>

                                                                </a>

                                                        </li>

                                        <?php

                                                        $cont++;
                                                }

                                        }

                                        ?>

                                </ul>

                                <div class="clear"></div>

                        </section> <!-- portfolio -->
portfolio_ajax.php
<?php

        include("adm/db/conecta.php");

        $id = $_GET['id_categoria'];

        $sql = mysql_query("SELECT * FROM portfolio WHERE id_categoria = ".$id." ORDER BY id_portfolio DESC");
        while ($row = mysql_fetch_array($sql)){
                
        }

?>

Fico no aguardo de uma ajudinha!

Muito obrigado!

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...