如何在Laravel中通过路由参数将输入框值传递至控制器
Hey there! The issue with your current code is that the {sender_address} in your anchor tag is static—it won't automatically pull the real-time value from your input field. Let's fix this with a few straightforward steps tailored for Laravel:
First, update your routes/web.php to define a named route (this makes generating URLs more robust and maintainable):
Route::get('/sender-address/{sender_address}', [YourController::class, 'saveAddress']) ->name('sender.address.save') ->where('sender_address', '.*'); // Allow slashes/special characters in the address
The where('sender_address', '.*') clause ensures Laravel accepts addresses with slashes or special characters (you can remove it if your addresses won't include those).
We'll use JavaScript to update the anchor tag's URL with the input's value when clicked. First, add IDs to your input and anchor tag for easy DOM access:
<input type="text" id="senderAddressInput" name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid brown;" placeholder=" Enter & Save address"> <a href="#" id="saveAddressLink">Save Address</a> <script> document.getElementById('saveAddressLink').addEventListener('click', function(e) { // Prevent the default link jump behavior e.preventDefault(); // Get the input value and clean up whitespace const address = document.getElementById('senderAddressInput').value.trim(); // Basic validation: make sure the input isn't empty if (!address) { alert('Please enter an address first!'); return; } // Generate the base URL using Laravel's named route (we'll fill in the parameter dynamically) const baseUrl = "{{ route('sender.address.save', ['sender_address' => '']) }}"; // Encode the address to safely handle spaces, commas, or special characters const fullUrl = baseUrl + encodeURIComponent(address); // Redirect to the fully constructed URL window.location.href = fullUrl; }); </script>
The encodeURIComponent() function is critical here—it ensures your address doesn't break the URL structure when it contains non-standard characters.
Now, in your controller (replace YourController with your actual controller name), create the saveAddress method to receive and process the address:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class YourController extends Controller { public function saveAddress(string $sender_address) { // Optional: Add validation logic (adjust rules to match your needs) if (empty($sender_address)) { return redirect()->back()->with('error', 'Address cannot be empty!'); } // Do your desired logic here: save to database, store in session, etc. session()->put('sender_address', $sender_address); // Redirect back to the previous page with a success message return redirect()->back()->with('success', 'Address saved successfully!'); } }
If you'd rather avoid writing JavaScript, you can use a GET form instead—style the submit button to look like a regular link:
<form action="{{ route('sender.address.save') }}" method="GET"> <input type="text" name="sender_address" value="Alberta" class="form-control margin-bottom-20 hide" style="border:1px solid brown;" placeholder=" Enter & Save address"> <button type="submit" class="btn btn-link p-0">Save Address</button> </form>
This will automatically append the input value as a route parameter when submitted, and you can handle it exactly the same way in your controller.
内容的提问来源于stack exchange,提问作者Otis

