Home > Web Front-end > JS Tutorial > body text

How Can I Submit Form Data with $.load Without Reloading the Page?

Susan Sarandon
Release: 2024-11-11 07:14:02
Original
970 people have browsed it

How Can I Submit Form Data with $.load Without Reloading the Page?

Submitting Data via $.load Without Page Reload

When loading remote content within a form using $.load(), there can be issues with submitting data. This problem occurs when the request is handled by the same page as the form, resulting in a reload instead of proper data submission.

To resolve this, consider using AJAX techniques to post data to an external PHP file. Here's an example to demonstrate:

AJAX Code (FILE #1):

<script>
$(function() {
    $('#Sel').change(function() {
        var opt = $(this).val();
        var someelse = 'Hello';
        var more_stuff = 'Goodbye';
        $.ajax({
            type: "POST",
            url: "receiving_file.php",
            data: 'selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
            success:function(data){
                alert('Data received: ' + data);
            }
        });
    });
});
</script>
Copy after login

PHP File #2 (receiving_file.php):

<?php
    $recd = $_POST['selected_opt'];
    echo 'Option chosen: ' . $recd;
?>
Copy after login

In this setup, when the user changes the dropdown selection, the AJAX request is sent to receiving_file.php, which processes the data and returns a response that can be displayed in the browser without reloading the page. This addresses the issue of data not being posted correctly within the form.

The above is the detailed content of How Can I Submit Form Data with $.load Without Reloading the Page?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template