mardi 7 juin 2016

How to get values from foreach loop of php in jquery

I'm Working on a plugin in Wordpress in which i'm storing values in new table which is created on plugin activation its working perfectly.

Now I fetched values from table like following

     <tbody>
                <?php
                global $wpdb;
                $result = $wpdb->get_results("SELECT * FROM wp_ajc_images");
                foreach ($result as $row) {
                    ?>
                    <tr id="list_item">
                        <td width="12%">
                            <img src="<?php echo $row->ajc_img_path ?>" width="100">
                        </td>
                        <td width="22%">
                            <input type="text" class="img_link" value="<?php $row->ajc_img_link ?>" size="30">
                            <input type="hidden" class="ajc_field_id" value="<?php echo $row->ajc_img_id ?>">
                        </td>
                        <td width="26%">
                            <textarea rows="3" class="img_description"><?php $row->ajc_img_description ?></textarea>
                        </td>
                        <td width="22%">
                            <input type="text" class="img_title" value="<?php $row->ajc_img_title ?>" size="30">
                        </td>
                        <td width="20%">
                            <input type="submit" id="update" value="Update">
                            <button class="delete">Delete</button>
                        </td>
                    </tr>
                    <?php
                }
                ?>
</tbody>

after fetching the data I display the values in form of listing.

Now I want to update values of input fields and textarea through ajax for which I wrote following code

add_action('wp_ajax_ajc_update_values', 'ajc_update_value');
add_action('wp_ajax_nopriv_ajc_update_values', 'ajc_update_value');

add_action("admin_head", "ajc_input_ajax");
function ajc_input_ajax()
{
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function () {

            jQuery("#update").click(function () {
                var id = jQuery('.ajc_field_id').val();
                var update_link = jQuery('.img_link').val();
                var update_desc = jQuery('.img_description').val();
                var update_title = jQuery('.img_title').val();

                alert(id);

                var data = {
                    action: 'ajc_update_values',
                    ajc_id: id,
                    ajc_update_link: update_link,
                    ajc_update_desc: update_desc,
                    ajc_update_title: update_title
                };

                alert(data.toSource());

                jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (result) {
                    // alert(result);
                });
            });

        });
    </script>
    <?php
}

//Updating values in database
function ajc_update_value()
{
    $id = $_POST['ajc_id'];
    $link = $_POST['ajc_update_link'];
    $desc = $_POST['ajc_update_desc'];
    $title = $_POST['ajc_update_title'];

    global $wpdb;
    $wpdb->update(
        'wp_ajc_images',
   array(
        'ajc_img_title' => $title,
        'ajc_img_description' => $desc,
        'ajc_img_link' => $link
    ),
        array('ajc_img_id' => $id)
    )
    die();
    return true;
}

When I click on first item's update button it alerts me right id eg. (12) and update it, but when I click on rest of the item's update button it alerts me first item's id (12) and data is not updated.

Hope you'll understand my question.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire