1

I have a tiny CI form, like following:

<div class="box_txt">
<?php $attributes = array('clock_id' => $clock['id']); ?> 
<?php echo form_open('clock/start', $attributes) ?>
      <input type="submit" value="Start" />
      </form>
 </div>

And it generates a from with the folowing openning:

<form action="../clock/start" clock_id="123" method="post" accept-charset="utf-8">

I would really need to know what's the correct way to capture the clock_id in controller's function?

2
  • are you sure about clock_id attribute for html form tag? Commented Apr 15, 2016 at 8:23
  • Absolutely. That's a copy/paste from source code viewed in browser. If there is a better way to pass this single hidden value to the controler, I would be glad to know that. Commented Apr 15, 2016 at 8:24

3 Answers 3

2

Instead of putting clock_id as a form attribute, you should consider using a hidden input tag:

<input type='hidden' name='clock_id' value='<?php echo $clock["id"]; ?>'/>

then you can handle it using $this->input->post('clock_id')

Otherwise, if you really want to stick to your code, you could use jquery to get the attribute and send an ajax request:

var clock_id = $('form').attr('clock_id');

$('form').submit(function(e){
    e.preventDefault();
    var url = $(this).attr('action');
    var type = $(this).attr('method');
    var data = $(this).serialize() + '&clock_id=' + clock_id
    $.ajax({
        url: url,
        type: type,
        data: data,
        success:function(response){ /* handle response */}
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

The clock_id value on the form tag won't be sent with a form submit, as it's not a form element. Instead, you should do the following to use a hidden field:

echo form_hidden('clock_id', $clock['id']);

That will create a hidden field within your form, with the name 'clock_id' and the value, in this case, '123'

Comments

1

If you need clock_id in controller,try this code.

<div class="box_txt">

    <?php echo form_open('clock/start') ?>
         <input type="hidden" name="clock_id" value="123" />
         <input type="submit" value="Start" />
     <?php echo form_close() ?>

     </div>

Controller:

function start()
{
    $clock_id = $this->input->post('clock_id');
}

Comments

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.