Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Abhishek Bhardwaj
Abhishek Bhardwaj
3,316 Points

Jquery $.ajax not working

<form name="uploadp" id="uploadp" action="queries/basicintro.php" onsubmit="submitProfile(eve)" method="post" enctype="multipart/form-data" > <span class="fa fa-camera upload" id="uploadpic"></span> <input type="file" class="form-control opacity0" onchange="this.form.submit()" name="uploadprofile" id="uploadprofile"> <!-- <input type="submit" class="btn btn-success" id="button" name="btnform"> --> </form> <div id="msg"></div>


<script type="text/javascript">

function submitProfile(eprofile){

    var uploadprofile = $('#uploadprofile').val();
    eprofile.preventDefault();
    console.log(uploadprofile);

    $.ajax({
        url: 'queries/basicintro.php',
        type:'POST',
        async:false,
        data: {
            'uploadprofile': uploadprofile,
        },
        success:function(ta){
            $('#msg').html(ta);
        }

    });


    alert('hii');


}

</script>

Maximillian Fox
Maximillian Fox
Courses Plus Student 9,236 Points

Just looking at your function call - you have

<input type="file"  class="form-control opacity0" onchange="submitProfile(this.form.submit())"   name="uploadprofile" id="uploadprofile">

but then in your JS your function doesn't appear to input any arguments.

function submitProfile()

I see you call eprofile.preventDefault() - so should this be passed as an argument to then prevent the form submit event from reloading the page?

function submitProfile(eprofile)

I have never used AJAX but from a code POV, something seems off there. Maybe I'm wrong so not put this as an actual answer.

Abhishek Bhardwaj
Abhishek Bhardwaj
3,316 Points

I made some changes but still its not working. please take a look again in code :)

Maximillian Fox
Maximillian Fox
Courses Plus Student 9,236 Points

Add some console.log() at various points of the function. Check which ones appear and which don't and use that as a basis to debug your code. Check whether all your variables are printing correctly, check there are no syntax errors as well to be sure.