jonthoughtit
jonthoughtit

Reputation: 165

Radio/Checkboxes not saving to db

I have this form in a foreach loop, so it shows multiple times on the same page.

Everything submits fine in each form EXCEPT the radio buttons and checkboxes. They don't save the values to the db.

EDIT: I've narrowed it down to the ajax causing the error but can't figure out how to correct it.

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

});

Upvotes: 0

Views: 127

Answers (1)

John Moses
John Moses

Reputation: 1283

You need quotes around the value for status in your SQL since the value is a string. Your include_timelog and invoice_id values are integers and do not need quotes.

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

Upvotes: 2

Related Questions