Welcome Guest, Not a member yet? Register   Sign In
[HOW] When <a > click automatically send value 1 or 0 to db
#1

[eluser]Wondering Coder[/eluser]
hi to all,


Need help again, ^_^

Im using this link <a href="#" class="check"></a>.
Now what I want is when that link is clicked it will send a value of 1 then when clicked again it will become 0 in one of my column in db. It's like switching on and off or 1 and 0.

Can anyone help me with this? please and thanks in advance. Really want to do this
coz I'll be applying it in most of my forms.
#2

[eluser]johnpeace[/eluser]
What URL does that link have as an href?

You could do something like this:
- depending on the value in the DB (0 or 1) set the value in the link to the opposite, so
- if the DB has a 1, the link will have a 0 and the inverse is also true

So, the value being passed in the URL (you know how URLs route to controllers with parameters, right?) is always the inverse of what was in the DB at page load time.
#3

[eluser]theprodigy[/eluser]
I'm assuming you don't want the link to actually go anywhere, just to update the database?

Your best bet would be to use ajax, and some link attributes (or hidden input fields)

Something similar to the following:
Code:
&lt;input type="hidden" name="db_field" id="db_field" value="1" /&gt;
<a href="#" name="switch_db_field" id="switch_db_field">Click me</a> to toggle the value
&lt;script type="text/javascript"&gt;
var the_link = document.getElementById('switch_db_field');
the_link.addEventListener('click',function (e) {
  var dbf = document.getElementById('db_field');
  var new_value;

  if(dbf.value == 1)
  {
    new_value = 0;
    dbf.value = 0;
  }
  else if(dbf.value == 0)
  {
    new_value = 1;
    dbf.value = 1;
  }
  else
  {
    alert('stop messing with my stuff!');
  }

  //do your ajax stuff here passing new_value to your php code.
  e.preventDefault(); // this line just prevents the link from actually going anywhere.
},false);
&lt;/script&gt;
#4

[eluser]Wondering Coder[/eluser]
hello again theprodigy,

yes, exactly. When a link was clicked it will not go anywhere. It will just change the value from 1 to 0 or vice versa.
Will try your code sample later.
#5

[eluser]Wondering Coder[/eluser]
theprodigy, tried your code, tell you the truth I'm not that good in javascript so I can't fill the missing arguments. So I used my own version, ^_^, pure php. I'll share my code even though you may not like it but it works for me.

controller

Code:
$item = $this->uri->segment(3)
if($item != 0)
            {
            $cond = array('user_id'=>$item);
            $query = $this->admin_db->getAdmin($cond)->row();
                if($query->display == 0)
                {
                    $admin_info = array('display' => 1);
                }
                else
                {
                    $admin_info = array('display' => 0);                    
                }
                $this->admin_db->save_admin($item,$admin_info);
                redirect('admin/view_accounts','refresh');
            }

view
Code:
<td width="10%"><a >user_id?&gt;"
                    &lt;?php if($item->display == 1) {echo "class='check'";} else { echo "class='uncheck'";}?&gt;></a></td>
#6

[eluser]theprodigy[/eluser]
Doesn't matter whether or not I like it. Tongue
As long as it works, and you're happy with it!
#7

[eluser]Wondering Coder[/eluser]
out of the topic theprodigy, do you happen to know any pdf generator.? What I want is to build a resume template in pdf. All data's are in my db. A pdf where I can create a template for my resume.
#8

[eluser]theprodigy[/eluser]
sorry, not particularly.




Theme © iAndrew 2016 - Forum software by © MyBB