Lauren F
Lauren F

Reputation: 1322

Disable button on click but still process form

I want to disable my register button on click to prevent spam, but I don't want to prevent the form the button submits from submitting.

I'm able to disable the button on click, but can't figure out why the form still won't submit...

button:

<?= form_open("course/registerCourse", "method='post'") ?>
   <input type="hidden" name="session" value="<?= $session["id"] ?>">
   <button type="submit" class="btn-sm btn btn-success register">REGISTER</button>
<?= form_close() ?>

jQuery:

$('.register').on('click',function() {
    $(this).prop("disabled",true);
    $("form").submit();
});

register function:

public function registerCourse()
{

    $user = $this->users_model->userAccountTP();
    $appParticipant = $user[0]["id"];

    $author_id = $this->session->userdata("ci_user_id");
    $appSession = $this->input->post('session');

    $this->ci_channel_entry_model->save_multiple(array(
        "channel_id" => 11,
        "title" => date("Y-m-d h:i:s"),
        "author_id" => $author_id,
        "status_id" => 7,
        "date" => date("Y-m-d h:i:s"),
        "data" => array(
            array(
                // application-session
                "field_id" => 28,
                "content" => $appSession
            ),
            array(
                // application-participant
                "field_id" => 29,
                "content" => $appParticipant
            )
        ),
        "hook" => FALSE
    ));

    redirect($this->agent->referrer());
}

Upvotes: 3

Views: 1253

Answers (2)

Lauren F
Lauren F

Reputation: 1322

Ended up reworking my form to align with this answer and everything worked:

https://stackoverflow.com/a/5691065/4141833

Upvotes: 1

taxicala
taxicala

Reputation: 21789

I think you might have a typo, because I dont see where you are declaring $form, so maybe this helps:

$('.register').on('click',function() {
    $(this).prop("disabled",true);
    $("form").submit();
});

Upvotes: 4

Related Questions