0

I am creating an application where a user can select from a list of checkbox's and their choice will be captured, post, and sent to the database. I have searched many forums none have been successful. Here is the code I have:

$('#chkbxId').on('change', function (e){
var value = $(this).val();
var name = $(this).attr('name');

e.preventDefault();

$.ajax({
  type: 'POST',
  url:  '../php/some.php',
  async:  true,
  data: {
    name: value
  },
  success:  function (msg) {
    alert('success');
    if (msg!='success') {
      alert('fail');
    }
  }
});

}); I have an onchange event because I do not want a submit button.

Edit: While other post have tried to use ajax and jquery to submit a form without redirection. My inquiry is to do it onchange through a checkbox element. My application does not contain a submit button.

  • @OmriLuzon it is not. That user is attempting to send a form without redirection , when I am trying to submit an input element individually onchange. Please read the differences before making assumptions please. –  Jun 10 '17 at 17:33
  • I flagged as possible duplicate because you seem very confused, there is no single part in your code that does redirection. And I encounter this kind of question several times per day (php post without redirect). Are you using `preventDefault` to prevent state change on the checkbox? – Omri Luzon Jun 10 '17 at 17:40
  • It seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Omri Luzon Jun 10 '17 at 17:40
  • @OmriLuzon thats inaccurate as i still run my program it still redirects and i have preventDefault; however, it still redirects. –  Jun 10 '17 at 17:44
  • So update the question with the relevant code to the page redirection, and I'll remove the flag. Your title suggests you have an unwanted page redirection that isn't present in your code. (you even used the word submit, tell me this isn't really what you meant) – Omri Luzon Jun 10 '17 at 18:07

0 Answers0