Redirect page by passing "ajax" response to a div

1

I know the question is strange and may seem nonsensical, but I need a response, even if it's for impossível . I do a search in my index and I need to pass the result on it to another page positioning in a div. I did some research and tried some alternatives, but without success and as I said, I do not even know if this is possible.

What I have already got is this, the return of a search, I consult the response and I already visualize the desired content.

$(function() {      
    $("#frmBusca").validate({
        submitHandler: function(form) {
            var data = $(form).serialize();             

            $.ajax({
                type: 'POST',
                url: 'pBuscaGenerica.php',
                data: data,
                dataType: 'html',

                success: function(response) {

                    window.location.replace("outra_pag.php");

                    // EXIBINDO O RETORNO DAS INFORMAÇÕES   
                     $("#msgRamais").html(response);
                    // RESETANDO A FUNÇÃO
                     _toggle();                 

                },
                error: function(xhr, ajaxOptions, thrownError) {
                    console.log(xhr, ajaxOptions, thrownError);
                    $("#msgRamais").html('×ATENÇÃO! Ocorreu um erro ao tentar obter o ramal. Contate o suporte técnico.');
                }
            });
            return false;
        }
    });
});

I need to redirect to a certain div , I will not post the attempts because they were unproductive and would not add anything to the post.

    
asked by anonymous 02.08.2016 / 16:09

3 answers

2

From what I realized is what you want (the way I would):

JS:

$.ajax({
            type: 'POST',
            url: 'pBuscaGenerica.php',
            data: data,
            dataType: 'html',

            success: function(response) {
                window.location.replace("outra_pag.php");
            },
            error: function(xhr, ajaxOptions, thrownError) {
                console.log(xhr, ajaxOptions, thrownError);
                $("#msgRamais").html('×ATENÇÃO! Ocorreu um erro ao tentar obter o ramal. Contate o suporte técnico.');
            }
        });

On the server side, php, stored the information you wanted to pass to the other page, this is when you perform the ajax call:

pBuscaGenerica.php :

session_start();
...
$_SESSION['ajax_result'] = 'isto vai ser exibido dentro de uma div numa outra página que não a que fez a chamada';
...

otra_pag.php :

session_start();
...
if(!isset($_SESSION['ajax_result'])) {
    // fazer uma coisa qualquer caso não haja essa variável, não tenha armazenado a informação
}
else {
    echo '<div>' .$_SESSION['ajax_result']. '</div>';
}

Some notes:

  • Your error is that you are doing $("#msgRamais").html(response); but this is useless, you do not need this on this page (because it will be redirected to another, window.location.replace("outra_pag.php"); )

  • If you need to have the variable $_SESSION['ajax_result'] set to display the other_pag.php within if(!isset($_SESSION['ajax_result'])) {... put the redirect ... In this case you have to else {... is down ... Make sure you have this redirect at the top of the page before any print (output).

  • 02.08.2016 / 16:28
    2

    I will not post codes, but the idea would be as follows:

    Perform the search via AJAX. Store in one variable and move to another page via PHP POST.

        
    02.08.2016 / 16:20
    2

    @adventistapr, So the example below about Ajax + PHP

    File (index.html):

    <html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head><body><formid="ajax_form">
            <p>Texto:</p>
            <p><textarea name="texto" rows="10" cols="30" placeholder="Descreva um comentario" /></textarea></p>
            <p><input type="submit" name="enviar"/></p>
        </form>
        <hr><p>Ajax:</p>
        <textarea id="log" rows="20" cols="70"></textarea>
    </body>
    
    <script>
    $(document).ready(function(){
        $('#ajax_form').submit(function(){
            var dados = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "log.php",
                data: dados,
                success: function(testlog) {
                    $('textarea#log').text(testlog);
                }
            });
            return false;
        });
    });
    </script>
    
    </html>
    

    File (log.php):

    <?php
    if (!empty($_POST)) {
        echo $_POST['texto'];
    }
    ?>
    
        
    02.08.2016 / 16:38