Samstag, 13 September 2025

Top 5 diese Woche

Ähnliche Tutorials

Wechselkurs FX

Dieser Codeschnipsel ermöglicht die dynamische Währungsumrechnung per AJAX.

<?php
if (isset($_GET['amount'], $_GET['from'], $_GET['to'])) {
    header('Content-Type: application/json; charset=UTF-8');
    $amount = floatval($_GET['amount']);
    $from   = rawurlencode(strtoupper($_GET['from']));
    $to     = rawurlencode(strtoupper($_GET['to']));
    // Exchangerate.host Konvertierungs-Endpoint
    $url = "https://api.exchangerate.host/convert"
         . "?from={$from}&to={$to}&amount={$amount}";
    $response = @file_get_contents($url);
    if ($response === false) {
        http_response_code(502);
        echo json_encode(['error' => 'API-Fehler']);
        exit;
    }
    echo $response;
    exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <title>Einfacher Währungsrechner</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 2rem; }
    input, select, button { padding: .5rem; margin-right: .5rem; }
    #result { margin-top: 1rem; font-size: 1.2rem; }
  </style>
</head>
<body>
  <h1>Währungsrechner</h1>
  <form id="fxForm">
    <input
      type="number"
      id="amount"
      placeholder="Betrag"
      step="0.01"
      required>
    <select id="from">
      <option value="EUR">EUR</option>
      <option value="USD">USD</option>
      <option value="GBP">GBP</option>
      <option value="JPY">JPY</option>
      <option value="CHF">CHF</option>
    </select>
    →  
    <select id="to">
      <option value="USD">USD</option>
      <option value="EUR">EUR</option>
      <option value="GBP">GBP</option>
      <option value="JPY">JPY</option>
      <option value="CHF">CHF</option>
    </select>
    <button type="submit">Umrechnen</button>
  </form>
  <div id="result"></div>
  <script>
    document.getElementById('fxForm').addEventListener('submit', function(e) {
      e.preventDefault();
      const amount = document.getElementById('amount').value;
      const from   = document.getElementById('from').value;
      const to     = document.getElementById('to').value;
      const result = document.getElementById('result');
      result.textContent = 'Lädt…';
      fetch(`?amount=${encodeURIComponent(amount)}&from=${from}&to=${to}`)
        .then(res => res.json())
        .then(data => {
          if (data.error || typeof data.result === 'undefined') {
            result.textContent = 'Fehler bei der Umrechnung';
            return;
          }
          result.innerHTML = `
            <strong>${amount} ${from}</strong> = 
            <strong>${data.result.toFixed(2)} ${to}</strong><br>
            (Kurs: 1 ${from} = ${data.info.rate.toFixed(4)} ${to})
          `;
        })
        .catch(err => {
          console.error(err);
          result.textContent = 'Ein unerwarteter Fehler ist aufgetreten.';
        });
    });
  </script>
</body>
</html>
Vorheriges Tutorial

Hier etwas für dich dabei?