0

I'm needing some help with the following code. I am trying to update multiple rows in my database using an ajax call. I am not receiving any error messages and nothing is appearing in my browser log either. I can see that the click function is executing but the database query is not being executed. Can someone point me in the right direction?

Thanks for any help you can give.

Update - The code below has been updated to work. I have commented out the original, non-working code.

My input fields:

<input type="hidden" id="Opportunity_Id" name="Opportunity_Id" value="<?php echo $opportunity->Opportunity_Id; ?>"/>
<input type="hidden" id="Class_Numbers[]" name="Class_Numbers" value="<?php echo $Class_Number; ?>"/>
<input type="text" id="Class_Dates[]" name="Class_Dates" value="<?php echo $Class_Date; ?>"/>
<input type="text" id="Start_Times[]" name="Start_Times" class="col-xs-12 col-sm-11" value="<?php echo $Preferred_Start_Time; ?>"/>
<input type="text" id="End_Times[]" name="End_Times" class="col-xs-12 col-sm-11" value="<?php echo $Preferred_End_Time; ?>"/>

My jQuery function:

/*$("#test").click(function() {
    $("#validation-form").ajaxForm({url: 'schedule/schedule_opportunity', type: 'post'});
    var form_data = {
        Opportunity_Id: $('#Opportunity_Id').val(),
        Class_Number: $('#Class_Numbers').val(),
        Class_Date: $('#Class_Dates').val(),
        Start_Time: $('#Start_Times').val(),
        End_Time: $('#End_Times').val(),
        ajax: '1'
    };

    $.ajax({
        url: "<?php echo site_url('schedule/schedule_opportunity'); ?>",
        type: 'POST',
        data: form_data,
        dataType: 'json',
        cache: false
    });

    return false;
})*/

$('#validation-form').submit(function(e){
    e.preventDefault();
    var form = $(this);

    $.ajax({
        url: "<?php echo site_url('schedule/update_classes'); ?>",
        method: form.prop('method'),
        data: $('#validation-form').serialize(),
        success: function(){
            alert('Success');
        }
    });

})

My controller:

function update_classes() {
    $Opportunity_Id = $this->input->post('Opportunity_Id');
    $Class_Numbers = $this->input->post('Class_Numbers');
    $Class_Dates = $this->input->post('Class_Dates');
    $Start_Times = $this->input->post('Start_Times');
    $End_Times = $this->input->post('End_Times');
    $this->ion_auth_model->update_classes($Opportunity_Id, $Class_Numbers, $Class_Dates, $Start_Times, $End_Times);
}

My model:

function update_classes($Opportunity_Id, $Class_Numbers, $Class_Dates, $Start_Times, $End_Times) {
    foreach($Class_Numbers as $key => $Class_Number) {
        //$Opportunity_Id = $Opportunity_Id[$key];
        $Class_Date = $Class_Dates[$key];
        $Start_Time = $Start_Times[$key];
        $End_Time = $End_Times[$key];

        $Class = array(
            'Class_Date' => $Class_Date,
            'Start_Time' => $Start_Time,
            'End_Time' => $End_Time
        );

        $this->db->where('Opportunity_Id', $Opportunity_Id);
        $this->db->where('Class_Number', $Class_Number);
        $this->db->update('Classes', $Class);
    }
}

1 Answer 1

1

Well, I think it's time for you to do some debugging. What's very unclear to me is how you plan to get multiple values for all the fields from the front-end, but that being said, these are the problems in your code that I see at this point:

  1. Opportunity_Id: $('#Opportunity_Id').val() => I see no field with the ID "Opportunity_Id". (Fixed by question edit)
  2. Class_Number: $('#Class_Numbers').val(), etc. => There is no field with the ID "Class_Numbers". There is a "Class_Numbers[]", but that will not be returned in your lookup. Same goes for all the other fields.
  3. You seem to be under the impression that JQuery will automatically create some sort of array from your textbox values and pass them to the server. That's not going to happen.
Sign up to request clarification or add additional context in comments.

6 Comments

I left Opportunity_Id out by mistake in my above post. I have since updated. Hmm, I understand. I had done something very similar to this just using a regular form submit in the past and thought I could do the same using jquery. Thanks
For a regular form submit, this will indeed work. You can do a regular form submit over AJAX with JQuery though. It doesn't have to be JSON.
I see. I have updated my post with the new function I am using to do just that. Still getting the same result. I get a Success alert and no errors.
Had to make a small change to my model function and it's not working! I updated my post to reflect the working code. Thanks!
Great news! It would have probably been better to not change the contents of your question, and add your modified code at the bottom. Now the original problem description is no longer there for people that come across this post in the future.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.