Tushar
Tushar

Reputation: 166

Submitting form using Jquery Issue

I am using Jquery to submit a form. My code looks like this.

<?php if(isset($_POST['submit'])){
  echo $_POST['value'];
} else { ?>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type="text/javascript" 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCV9b6j5csMVrafZXp6uTHOavxiJk4vqK4"></script>

  <form method="POST" action="" id="form1" >
    <input type="text" id="value" name="value">
    <button name="submit" onclick="tushar(); return false;">
      Click Me
    </button>

  </form>
  <script type="text/javascript">
    var tushar = function(){
      var geocoder = new google.maps.Geocoder();
      var address = "Hostel-7, IIT Bombay, Powai, Mumbai, Maharashtra,400076";
      geocoder.geocode({ 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          $("#value").val("POINT(");
          $('form#form1').submit();  
        } else {
          console.log(status);
          return false;
        }

      });       
    }
  </script>
<?php } ?>

On clicking the button, the value of input field is changed but the form is not being submitted. Can anyone help me with this? Thanks in advance.

Upvotes: 0

Views: 41

Answers (2)

PeterKA
PeterKA

Reputation: 24638

Please change $('form#form1').submit(); to $('#form1')[0].submit();

$('form#form1').submit(); is simply re-triggering the submit event on the form.

$('form#form1')[0].submit(); is used for submitting the form the default way. Note: an ID is usually enough to select an element hence $('#form1').

Further

As noted by @epascarello, you also need to change the name of your submit button so that it does not conflict with the event.

Upvotes: 0

epascarello
epascarello

Reputation: 207511

Change the button name from submit to btnSubmit

<button name="btnSubmit" onclick="tushar(); return false;">Click Me</button>

and submitting should magically work when you stop overriding the method.

Upvotes: 1

Related Questions