3

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());
}
Lauren F
  • 1,282
  • 4
  • 18
  • 29
  • Have you tried putting $("form").submit(); before adding the disabled attribute to the button? – OllyBarca Jun 03 '15 at 20:56
  • 1
    what'd be the point of this? your form doesn't have to get filled out. a spammer can just directly submit your field=value pairs directly to the server with out EVER loading your form. – Marc B Jun 03 '15 at 21:00
  • Tried that and still isn't working – Lauren F Jun 03 '15 at 21:00
  • 1
    @MarcB if a user presses the register button multiple times while the form is submitting, multiple registration requests are posted the server. Disabling the button would allow the form to submit with only one request. – Lauren F Jun 03 '15 at 21:02
  • can you show exactly your form code? – Alive to die - Anant Jun 03 '15 at 21:02
  • @anantkumarsingh I've added the function the form posts to my question. The form itself isn't more than what I'd already shown. – Lauren F Jun 03 '15 at 21:05

2 Answers2

4

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();
});
taxicala
  • 21,408
  • 7
  • 37
  • 66
1

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

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

Community
  • 1
  • 1
Lauren F
  • 1,282
  • 4
  • 18
  • 29