3

I have the following HTML fields being created inside a PHP look

<td><input type=\"checkbox\" name=\"investigator_id[]\" id=\"investigator_id\" value=\"$name_degree[$i]\"> 
<td><input type=text name=\"inv_rank[]\" id=inv_rank maxlength=\"2\" size=\"2\"></td>
<td><textarea name=\"inv_comm[]\" id=inv_comm rows=2 cols=20></textarea></td>

I am trying to save the data in these fields by calling a jquery function based on clicking on this button

Here is the script that is being called. I know that the js is being called because the "alert("now")" is poping up, but the dataString is not being populated correctly. I tested this on http://jsfiddle.net/ and it worked fine, but won't work on my site.

<script>
$(document).ready(function() {
    $("#submit").click(function() {
        alert("now");
        var dataString = $("'[name=\"investigator_id\[\]\"]', '[name=\"inv_rank\[\]\"]','[name=\"inv_comm\[\]\"]'").serialize();
        alert("ee"+dataString);
        $.ajax({
            type: "POST",
            url: "save_data.php",
            dataType: "json",
            data: dataString,
            success: function(data){
                alert("sure"+data);
                $("#myResponse").html(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("There was an error.");
            }
        });
    });
});
</script>
1
  • Use $('[name="investigator_id[]"]', '[name="inv_rank[]"]', '[name="inv_comm[]"]').serialize(); Commented Dec 23, 2015 at 11:54

1 Answer 1

2

Try this with the help of FormID like this:

<form method="post" id="yourFromID">
//Your form fields.
</form>

JS Code:

$("#yourFromID").submit(function (e){
  e.preventDefault();
  var dataString = $(this).serialize();

  // then you can do ajax call, like this
  $.ajax({
       url: 'site.com',
       data: dataString,
       methodL 'post',
       success: function(){...}
  })

return false;
    });
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, but I tried using it this way, but the problem is that it refreshes the page and I was trying to save it without refreshing the page. Any thoughts on how to do that?
@wessam-sonbol: also add return false; in ajax success after last line in success
@samiam: nice username well this is good now need one more thing chose the best answer and mark as accepted by left green tick mark this will help to others who facing same issue

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.