简体   繁体   中英

Trouble submitting AJAX form using hyperlink instead of submit button

I'm having problem submitting a HTML form using JavaScript & AJAX from a hyperlink (href) instead of the normal input type. My current code is as follows:

Javascript (executed just before body tag):

$(document).ready(function () {

        //Chronicle Form 1
        $('#fm_chronicledate').submit(function(event) {
            var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
                $.ajax({
                    type: "GET",
                    url: "script.php",
                    data: data, 
                    cache: false,
                    success: function(html) {
                        if(html == 1){
                        $('#chron_part1').fadeOut('slow');
                        } else alert('Error');
                    }
                });
            return false;
        });
});

HTML:

<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="#" class="chron_submit1" id="chron_submit1" onclick="fm_chronicledate.submit(); return false;">Submit</a>
</form>

Anyways for some reason it just does not execute the AJAX.

Any help would be much appreciated. Thank you.

Rather than use inline javascript, you can reference the link in the main script and capture the click event, like this...

HTML:

<form id="fm_chronicledate" method="POST" action="#" >
    <input type="text" class="website" name="chron_time" id="chron_time" />
    <input type="text" class="website" name="chron_date" id="chron_date" />
    <a href="#" class="chron_submit1" id="chron_submit1">Submit</a>
</form>

Javascript

$(document).ready(function () {
    $('#chron_submit1').on("click", function(event) {
        var data = $('#fm_chronicledate').serialize() +
            'cron=1' +
            '&chron_date=' + $("#chron_date").val() +
            '&chron_time=' + $("#chron_time").val();
        $.ajax({
            type: "GET",
            url: "script.php",
            data: data, 
            cache: false,
            success: function(html) {
                if (html == 1) {
                    $('#chron_part1').fadeOut('slow');
                }
                else
                    alert('Error');
            }
        });
    });
});

Why don't you just do:

<a href="#" class="chron_submit1" id="chron_submit1" onclick="Javascript:GetAjax()">Submit</a>

$(document).ready(function () {
    function GetAjax() {
        var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
            $.ajax({
                type: "GET",
                url: "script.php",
                data: data, 
                cache: false,
                success: function(html) {
                    if(html == 1){
                    $('#chron_part1').fadeOut('slow');
                    } else alert('Error');
                }
            });
        return false;
    }
});

First of all remove the onclick from your link and change it to

<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="javascript:void(0)" class="chron_submit1" id="chron_submit1">Submit</a>
</form>

Change you event to

$('#chron_submit1').click(function(event) {

Also you have an error in when serializing the form. Must be

var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + $(chron_date).val() + '&chron_time=' + $(chron_time).val();

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