AJAX-No refresh navigation with PHP

Asked

Viewed 621 times

2

Good staff I am with a doubt that I can not solve through the WEB.
On my page I have a menu that calls some contents to a DIV of content id, that with ajax, and is working, the contents are updated and loaded as I call them.
My problem is this:

The loaded content is a form, and in this form has an imput type="Submit" button, and the data is passed by post. In this form I did not put action because I want it to be passed by ajax and not directly by PHP to not generate refresh. But when I click the button it does not work, I believe it is by my ajax code made for Submit. Because I load it when the page opens and the form is inserted after that through the menu request. Follow my ajax code:

    $(document).ready(function(){
form_ajax();

    });

    function form_ajax(){
$('#form-ajax').submit(function(){
    var content = $('#conteudo');
    alert('carregou');
        $.ajax({
            type: "POST",
            url: "processa.php",
            success: function( response )
            {
                var data = $( '<div>'+response+'</div>' ).find('#conteudo').html();
                window.setTimeout( function(){
                        content.fadeOut('fast', function(){
                            content.html( data ).fadeIn('fast');
                        });
                    }, 50 );
            }
        });

        return false;
    });
    }


In this code I put an Alert to see if it reached there, but it didn’t. So I think it’s because of the load of the function.
I wonder if anyone has a better option, or something to tell me how to do it. I did a lot of research but I didn’t get into the context I’d like.

I hope you understand the question.
From now on thank you all!
Follows HTML requested:

<!DOCTYPE html>
    <html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/estilo.css" type="text/css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <title>index</title>
    </head>
    <body>
<div class="global">
    <div class="topo">

    </div>
    <!--End TOPO-->
    <div class="central">
        <div class="menu-lateral" id="menu">
            <li class="active">
                <img src="icon2.png" alt="">
                <a href="site_oficial/login.php"> Home </a>
            </li>
            <li>
                <img src="icon1.png" alt="">
                <a href="form-ajax.php"> DashBoard </a>
            </li>
            <li class="parent">
            <img src="icon3.png" alt="">
            <a href="#">Teste 1</a>
                <ul class="sub-menu">
                    <li><a href="#">testado 1</a></li>
                    <li><a href="#">testado 2</a></li>
                </ul>
            </li>
            <li class="parent">
            <img src="icon4.png" alt="">
            <a href="#">Teste 2</a>
                <ul class="sub-menu">
                    <li><a href="#">testado 3</a></li>
                    <li><a href="#">testado 4</a></li>
                </ul>
            </li>
            <li class="parent">
            <img src="icon5.png" alt="">
            <a href="#">Teste 3</a>
                <ul class="sub-menu">
                    <li><a href="#">testado 3</a></li>
                    <li><a href="#">testado 4</a></li>
                </ul>
            </li>

        </div>
        <!--End MENU-->
        <div id="conteudo">

        </div>
        <!--End CONTEUDO-->
    </div>
    <!--End CONTAINER-->
</div>
<!--End GLOBAL-->
<script src="js/menu-lateral.js"></script>
<script src="js/form-ajax.js"></script>
<script src="js/ajax-load.js"></script>

  • Display HTML so people can see.

  • @Andréhenriques put the HTML index is the one you wanted?

  • If I use the from button that has been loaded the onclick="method()" property it even runs, otherwise it does not. :(

1 answer

1


It seems to me that your problem is delegation. That is when you run form_ajax() to <form> is not yet on the page, and $('#form-ajax') will empty and from there does not even run the rest of the code.

You have two options, or you add that call to duty form_ajax() in AJAX that adds the HTML of the form (after the line that inserts the form); or you change this event receiver to $(document).on('submit', '#form-ajax', function(){. That way only the moment Submit happens jQuery will check where it came from, and then the <form> is already on the page.

  • 1

    Thank you Sergio, your reply was very helpful and solved my problem, it seems that you read thought :D was even worth!!!

Browser other questions tagged

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