mahin993 / shopping-cart

0 stars 0 forks source link

Info update #3

Open mahin993 opened 6 months ago

mahin993 commented 6 months ago

<!DOCTYPE html>

Button App

Js:

let buttonCount = 0; const xhr = new XMLHttpRequest(); let currentButton; const createdButtons = [];

function createButton(shape) { const newButton = document.createElement('button'); newButton.className = 'button'; newButton.innerHTML = Button ${++buttonCount};

if (shape === 'circle') {
    newButton.style.borderRadius = '50%';
}

document.body.appendChild(newButton);

createdButtons.push(newButton);

newButton.addEventListener('click', function () {
    currentButton = this;
    openModal();
    displayButtonInfo();
});

}

function openModal() { const modal = document.getElementById('infoModal'); modal.style.display = 'block'; }

function closeModal() { const modal = document.getElementById('infoModal'); modal.style.display = 'none'; }

function displayButtonInfo() { const url = 'getButtonInfo.php'; const data = JSON.stringify({ buttonName: currentButton.innerHTML, });

ajaxRequest('POST', url, data, function (response) {
    if (response.success) {
        const updateInfoInput = document.getElementById('updateInfo');
        updateInfoInput.value = response.buttonInfo;
    } else {
        console.error('Error fetching button information:', response.error);
        closeModal();
    }
});

}

function saveUpdatedInfo() { const updateInfoInput = document.getElementById('updateInfo'); const updatedInfo = updateInfoInput.value;

currentButton.innerHTML = updatedInfo;

const url = 'updateButtonInfo.php';
const data = JSON.stringify({
    buttonName: currentButton.innerHTML,
    updatedInfo: updatedInfo,
});

ajaxRequest('POST', url, data, function (response) {
    if (response.success) {
        console.log('Information updated successfully');
    } else {
        console.error('Error updating button information:', response.error);
    }

    closeModal();
});

}

function closePage() { const confirmClose = confirm('Want to close this page?'); if (confirmClose) { const allButtonData = getAllButtonsData(); saveAllButtonsData(allButtonData); window.close(); } }

function getAllButtonsData() { const allButtonData = [];

for (const button of createdButtons) {
    const positionX = button.offsetLeft;
    const positionY = button.offsetTop;
    const buttonName = button.innerHTML;
    const shape = getButtonShape(button);

    allButtonData.push({
        positionX,
        positionY,
        buttonName,
        shape,
    });
}

return allButtonData;

}

function saveAllButtonsData(allButtonData) { const url = 'saveAllButtonsData.php'; const data = JSON.stringify(allButtonData);

ajaxRequest('POST', url, data, function (response) {
    if (response.success) {
        console.log('All button data saved successfully:', response);
    } else {
        console.error('Error saving all button data:', response.error);
    }
});

}

function getButtonShape(button) { return button.style.borderRadius === '50%' ? 'circle' : 'rectangle'; }

function ajaxRequest(method, url, data, callback) { xhr.open(method, url, true); xhr.setRequestHeader('Content-type', 'application/json');

xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            const response = JSON.parse(xhr.responseText);
            callback(response);
        } else {
            console.error('Error in AJAX request:', xhr.statusText);
        }
    }
};

xhr.send(data);

}

getButtonInfo.php

<?php $data = json_decode(file_get_contents('php://input'), true);

$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name";

try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$buttonName = $data['buttonName'];

$stmt = $conn->prepare("SELECT buttonInfo FROM button_data WHERE buttonName = :buttonName");
$stmt->bindParam(':buttonName', $buttonName);
$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

echo json_encode(['success' => true, 'buttonInfo' => $result['buttonInfo']]);

} catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }

$conn = null; ?>

UpdatButtonInfo:

<?php $data = json_decode(file_get_contents('php://input'), true);

$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name";

try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$buttonName = $data['buttonName'];
$updatedInfo = $data['updatedInfo'];

$stmt = $conn->prepare("UPDATE button_data SET buttonInfo = :updatedInfo WHERE buttonName = :buttonName");
$stmt->bindParam(':buttonName', $buttonName);
$stmt->bindParam(':updatedInfo', $updatedInfo);
$stmt->execute();

echo json_encode(['success' => true]);

} catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }

$conn = null; ?>

SaveAllButtonsData:

<?php $data = json_decode(file_get_contents('php://input'), true);

$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name";

try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("INSERT INTO button_data (positionX, positionY, buttonName, shape) VALUES (:positionX, :positionY, :buttonName, :shape)
                       ON DUPLICATE KEY UPDATE positionX = VALUES(positionX), positionY = VALUES(positionY), shape = VALUES(shape)");

foreach ($data as $button) {
    $stmt->bindParam(':positionX', $button['positionX']);
    $stmt->bindParam(':positionY', $button['positionY']);
    $stmt->bindParam(':buttonName', $button['buttonName']);
    $stmt->bindParam(':shape', $button['shape']);
    $stmt->execute();
}

echo json_encode(['success' => true]);

} catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }

$conn = null; ?>