Welcome Guest, Not a member yet? Register   Sign In
pass hidden variables relevant to the form efficiently when submitted?
#1

[eluser]livewirerules[/eluser]
Im using the below code to list of each month of a user's pending payment. so once the submit button is pressed the record for that month will be deleted from the database. i haven't set a primary key for that table because the records are being deleted and no longer used.
Code:
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
    <tr>
        &lt;?php echo form_open('confirm');?&gt;
&lt;input type="hidden" value="&lt;?php echo $month;?&gt;" name="month_&lt;?php echo $s;?&gt;" /&gt;
&lt;input type="hidden" value="&lt;?php echo $payment['ref_code'];?&gt;" name="ref_&lt;?php echo $s;?&gt;" /&gt;
        <td>&lt;?php echo $payment['ref_code'];?&gt;</td>
        <td>&lt;?php echo $month;?&gt;</td>
        <td>&lt;?php echo $payment['year'];?&gt;</td>
    <td>&lt;input type="submit" name="submit&lt;?php echo $s;?&gt;" value="MAKE PAYMENT" class="red" /&gt;&lt;/td>
       &lt;?php echo form_close();?&gt;
    </tr>
&lt;?php endforeach; ?&gt;
so in the above code when the user submits the month and ref code will be passed from the controller to the model and the relevant record will be deleted.

the problem that i am having is i have to create 12 IF statements to check if the appropriate button has been pressed.

Code:
if ($this->input->post('submit0')):
$ref = $this->input->post('ref_0');
$month= $this->input->post('month_0');

if ($this->input->post('submit1')):
$ref = $this->input->post('ref_1');
$month= $this->input->post('month_1');
....

is there an easy or more efficient way to achieve this task without repeating the code many times

#2

[eluser]pickupman[/eluser]
The other option is to concatenate the payment id with the fields value. Then when you receive the post value from the form, you can use explode to convert back.
#3

[eluser]Sagar Ratnaparkhi[/eluser]
You can also achieve it by passing value through submit button, Set background image to submit button which would say "Make payment" and pass $s in value attribute.

You can apply some css tricks to hide it. this way you don't need to loop to check submit value.
#4

[eluser]TWP Marketing[/eluser]
[quote author="livewirerules" date="1347717136"]Im using the below code to list of each month of a user's pending payment. so once the submit button is pressed the record for that month will be deleted from the database. i haven't set a primary key for that table because the records are being deleted and no longer used.
Code:
&lt;?php foreach ($payments as $s =>$payment):?&gt;
&lt;?php $month = $payment['month'];
?&gt;
    <tr>
        &lt;?php echo form_open('confirm');?&gt;
&lt;input type="hidden" value="&lt;?php echo $month;?&gt;" name="month_&lt;?php echo $s;?&gt;" /&gt;
&lt;input type="hidden" value="&lt;?php echo $payment['ref_code'];?&gt;" name="ref_&lt;?php echo $s;?&gt;" /&gt;
        <td>&lt;?php echo $payment['ref_code'];?&gt;</td>
        <td>&lt;?php echo $month;?&gt;</td>
        <td>&lt;?php echo $payment['year'];?&gt;</td>
    <td>&lt;input type="submit" name="submit&lt;?php echo $s;?&gt;" value="MAKE PAYMENT" class="red" /&gt;&lt;/td>
       &lt;?php echo form_close();?&gt;
    </tr>
&lt;?php endforeach; ?&gt;
so in the above code when the user submits the month and ref code will be passed from the controller to the model and the relevant record will be deleted.

the problem that i am having is i have to create 12 IF statements to check if the appropriate button has been pressed.

Code:
if ($this->input->post('submit0')):
$ref = $this->input->post('ref_0');
$month= $this->input->post('month_0');

if ($this->input->post('submit1')):
$ref = $this->input->post('ref_1');
$month= $this->input->post('month_1');
....

is there an easy or more efficient way to achieve this task without repeating the code many times

[/quote]

Since you have the hidden value $month passed by each form, first read that value and then use it to format your single if statement:
Code:
...
$mn = $this_input->post('month'); // get the submitted month number
if ($this->input->post('submit'.$mn))
{
$ref = $this->input->post('ref_'.$mn);
$month= $this->input->post('month_'.$mn);
}else{
// an error condition because the posted month does not match any submit button
// process as an error
}
...





Theme © iAndrew 2016 - Forum software by © MyBB