In your JavaScript code example:
function searchURL(){</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">window.location = "http://www.myurl.com/search/" + (input text value);
}
you need to replace (input text value) with the value of the input field. To do this, you can use the JavaScript document.getElementById('id') method to get the element by its ID and then use the .value property to get the value of the field.
function searchURL(){</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">window.location = "http://www.myurl.com/search/" + document.getElementById('searchTxt').value;
}
Another way to get the value of the input field is to use the JavaScript document.querySelector('selector') method. This method allows you to select an element by its CSS selector. In your case, you can use the following selector:
input[name="searchTxt"]<br>
The following JavaScript code uses the document.querySelector('selector') method to get the value of the input field:
function searchURL(){</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">window.location = "http://www.myurl.com/search/" + document.querySelector('input[name="searchTxt"]').value;
}
Once you have the value of the input field, you can use it to construct the URL for the search.
The above is the detailed content of How to Properly Retrieve and Use Input Field Values in JavaScript for URL Construction?. For more information, please follow other related articles on the PHP Chinese website!