Welcome Guest, Not a member yet? Register   Sign In
checking whether a form value has changed
#1

[eluser]fidlet[/eluser]
I'm new at Codeigniter (and heavy programming in general), and am trying to set up a form so that when a user changes a certain form value, and hits submit, the changed form value is checked and, if it's equal to a certain value, an email is sent. So, the logic would run roughly as follows:

1. If the user changes form value x, then note it (set a variable?)
2. When the user hits submit, and the changed value variable is set, then check wether x = value y.
3. If x = y, then send email.

I'm not sure if javascript (onChange event) is the best way to check for a changed value, or if there is something else (perhaps within Codeigniter?) that would serve this purpose better.
#2

[eluser]RiccardoC[/eluser]
For something like that I will use an hidden form field that will contain the old value
something like this view
Code:
<input type=hidden name=old_value value="<?php echo $some_value; ?>">
<input type=text name=new_value value="<?php echo $some_value; ?>">
In your controller you have only to match $this->input->post("old_value") with $this->input->post("new_value")
#3

[eluser]jvicab[/eluser]
The RiccardoC solution should be enough for the server side (after the form is submitted).
If you want to do some "before-form-submit" task, it would prety easy to do as well. In that case, you may write your code in the onsubmit event of the form and do what you need and continue with the submission process or prevent it as desired, like: (using RiccardoC HTML example adding the id tag with the same value as its name tag)
Code:
$('#formid').ready(function(){
      $('#formid').submit(function(){
         if ($('#old_value').val() !== $('#new_value').val()) {
            // do here something when they are not equal, meaning the value has changed
         } else {
            // do here something when they are equal, meaning the value hasn't changed
         }
      });
   });
// note: add return false; if you don't want to submit the form
// you need to use jquery.js for this to work!
#4

[eluser]RiccardoC[/eluser]
[quote author="jvicab" date="1334172231"]The RiccardoC solution should be enough for the server side (after the form is submitted).
If you want to do some "before-form-submit" task, it would prety easy to do as well. In that case, you may write your code in the onsubmit event of the form and do what you need and continue with the submission process or prevent it as desired, like: (using RiccardoC HTML example adding the id tag with the same value as its name tag)
Code:
$('#formid').ready(function(){
      $('#formid').submit(function(){
         if ($('#old_value').val() !== $('#new_value').val()) {
            // do here something when they are not equal, meaning the value has changed
         } else {
            // do here something when they are equal, meaning the value hasn't changed
         }
      });
   });
// note: add return false; if you don't want to submit the form
// you need to use jquery.js for this to work!
[/quote]
Perfect




Theme © iAndrew 2016 - Forum software by © MyBB