简体   繁体   English

在while循环中使用Ajax将表单提交到数据库

[英]Submit form in to database using ajax in while loop

I am trying to insert value in database through ajax. 我试图通过ajax在数据库中插入值。 There rows which are coming in while loop from database. 从数据库循环进入while的行。 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 : PHP代码:

<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 : Ajax和javascript代码:

<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 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 我们需要知道您的response.php是否准备好像定义myData变量一样接收任何json对象

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: 但是总的来说,就像您说的那样,您只是传递第一个值,您需要tio获得每个crsclg ,存在很多方式,但是您需要这样的东西:

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

check this: https://jsfiddle.net/8gxs0wka/ 检查一下: https : //jsfiddle.net/8gxs0wka/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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