无需AJAX/JQuery:HTML表单+JS+PHP实现合同号关联供应商码方案问询
Got it, let's work through this problem step by step since you've got specific restrictions (no AJAX/JQuery, no PHP in JS, only form action for PHP requests). Here's a solid approach that fits all your requirements:
Since we can't use async requests, we'll split the process into two form submission steps—fully relying on PHP's form action mechanism to handle data exchange:
1. Initial Form: Collect Contract-No
First, create a simple form that only asks for the Contract-No and a NEXT button. This form submits to an intermediate PHP page (e.g., fetch-vendor.php) whose sole job is to look up the corresponding Vendor-Code from your MySQL table.
<!-- initial-form.html --> <form action="fetch-vendor.php" method="POST"> <label for="contract-no">Contract-No:</label> <input type="text" id="contract-no" name="contract-no" required> <button type="submit" name="next">NEXT</button> </form>
2. Intermediate PHP Page: Fetch Vendor-Code & Render Tracking Number Form
This PHP page will receive the submitted Contract-No, query the database for the matching Vendor-Code, then render a second form that displays the Vendor-Code and lets the user input the 3-digit suffix. This second form will finally submit to your target PHP file (e.g., final-process.php).
We'll store the Vendor-Code in a hidden input field (to pass it to the final page) while also displaying it visibly for the user to confirm.
<?php // fetch-vendor.php $vendor_code = ''; $contract_no = $_POST['contract-no'] ?? ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($contract_no)) { // Connect to your MySQL database (replace with your credentials) $db_conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_db_name'); if (!$db_conn) { die("Database connection failed: " . mysqli_connect_error()); } // Use prepared statement to prevent SQL injection $stmt = mysqli_prepare($db_conn, "SELECT `Vendor-Code` FROM your_table_name WHERE `Contract-No` = ?"); mysqli_stmt_bind_param($stmt, "s", $contract_no); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $vendor_code); mysqli_stmt_fetch($stmt); // Cleanup mysqli_stmt_close($stmt); mysqli_close($db_conn); // Handle case where no Vendor-Code is found if (empty($vendor_code)) { echo "<p>No matching Vendor-Code found for Contract-No: $contract_no</p>"; echo '<a href="initial-form.html">Go back to re-enter</a>'; exit; } } ?> <!-- Form for tracking number input --> <form action="final-process.php" method="POST"> <!-- Hidden fields to pass necessary data to final page --> <input type="hidden" name="vendor-code" value="<?php echo htmlspecialchars($vendor_code); ?>"> <input type="hidden" name="contract-no" value="<?php echo htmlspecialchars($contract_no); ?>"> <p>Tracking Number Prefix (Vendor-Code): <strong><?php echo htmlspecialchars($vendor_code); ?></strong></p> <label for="tracking-suffix">Enter 3-digit suffix:</label> <input type="text" id="tracking-suffix" name="tracking-suffix" maxlength="3" pattern="[A-Za-z0-9]{3}" required> <button type="submit">Submit Tracking Number</button> </form>
3. Final PHP Page: Process the Complete Tracking Number
In your target PHP file, you can combine the Vendor-Code and 3-digit suffix to create the full tracking number, then handle whatever business logic you need:
<?php // final-process.php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $vendor_code = $_POST['vendor-code'] ?? ''; $tracking_suffix = $_POST['tracking-suffix'] ?? ''; $contract_no = $_POST['contract-no'] ?? ''; // Generate full tracking number $full_tracking_number = $vendor_code . $tracking_suffix; // Add your business logic here (e.g., save to DB, redirect, etc.) echo "<p>Submission successful! Full Tracking Number: <strong>$full_tracking_number</strong></p>"; echo "<p>Associated Contract-No: $contract_no</p>"; } ?>
Why this fits your constraints:
- No AJAX/JQuery used—all data transfer happens via standard form submissions triggered by the
actionattribute - No PHP code embedded in JavaScript
- The final form correctly redirects to your specified PHP file
- Fully implements the flow: input Contract-No → fetch Vendor-Code → let user complete the tracking number
内容的提问来源于stack exchange,提问作者SARTHAK MEHRA

