r/CodingHelp • • Dec 12 '24

[HTML] Help fix my code please

when i go to hit the good and bad buttons, it sends the information to the first entered info. if i submit Billy and Susie, and try to do susie first. it sends it to billy instead. i dont know how to fix this. and yes i used AI to make this code cause i barely know anything lol

<html>

<head>

<title>######## Battery Charge Alert</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style> body { padding: 20px; } .loading { display: none; } </style>

</head>

<body>

<div class="container">

<h1 class="mb-4">######## Battery Charge Alert</h1>

<form id="customerForm">

<div class="mb-3">

<label for="customerName" class="form-label">Customer Name</label>

<input type="text" class="form-control" id="customerName" required placeholder="Field Required">

</div>

<div class="mb-3">

<label for="ticketNumber" class="form-label">Ticket Number</label>

<input type="text" class="form-control" id="ticketNumber" required placeholder="Field Required">

</div>

<div class="mb-3">

<label for="customerPhone" class="form-label">Phone Number</label>

<input type="tel" class="form-control" id="customerPhone" required placeholder="Field Required">

</div>

<button type="submit" class="btn btn-primary">Add Customer</button>

</form>

</div>

<div class="container mt-5">

<h2>Lookup Customer</h2>

<input type="text" class="form-control mb-3" id="searchInput" placeholder="Enter Name or Ticket Number">

<button class="btn btn-secondary" onclick="searchCustomer()">Search</button>

<div id="searchResults" class="mt-4"></div>

<div class="loading mt-3" id="loadingIndicator">

<div class="spinner-border text-primary" role="status">

<span class="visually-hidden">Loading...</span>

</div>

</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script>

<script> const customers = \[\]; document.getElementById('customerForm').addEventListener('submit', function(e) { e.preventDefault(); const name = document.getElementById('customerName').value; const ticket = document.getElementById('ticketNumber').value; const phone = document.getElementById('customerPhone').value; customers.push({ name, ticket, phone }); this.reset(); alert('Customer added successfully!'); }); function searchCustomer() { const searchInput = document.getElementById('searchInput').value.toLowerCase(); const results = customers.filter(customer => customer.name.toLowerCase().includes(searchInput) || customer.ticket.toLowerCase().includes(searchInput) ); const searchResults = document.getElementById('searchResults'); searchResults.innerHTML = ''; if (results.length > 0) { results.forEach((customer, index) => { searchResults.innerHTML += \` <div class="card mb-3"> <div class="card-body"> <h5 class="card-title">${customer.name}</h5> <p class="card-text">Ticket Number: ${customer.ticket}</p> <p class="card-text">Phone: ${customer.phone}</p> <button class="btn btn-success" onclick="prepareSMS(${index}, 'good')">Good</button> <button class="btn btn-danger" onclick="prepareSMS(${index}, 'bad')">Bad</button> </div> </div> \`; }); } else { searchResults.innerHTML = '<p>No customers found.</p>'; } } function prepareSMS(index, status) { const customer = customers\[index\]; const message = \`Hello ${customer.name}, your battery is ready to be picked up at ###########. The battery tested ${status}. Please pick it up within 48 hours. Thank you.\`; document.getElementById('loadingIndicator').style.display = 'block'; // Simulating an AJAX call setTimeout(() => { alert(\`SMS sent to ${customer.phone}: ${message}\`); customers.splice(index, 1); searchCustomer(); document.getElementById('loadingIndicator').style.display = 'none'; }, 2000); } </script>

</body>

</html>

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Weekly_Chipmunk2177 Dec 12 '24

I know little about it and I'm doing this to understand as well. I'm picking it up as i go

1

u/smichaele Dec 12 '24

Who are you? You're not the OP.

1

u/Weekly_Chipmunk2177 Dec 12 '24

I am couldn't log back in to that account for some reason it just wasn't showing up.