[英]Call external api via input field data
Assume a 'part number' input field that is not in a form tag.假设“部件号”输入字段不在表单标签中。 When the user enters a value, the onblur or a button next to the input field should query an external site via that site's API, and return the 'product name' value.当用户输入一个值时,输入字段旁边的 onblur 或按钮应通过该站点的 API 查询外部站点,并返回“产品名称”值。
I have the code that will use the API to return the value.我有将使用 API 来返回值的代码。 But how to structure the input field so that it calls the API code?但是如何构造输入字段以使其调用 API 代码?
I think I need an AJAX call, but still a noob on Ajax stuff.我想我需要一个 AJAX 电话,但仍然是 Ajax 的菜鸟。 What code would be used to process the string in the input field.将使用什么代码来处理输入字段中的字符串。 (Note that there is no form tag associated with the input field.) (I primarily use PHP.) (请注意,没有与输入字段关联的表单标签。)(我主要使用 PHP。)
In response to the question and subsequent comments, conceptually it would work as follows then.针对问题和随后的评论,从概念上讲,它将按如下方式工作。
The HTML Markup: HTML 标记:
<input type="text" id="userInput"> <!--Text input of product id from user-->
<button id="btn">Get Product Name</button> <!--Button to invoke API request-->
<p id="productName"></p> <!--Element used to display product name result from API-->
The Javascript: Javascript:
//Attach an event listener to the button.
document.getElementById("btn").addEventListener("click", function(){
//Get the id of product the user entered
var productId = document.getElementById("userInput").value;
//pass productId to the Amazon API
//receive product name from API
//Display product name received
document.getElementById("productName").innerHTML = //The product name received from API
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.