M
M
Mikhail Belyakov2018-02-07 15:04:48
PHP
Mikhail Belyakov, 2018-02-07 15:04:48

Checkbox without a button, how to do it right?

Good afternoon everyone, the essence of the question is this. There is a Page with the output of articles, each article has its own checkbox, when you click on the enable or disable buttons, the status value in the table changes to 1 or 0.
Now the question itself is how to implement such functionality without buttons using ajax. In order to be so, if the status value in the database is 1, the checkmark is, if 0, respectively, no. also, when you click on the checkmark, the value in the database changed to true or vice versa. that is, to do all the same functionality without buttons. in js generally zero unfortunately. I will be grateful for help. Below is the current code

<?php foreach ($posts as $post) { ?>
<tr>
 <td style="text-align: center;">
<input type="checkbox" name="selected[]" value="<?= $post['post_id']; ?>"/> 
</td>
</tr>
......



<div class="buttons">
                        <a class="button">
                            <button id="but" formaction="<?=$turnon;?>">Включить</button>
                        </a>
                        <a class="button">
                            <button id="but" formaction="<?=$turnoff;?>">Отключить</button>
                        </a>
 </div>


public function turnoff()
    {
        $this->load->model('instagram/instagram');
        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $post_id) {
                $this->model_instagram_instagram->turnoff($post_id);
            }

            $this->session->data['success'] = "Пост выключен!";

        }
        $this->redirect($this->url->link('instagram/instagram', 'token=' . $this->session->data['token'], 'SSL'));

    }

    public function turnon()
    {
        $this->load->model('instagram/instagram');
        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $post_id) {
                $this->model_instagram_instagram->turnon($post_id);
            }

            $this->session->data['success'] ="Пост включен!";
        }
        $this->redirect($this->url->link('instagram/instagram', 'token=' . $this->session->data['token'], 'SSL'));
    }

public function turnoff($post_id)
    {
        $this->db->query("UPDATE " . DB_PREFIX . "instpost SET status = '0' WHERE  post_id= '"
            . $post_id . "'");
    }

    public function turnon($post_id)
    {
        $this->db->query("UPDATE " . DB_PREFIX . "instpost SET status = '1' WHERE  post_id= '"
            . $post_id . "'");
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Belka2007, 2018-02-15
@Belka2007

Watch a couple of videos and everything will become clear.
https://www.youtube.com/results?search_query=jquer...
In short:
1. Add one button with id (not necessarily a checkbox)
2. Bind an event to click on the button using jquery
3. Send ajax when the event is executed request to the php page
4. In php, make one handler that checks the current value of the record in the database and changes it to the opposite (If 1 then to 0, if 0 then to 1).
5. In jquery, when receiving a success response from an ajax request, we change the text (and / or css class) in the button.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question