navyad
navyad

Reputation: 3860

url should not change on submitting a form

i have this Django application in which when user request for "localhost:8000/time/ ,it is shown html form input.html.

<head>

<!-- paste this in your footer -->
<script type="text/javascript"> 
$(document).ready(function() {
        $('#myForm').submit(function() { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(),
                type: $(this).attr('POST'), // "post"
                url: $(this).attr('/poi/'), //  "/poi/"
                success: function(response) { 
                    // do something here
                }
            });
        return false;
    });
});
</script>
<!-- add jquery or it won't work -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>

<body>
        <form action="/poi/" method = "post">

        <!---------
        first name :<input type ="text" name ="fname">
        last name:<input type ="text" name ="lname">
        ------>

        enter a days :<input type ="text" name ="days">
        <br>
        <input type=submit name="submit" onclick="submit_function(); return false;">
        </form>

</body>

when form is submitted user sees response.html page and URL is changed to "localhost:8000/poi/ is shown having

urls.py

urlpatterns = patterns('',
                            (r'^hello/$', hello),   
                            ('^time/$', current_datetime),
                            ('^poi/$', next_datetime),

                        )

views.py

def current_datetime(request):  
    return render_to_response('input.html')


def next_datetime(request):

    now = datetime.datetime.now()
    now_day = now.day
    now_month = now.month
    now_year = now.year

    return render_to_response('response.html', {'now_day': now_day, 'now_month'}}

now i have to do same thing but the url should not change from "localhost:8000/time/ to "localhost:8000/poi/ but it should be "localhost:8000/time/

how to accomplish that?

Upvotes: 1

Views: 3473

Answers (1)

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11038

You have to use ajax to accomplish this With jquery it would look something like

<form id="myForm"> ... </form> <!-- add an id to your form -->

<!-- paste this in your footer -->
<script type="text/javascript"> 
$(document).ready(function() {
        $('#myForm').submit(function() { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(),
                type: $(this).attr('method'), // "post"
                url: $(this).attr('action'), //  "/poi/"
                success: function(response) { 
                    // do something here
                }
            });
        return false;
    });
});
</script>
<!-- add jquery or it won't work -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

and it should work out of the box

this is with an anonymous function, but you can put the code inside a named one and then add it to the button:

<input type="submit" onclick="submit_function(); return false;" />

it's basically the same

Upvotes: 1

Related Questions