Tutorial :is the browser or server ignoring the code?



Question:

i made this form:

<form id="form" name="msgform" method="" action="">  <input type="text" size="40" id="msg" name="message"/>  <input type="submit" id="button" name="clicker" value="click" />  </form>  

and this jquery script:

$(document).ready(function(){    $("#button").click(function(){    $("#form).submit(function(){      var submision= $("#form).val();    $.post("txt/process.php", submision, function(data){  alert(data);    });    });  });  });  

and this is the process.php file:

<?php  echo $_POST['message'] . "";  ?>  

now when i click the button the form is submited, but it sends it using the GET method because i can see it in the adress bar, but it never gets sent to the php file, and i checked to see if the names are correct and if i specify the POST method it still doesnt go to the php file. is the server or browser ignoring the code? or am i doing the whole thing wrong?

thanks


Solution:1

Please find the following code, it works and please go through with the documentation, it will tell you that what the mistake was being done.

  $(document).ready(function(){      $("#button").click(function(){          $("#form").submit(function(){              /* var submision= $("#form).val();                  THIS DOESN'T WORK TO GET ALL OF THE ELEMENTS IN                                   FORMAT TO PASS TO $.post EVENT,                   We can do this as I did in following example                           */              $.post("txt/process.php", { msg: $("#msg").val() }, function(data){                  alert(data);              });              /* Also you didn't put return false statement just at the end                             of submit event which stops propagating this event further.                               so it doesn't get submitted as usually it can be without ajax,                              So this stops sending the form elements in url. This was because                              by default if you define nothing in method property for form                              then it consider it as GET method.                           */              return false;          });      });  });  

Let me know please you are facing any issue.


Solution:2

You don't need to register the submit event for the form inside the click handler of the button. As it is a submit button it will automatically try to submit the form for which you register the corresponding handler:

$(function() {      $('#form').submit(function() {          // Get all the values from the inputs          var formValues = $(this).serialize();          $.post('txt/process.php', formValues, function(data) {              alert(data);          });          // Cancel the default submit          return false;      });  });  


Solution:3

$("#form).submit(function(){

see if this selector is missing a "

$("#form").submit(function(){


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »