0

I am trying to make the form submit the data for processor.php to be processed and responses added into a database, but the form is currently not submitting any data.

It is also very important to do this without page refresh, because I have an iframe below the form that should remain untouched.

Here is my form:

<form id="myForm" action="" method="post">
    <span id="surveyQuestion">At first glance, how likely it is for you to use the service/product being advertised?</span><br/>
    <span class="info">Not likely</span>
    <input type="radio" name="response" value="0" onClick="this.form.submit()">
    <input type="radio" name="response" value="1" onClick="this.form.submit()">
    <input type="radio" name="response" value="2" onClick="this.form.submit()">
    <input type="radio" name="response" value="3" onClick="this.form.submit()">
    <input type="radio" name="response" value="4" onClick="this.form.submit()">
    <input type="radio" name="response" value="5" onClick="this.form.submit()">
    <span class="info">Very likely!</span>
</form>

And here is the jQuery/Ajax:

  $(function () {

    $('#myForm').submit(function (e) {

      e.preventDefault();

      $.ajax({
        type: 'post',
        url: 'processor.php',
        data: $('#myForm').serialize(),
        success: function () {
          alert('form was submitted');
        }
      });

    });

  });
2
  • use $('#myform').bind('submit', function(){ dataType: 'json', Commented Apr 11, 2015 at 16:49
  • Isn't it so that in your case this.form.submit(), this refers to the radio button and not the form? Commented Apr 11, 2015 at 16:50

2 Answers 2

2

Try Jquery .change()

 $("input.myradio").change(function(){
   alert("hi");
$.ajax({
        type: 'post',
        url: 'processor.php',
        data: $('#myForm').serialize(),
        success: function () {
          alert('form was submitted');
        }
      });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm" action="" method="post">
    <span id="surveyQuestion">At first glance, how likely it is for you to use the service/product being advertised?</span><br/>
    <span class="info">Not likely</span>
    <input type="radio" class="myradio" name="response" value="0" onClick="this.form.submit()">
    <input type="radio" class="myradio" name="response" value="1" onClick="this.form.submit()">
    <input type="radio" class="myradio" name="response" value="2" onClick="this.form.submit()">
    <input type="radio" class="myradio" name="response" value="3" onClick="this.form.submit()">
    <input type="radio" class="myradio" name="response" value="4" onClick="this.form.submit()">
    <input type="radio" class="myradio" name="response" value="5" onClick="this.form.submit()">
    <span class="info">Very likely!</span>
</form>

Sign up to request clarification or add additional context in comments.

2 Comments

How do I prevent this from refreshing the whole page?
remove onClick="this.form.submit()" from the radios
0
$('input[type=radio]').click(function() {

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.