Redirect page by passing "Answer" from ajax to a div

Asked

Viewed 5,484 times

1

I know this is a strange question, and it may seem pointless, but I need an answer, even if it’s for impossível. I do a search in my indexand need to pass the result in the same to another page positioning in a div. I did some research and tried some alternatives, but without success and as I said, I don’t even know if that’s possible.

What I already have is this, the return of a research, I consult response and already visualise the content desired.

$(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 particular div, I will not post the attempts because they were unproductive and would not add anything to the post.

  • Want to put the result (Replay) inside a div of another page? That’s it?

  • Hello @Miguel, just that.

  • Not yet? I see that you have updated the code... So I see you want to redirect to another page when you receive the answer?

  • Hello @Miguel, what I need is a little complex, I need to move Replay to the next page and show it in a div, but I don’t know if this is possible.

  • Of course it is, follow the steps of my reply. Also read the notes. Want to redirect to the other pag in the ajax function succeso?

  • redirect and pass the content of 'Answer'.

  • Edited answer. I think that’s it... Read it carefully

Show 2 more comments

3 answers

2

I won’t post codes, but the idea would be:

Perform the search via AJAX. Store in one variable and pass the other page via PHP POST.

  • #Shura16 already answered the new rss post and I know what boring to do kkkkk

2


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

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 when you call ajax:

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';
...

other_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:

  1. Your mistake is what you’re doing $("#msgRamais").html(response); but this is useless, do not need this on this page (because it will be redirected to another, window.location.replace("outra_pag.php");)

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

  • Hello @Miguel, what is the value of this variable? $_SESSION['ajax_result']; Where are you assigning value to it?

  • It is whatever is presented in the div, your @adventistapr answer . Set this in pBuscaGenerica.php when you call ajax

2

@adventistapr, So the example below on 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>
    <form id="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'];
}
?>
  • I didn’t understand very well @Kingrider, I need to pass the content of 'Answer' to the next page, as was your example I have to perform a new rescue?

  • 1

    This is an example of code, to register and exit the reply give success without leaving on the success page.

Browser other questions tagged

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