简体   繁体   中英

Submit form in to database using ajax in while loop

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());



?>

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM