0

我在 foreach 循环中有此表单,因此它在同一页面上显示多次。

除了单选按钮和复选框之外,每个表单中的所有内容都可以正常提交。他们不会将值保存到数据库中。

编辑:我已将其范围缩小到导致错误的 ajax,但无法弄清楚如何纠正它。

<form action="process.php" method="post" name="editInvoice'.$invoice_id.'" id="editInvoiceForm'.$invoice_id.'" class="editInvoiceForm edit_invoice_container" enctype="multipart/form-data">
<div class="form_item_row">
<input type="radio" value="Unsent" '.$unsent.' name="status"/><span class="choice">Unsent</span>
<input type="radio" value="Sent" '.$sent.' name="status"/><span class="choice">Sent</span>
<input type="radio" value="Paid" '.$paid.' name="status"/><span class="choice">Paid</span>          
</div>          
<div class="form_item_row">
<label for="include_timelog'.$invoice_id.'">Include Time Log</label>
<input type="checkbox" value="true" '.$include_timelog.' name="include_timelog" id="include_timelog'.$invoice_id.'" />
</div>
<div class="clear"></div>               
<div class="form_item_row_btns">
<input type="hidden" value="'.$invoice_id.'" name="hiddenInvoiceID"/>
<input type="submit" class="btn" value="Update Invoice" name="action"/>
</div>
</form>

$query = "UPDATE invoices SET status = ".$db->prep($_POST['status']).", include_timelog = ".$db->prep((isset($_POST['include_timelog'])?1:0))." WHERE invoice_id = ".$db->prep($invoice_id);

$(document).ready(function()
{       
        var action = '';

        $(".due_date").datepicker();

        $('input[name=action]').click(function(){
            action = $(this).val();
        });

        $(".editInvoiceForm").submit(function() {
            $('.editInvoiceForm .form_message').html('<img src="images/loadingAnimation.gif" alt="loadingAnimation" width="30" height="8"/>');

            var dataToSend = {};
            $(this).find(':input').each(function (i,el) {
                dataToSend[el.name] = $(el).val();
            });

            dataToSend.action = action;

            $.ajax({
                type: "POST",
                url: "process.php",
                data: dataToSend,
                dataType: "json",
                cache: false,
                success: function(data){
                    //console.log(data.status);
                    if(data.status == 'error'){
                        $('.editInvoiceForm .form_message').removeClass('status_green').addClass('status_red').html(data.message).append(data.script);
                    }else{
                        $('.editInvoiceForm .form_message').removeClass('status_red').addClass('status_green').html(data.message).append(data.script);

                    }

                }
            });
            return false;
        });

});
4

1 回答 1

2

您需要在 SQL 中的状态值周围加上引号,因为该值是一个字符串。您的 include_timelog 和 invoice_id 值是整数,不需要引号。

$query = "UPDATE invoices SET status = '".$db->prep($_POST['status'])."', include_timelog = ".$db->prep((isset($_POST['include_timelog'])?1:0))." WHERE invoice_id = ".$db->prep($invoice_id);
于 2012-10-30T02:51:29.477 回答