Skip to content
Advertisement

How can i update a value in mysql database with php when a checkbox is checked?

In database i am updating the height of an image and after the checkbox is checked i want to see the image with that height.Any idea what should i be using to achieve this ? An example would be appreciated.

Advertisement

Answer

Okay, what you need to do is implement ajax for async call to database for updating image’s height in database and vanilla JavaScript (or jQuery) to resize image after the request is successful.

For Example:

HTML

<input type="checkbox" name="your_checkbox" value="50" onclick="changeHeight(this);">

<img src="image/url/here.jpg" alt="foo" class="your-class" id="img">

JavaScript

let changeHeight = checkbox => {
    if(checkbox.checked){
        $.ajax({
            url: 'your/path/to/updating/file.php',
            type: 'POST',
            data: {img_height: checkbox.value}
            success: res => {
                $('#img').css({height: checkbox.value+'px'});
            },
            error: err => {
                console.error(err);
            }
        });
    }
}

PHP

$height = $_POST['img_height'];

// query to update height in the database...
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement