ajax form upload loading a new page

2019-09-11 01:56发布

问题:

I had a similar question yesterday - cud not resolve the issue

I am uploading a form with file and other inputs and all that is working fine. But the problem is that it is loading a new page i.e

www.mywebsite.com/controller/function. 

This is my code

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 

<script type="text/javascript">
    $(document).ready(function() {
        var options = { 
            clearForm: true,
            resetForm: true
        };
        // bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function() { 
            //alert("Thank you for your comment!"); 

        }); 
        $('#myForm').ajaxForm(options);

    }); 
</script> 

</head>

<body>
<form id="myForm" name="myForm" action="/main/comment" method="post"         enctype="multipart/form-data"> 
    <input type="text" name="name" />
    <br /> 
    <textarea name="comment"></textarea> 
    <br />
    <input type="file" value="Share a Pic" name="file" id="file" />
    <br />
    <input type="submit" value="Submit Comment" /> 
</form>
</body>

I removed "main/comment" from my action and added url in var option part :-

var options = { 
            url: "<?php echo site_url().'/main/comment' ?>",
            clearForm: true,
            resetForm: true
        };

but even this does not seem to work. Please help Thanks

回答1:

If you follow the example shown here

I think you need to do this:

function successFunction(){
    alert('Thank you');
}
$(document).ready(function() {
        var options = { 
            clearForm: true,
            resetForm: true,
            success: successFunction
        };
        $('#myForm').ajaxForm(options);
});