skip to Main Content

Trying to submit a form using ajax, but it’s just not working

$('#Submit').on('click',function(){
    $.ajax({
        type: 'get',
        url: 'ItemProcessor.php',
        data: $('#input_form').serialize(),
        success: function(response){
            alert(response)
        },
        error: function(){
            alert('error')
        }
    })
event.preventDefault()
})
<form id="input_form">
    <input type="image" src="AddToCart.png" id="Submit"/>
    <input type="number" id = "stupid" name = "Amount" min="1">
    <input type="text" name = "ID" value="Ice Cream" readonly style="display: none;">
    <input type="text" name = "Cost" value="2" readonly style="display: none;">
    <input type="text" name = "Kom" value="1" readonly style="display: none;">
    <input type="text" name = "EAN" value="4551212511" readonly style="display: none;">
    <input type="text" name = "Type" value="Food/Sweets" readonly style="display: none;">
</form>

I want the javascript code to send the form to ItemProcessor.php, but when I click the form, the form just acts like normal and the function doesn’t get called. neither success nor error get called, so I’m assuming the function isn’t activating but I have no idea why. This is the only form on the page

2

Answers


  1. My guess is event is not defined, give your event function a parameter i.e. event

    $('#Submit').on('click',function(event){
    
    Login or Signup to reply.
  2. You need to set type to ‘post’, you can not send data with ‘get’.

    $('#Submit').on('click',function(){
        $.ajax({
            type: 'post',
            url: 'ItemProcessor.php',
            data: $('#input_form').serialize(),
            success: function(response){
                alert(response)
            },
            error: function(){
                alert('error')
            }
        })
    event.preventDefault()
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search