简体   繁体   中英

How to remain on the same page after clicking submit on a form?

I have an html file/page where there will be multiple input fields in a form. This form action field is linked to another .php file where all the inputted data is written to a database. But once when i click submit the form i get directed to the .php file which is not what i want.Is there any way by which i could write the data into the database ,but remain in the same html file?

If you want to do this, you have to use javascript and ajax.

your form:

<form id="yourform">...</form>

javascript:

$(function() {
    $('#yourform').on('submit', function(e) {
        var data = $("#yourform :input").serialize();
        $.ajax({
            type: "POST",
            url: "script.php",
            data: data,
        });
        e.preventDefault();
    });
});

of course. use ajax. do some web searches on ajax combined with html and you will find the answers you are looking for. jquery is one possible javascript/ajax platform that could simplify this process for you.

You have 2 options:

1 - Submit you form using AJAX. With this approach the page will stay "refresh" and the user will be on the same page

2 - After the submit, redirect the user back to the previous page.

You could make the submit button send the form data to a div tag on the same page using ajax. The code from Dynamic Drive should work if you read through it.

Dynamic Drive - Dynamic Ajax Content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM