0

I need some help getting the value of a submit button. The code below fires off my controller function, but I am unable to get the value of the 'Invite' button. var_dump states 'bool(false)' and a 0 for Educator_Id is inserted into my final query to the database.

Thanks for any help you can give!

My submit button:

<?php foreach($educators as $educator): ?>
    <button type="submit" id="Invite" name="Invite" value="<?php echo $educator->Educator_Id; ?>">Invite</button>
<?php endforeach; ?>

My jQuery function:

$("#Invite").click(function() {
    var form_data = $('#validation-form').serialize();

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

    return false;
})

My controller:

function send_invite() {
    $email = $this->input->post('Educator_Email');
    $Opportunity_Id = $this->input->post('Opportunity_Id');
    $Educator_Id = $this->input->post('Invite');
    $Class_Numbers = $this->input->post('Class_Numbers');

    foreach($Class_Numbers as $Class_Number):
        $this->ion_auth_model->update_class_educator($Opportunity_Id, $Class_Number, $Educator_Id);
    endforeach;
}

My model:

function update_class_educator($Opportunity_Id, $Educator_Class, $Educator_Id) {
    $Class = array(
        'Educator_Id' => $Educator_Id
    );

    $this->db->where('Opportunity_Id', $Opportunity_Id);
    $this->db->where('Class_Number', $Educator_Class);
    $this->db->update('Classes', $Class);
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Derek Morgan
  • 165
  • 1
  • 2
  • 16
  • You couldn't narrow it down to either the PHP or JavaScript? That should be easy enough to do. – John Conde Sep 12 '14 at 18:58
  • @JohnConde Seems to be a PHP issue to me. – Derek Morgan Sep 12 '14 at 19:00
  • 1
    according to this answer - http://stackoverflow.com/a/9866762/689579 - jquery `.serialize()` does not encode buttons. – Sean Sep 12 '14 at 19:08
  • 2
    http://api.jquery.com/serialize/ says "Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button." – Jonathan Kuhn Sep 12 '14 at 19:08
  • do a `console.debug` of `form_data` – Francisco Corrales Morales Sep 12 '14 at 19:09
  • From reading the docs, you could likely include the selector from the clicked button and it would include it. so `$('#validation-form, #Invite').serialize();`. Only other thing I would suggest is give each button a unique id because they are supposed to be unique. – Jonathan Kuhn Sep 12 '14 at 19:13
  • `id`s should be unique, but since you are creating these in a loop -> `foreach($educators as $educator):` you will end up with `n` `id="Invite"`, so when using `$("#Invite").click(function(){` how will jquery know which button value you want to include? – Sean Sep 12 '14 at 19:16
  • Turn the buttons into checkboxes – Dale Sep 13 '14 at 07:06

3 Answers3

0

Try to passing the value to a hidden input

JavaScript:

$("#Invite").live("click", function(){
  var this_value = $(this).val();
  $("input[name='InviteValue']").val(this_value);
});
p1errot
  • 41
  • 1
  • 4
0

Instead of using button use input like this :

<?php 
  $count = 0;
  foreach($educators as $educator): ?>
     <input type="submit" id="Invite_$count" class="invite" name="Invite" data-value="<?php echo $educator->Educator_Id; ?>" value="Invite"/>
<?php
  $count++;
  endforeach; ?>

And in jquery you can use :

$(".invite").click(function() {
    var submit_data = $(this).data('value');
    var form_data = $('#validation-form').serialize();

    $.ajax({
        url: "<?php echo site_url('schedule/send_invite'); ?>",
        type: 'POST',
        data: form_data + "&submit_data=" + submit_data
    });

    return false;
})

In Controller:

function send_invite() {
    $email = $this->input->post('Educator_Email');
    $Opportunity_Id = $this->input->post('Opportunity_Id');
    $Educator_Id = $this->input->post('submit_data');
    $Class_Numbers = $this->input->post('Class_Numbers');

    foreach($Class_Numbers as $Class_Number):
        $this->ion_auth_model->update_class_educator($Opportunity_Id, $Class_Number, $Educator_Id);
    endforeach;
}

Try it. This might solve your problem.

KCP
  • 929
  • 9
  • 29
  • Thanks for the response. Still getting the same result though. – Derek Morgan Sep 12 '14 at 19:34
  • Oh sorry, there was a mistake in my solution. I didnot noticed the for loop and used the same "id" in the loop. I am correcting it by adding a class and then it will definitely work. – KCP Sep 15 '14 at 15:37
0

Try this for the PHP:

 <?php
    foreach($educators as $educator){ 
        echo "<input type='checkbox' name='invite[]' value='".$educator->Educator_Id."'>".$educator."<br/>";
    } 
    echo "<input type='submit' value="Invite"/>";

It will render a checkbox for each educator and a SINGLE submit button. Then, in your JS:

$("#invite").click(function() {
var form_data = $('#validation-form').serialize();

$.ajax({
    url: "<?php echo site_url('schedule/send_invite'); ?>",
    type: 'POST',
    data: form_data,
    success: function(){
        // Do something here if succeded
    }
});

return false;

});

This way it only sends checked checkboxes to the server, so you can handle who is selected in your controller from there.

$Educator_Id = $this->input->post("invite[]");

instead of

$Educator_Id = $this->input->post('Invite');

I think that should work... Your final goal is a little unclear to me, but your logic is what's off in this case, and this should help clear things up.

Cheers!

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102