How to reload a PHP page via Javascript?

Asked

Viewed 118 times

0

I own a page in PHP and that records the dodos of a form via JSON, below the script:

Index.php

<?php
  include '../includes/config_db.php';
  include '../includes/functions.php';
  include 'pdv_lib.php';

  class Conta {
   public $mesaNumero;
   public $id;
   public $referenciaId;
   public $status;
  }

  $id_estabelecimento = addslashes($_REQUEST['estabelecimento']);
  $mac        = addslashes($_REQUEST['mac']);

  $conn = conectar_pdv();
  $conta = get_conta($conn, $mac, $link, $id_estabelecimento);
 ?>

pdv_lib.php

<?php
  function get_conta($conexao_pdv, $mac, $conexao_portal, $estabelecimento_id)  {
    $conta = new Conta();

    echo "Mac = $mac, Estabelecimento = $estabelecimento_id<br>"; 
    echo '<pre>';
    print_r($conta);
    echo '</pre>';

    $query_cliente_conta = "SELECT referencia_conta_id, conta_id FROM clientes_contas where mac = '$mac' and estabelecimento_id = $estabelecimento_id order by modified desc";
    $result = mysqli_query($conexao_portal, $query_cliente_conta);
    $db_conta = $result->fetch_array(MYSQLI_ASSOC);

    $conta->referenciaId = $db_conta['referencia_conta_id'];
    $conta->id = $db_conta['conta_id'];

    echo '<pre>';
    print_r($conta);
    echo '</pre>';

    return $conta;
  }
?>

    function gravar_conta(mac, mesa_numero, estabelecimento_id, nome_conta)  {
      $.ajax({
        type: "POST",
        url: "gravar_conta.php?mac=" + mac + '&mesa_numero=' + mesa_numero + '&estabelecimento_id=' + estabelecimento_id + '&nome_conta=' + nome_conta,
        dataType: 'json',
        success: function() {
        }
      });

      location.href = "index.php?estabelecimento=" + estabelecimento_id + "&mac=" + mac;
    }

The code works correctly. The data is recorded in my database and then the page is reloaded by passing to itself two parameters, these two parameters will be queried in my database via PHP. The problem is that this newly recorded data is not found in my database after I reload the page via Javascript. Detail:

  • If I give a refresh via keyboard the page then finds this data.
  • If I exchange the values for data that has been recorded previously the page reloads recognizing these records, the problem apparently is that my PHP page does not find what I just recorded via JSON.
  • Have you tried using Location.href inside the Success callback? If you can post php that does the query, it may also be something related to browser/page caching

  • If I put inside the Success the callback does not work.

  • If you do not enter Success then there is something wrong with writing.php account, you get a php return in Success and debug the code in parts. If you can put the save account in the question.

  • Do not enter Success because the write.php account does not return a json, I just make insertions do not have to return something back. If I had an error I wouldn’t write it. But I will post the short php.

  • 1

    The correct is to put inside the method success even, because you can be redirected before completing the request of AJAX.

  • That’s right Paul, thank you!

  • In fact the correct, very correct (rs) since $.ajax implements the "interface" of Deferred Objects would be put in the jQuery.done

Show 2 more comments
No answers

Browser other questions tagged

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