How to pass the `id` of a post via``by ultilizing AJAX?

Asked

Viewed 1,692 times

2

I would like to update via ajax, but I’m having a hard time sending the id of postage via post or get for the external form: php/editarDespesa.php. The form is not displaying the dados, but without using ajax the data appears in the form.

Table ec_despesas have the following fields:

`id, aba, status, nome, categoria, conta, valor, data`

index php.:

<div id="dadosDespesas">
                <?php
                    $despesas = dbRead('despesas', "WHERE aba = 'Despesas'", 'id, nome, categoria, valor, data');

                    if(!$despesas)
                        echo '<h3>Nenhuma Despesa Para Pagar!</h3>';
                    else
                        foreach ($despesas as $visual):
                            $valorNormal = $visual['valor'];
                            $valorDecimal = decimalBr($valorNormal);

                            echo '<li>'
                                    .'<p class="b">'.$visual['nome'].'</p>'
                                    .'<p>'.$visual['categoria'].'</p>'
                                    .'<p>'.$valorDecimal.'</p>'
                                    .'<p>'.$visual['data'].'</p>'
                                .'</li>';

                ?>
                <a class="editarDespesa" href="php/editarDespesas.php?id=<?php echo $visual['id']; ?>">Editar</a>

                <?php endforeach; ?>
            </div>

js control.

    // ABRE A CAIXA DE FORMULARIO PARA EDITAR
$(".editarDespesa").click(function() {
$("#formularioDespesas[class]") .attr("class","formVisivel");

$.post("php/editarDespesa.php?id=<?php echo $visual['id']; ?>", function(data){
    $("#formularioDespesas").html(data);
});

    return false;
}); });

editarDespesa.php

<?php
require_once 'config.php';
require_once 'database.php';

if( !isset( $_GET['id'] ) || empty($_GET['id']) )
    header('Location: index.php');
else{

    $id   = dbEscape( strip_tags( trim( $_GET['id'] ) ) );
    $post = dbRead( 'despesas', "WHERE id = '{$id}' LIMIT 1");

        $post = $post[0];
} ?>


<div class="titulo">
                    <span>Editar Despesa</span>
                    <a id="fecharFormularioDespesas" class="icon-close"></a>
                </div>
                <div id="conteudo">
                    <form action="" method="post">  
                        <li><p>Descrição</p><input id="nomeDespesas" name="descricao" placeholder="Nome" type="text" value="<?php echo $post['nome']; ?>"></li>
                        <li><p>Valor</p><input id="valorDespesas" name="valor" placeholder="0.00" type="text" value="<?php echo $post['valor']; ?>"></li>
                        <li id="toggle1"><p>pago</p><input id="statusDespesas" type="checkbox" name="status" value="0">
                            <label for="statusDespesas" data-text-true="Sim" data-text-false="Não"><i></i></label>
                        </li>
                        <li><p>Data</p><input id="dataDespesas" name="data" maxlength="10" placeholder="30/12/2014" type="text" value="<?php echo $post['data']; ?>"></li>
                        <li><p>Categoria</p><select id="categoriaDespesas" name="assunto">
                            <option value="<?php echo $post['categoria']; ?>"><?php echo $post['categoria']; ?></option>
                            <option value="Salário">Salário</option>
                            <option value="Investimentos">Investimento</option>
                            <option value="Contas">Conta</option>
                            <option value="Empréstimo">Empréstimo</option>
                            <option value="Serviços prestados">Serviços prestados</option>
                            <option value="Outros">Outros</option>
                        </select></li>
                        <input id="abaDespesas" type="hidden" name="aba" value="Despesas"/>
                        <input id="contaDespesas" type="hidden" name="conta" value="bb"/>
                    </form>

                    <button id="botao_limparDespesas">
                        <span>LIMPAR</span>
                    </button>
                    <button id="butao_alterarDespesas" type="submit">
                        <span>ALTERAR</span>
                    </button>                   
                    <div id="loardDespesas"></div>
                </div>
  • Where in HTML is the value of the ID you want to send?

  • The value is being taken from the database that has a column with the name "id" and in it the value ex: 33

  • But when you want to do this $.post("php/editarDespesa.php?id=<?php echo $visual['id']; ?>" echo what do you have? if the js file is not processed by PHP then it won’t work. You have to echo that ID to a data-id inside the form. Does this make sense to you? Then each form must have a unique ID certain?

  • echo contains a number, this number should be passed to the php/editarDespesa.php page, like this: "http://localhost/php/editarDespesas.php? id=34" I just need this, only using ajax

  • I understand, your problem is that that line of code is in a file .js and I imagine (pretty sure) that this line is still with ?id=<?php echo $visual['id']; ?> on the client side (when it opens in the browser). So my question is every page should have a unique ID right? or need different Idxs for the same user/page?

  • @Deyvischarles Take a look at the [tour] accept a reply (and vote when you have that privilege) is the best way to thank.

Show 1 more comment

2 answers

1


In your index.php you give a href anchor to edit the expense. You can use this href that creates here:

<a class="editarDespesa" href="php/editarDespesas.php?id=<?php echo $visual['id']; ?>">Editar</a>

That is, in your file .js use like this:

$(".editarDespesa").click(function (e) {
    e.preventDefault();
    var href = $(this).attr("href");
    $("#formularioDespesas[class]").attr("class", "formVisivel");

    $.post(href , function (data) {
        $("#formularioDespesas").html(data);
    });

    return false;
});
  • 2

    Sergio you are the guy! Thank you very much. You solve my problem with this code that you drew up. I’m sorry I can’t vote for your answer, I need to have at least 15 reputable points, only I’m new to stackoverflow. Hugs!

  • 1

    @Deyvischarles I’m glad I helped. If you want you can mark the answer as right/accepted by clicking on the marker.

0

I know it’s been a while, but as some people still use this question as a basis, I decided to refine it by following what @Sergio did.

<a class="editarDespesa" href="#" data-id="<?php echo $visual['id']; ?>">Editar</a>

Switch to using the attribute date adding the value to it by setting the data-id="".

And in the javascript block insert like this:

$('body').on('click','.editarDespesa',function (e) {
e.preventDefault();
var id = $(this).data('id');
var href = 'php/editarDespesas.php?id='+id;
$("#formularioDespesas[class]").attr("class", "formVisivel");

$.post(href , function (data) {
    $("#formularioDespesas").html(data);
});

return false;
});

Personally I prefer to work that way.

Browser other questions tagged

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