0

I am trying to insert value in database through ajax. There rows which are coming in while loop from database. I have added one column in each row which have button. On click of button I am submitting the values in database its working fine.

But problem is that it inserts value from first row only. Need help. Thanks in advance.

Not getting solution for problem

PHP Code :

<td><div class="form_style">  
<input type="hidden" name="crs" id="crs" value="<?php echo $crs; ?>">     
<input type="hidden" name="clg" id="clg" value="<?php echo $clg; ?>">     

    <button id="FormSubmit">Apply Now</button>    
    <img src="images/loading.gif" id="LoadingImage" style="display:none" />     
    </div></td>   

Ajax and javascript Code :

<script type="text/javascript">   
$(document).ready(function() {     

    $("#FormSubmit").click(function (e) {   
            e.preventDefault();   

            $("#FormSubmit").hide(); 
            $("#LoadingImage").show(); 

            var myData = {
                    crs: $('#crs').val(),
                    clg: $('#clg').val()

                  };

            jQuery.ajax({
            type: "POST", 
            url: "response.php",
            dataType:"text", 
            data:myData, 
            success:function(response){
                $("#responds").append(response);
                $("#crs").val(''); 
                $("#clg").val('');
                $("#FormSubmit").show(); 
                $("#LoadingImage").hide(); 

            },
            error:function (xhr, ajaxOptions, thrownError){
                $("#FormSubmit").show(); 
                $("#LoadingImage").hide(); 
                alert(thrownError);
            }
            });
    });

});
</script>

Response.php

<?php




    $crs = $_POST["crs"]; 
    $clg = $_POST["clg"]; 



    $strsreg="insert into add_delete_record  (id, content, content1) values('', '$crs', '$clg')";
    $resultsreg=mysql_query($strsreg) or die(mysql_error());



?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Test
  • 45
  • 1
  • 8

1 Answers1

0

We need to know if your response.php is prepare to receive any json object of just as you define myData variable

But in general, like you said, you are just passing the first value, you need tio get each crs and clg, exists many ways, but you need something like this:

var data = [];
$("table").find(".form_style input:hidden").each(function(){ 
    data.push(this.id); 
});

check this: https://jsfiddle.net/8gxs0wka/

cromerou
  • 1
  • 1
  • I have updated response.php in my question. My problem is that it insert value for first click only not after that. – Test Mar 10 '17 at 12:39
  • Where should i use this function? – Test Mar 10 '17 at 12:43
  • when you use $("#crs") you are selecting all the #crs, but if you dont iterate through them, when call val(), you will get the first value. (for this i use jQuery.each) – cromerou Mar 10 '17 at 12:49
  • where should i use this function in my function – Test Mar 10 '17 at 12:51
  • @cromerou shouldn't iterate through. Ids should be unique. – chris85 Mar 10 '17 at 12:53
  • my friend, in javascript ids are a conceptual term (ids should be unique) but in practice you can use them as you want. check: https://jsfiddle.net/h6c9rpth/ – cromerou Mar 10 '17 at 12:57
  • Iterating over something is not needed if you have an identifier, which is the point of an `id`. – chris85 Mar 10 '17 at 14:24
  • #chris85but the point is, he is not using correctly the ids, he should use classes instead. @Test check this: http://stackoverflow.com/questions/5526917/how-to-do-a-batch-insert-in-mysql you can rewrite your response.php to manage the entire set of data, if you pass this correctly. – cromerou Mar 10 '17 at 17:10
  • where should i use your function ? – Test Mar 11 '17 at 06:04
  • Not getting solution for problem – Test Mar 11 '17 at 07:30