简体   繁体   English

通过按钮单击将输入值传递给脚本中的 php function

[英]Passing input value via button click to php function in script

I need to use the value from an input field - without a POST - to do a lookup from an external source.我需要使用输入字段中的值 - 没有 POST - 从外部源进行查找。

The input field has a button associated with it, which calls the script on button press.输入字段有一个与之关联的按钮,它在按钮按下时调用脚本。 So I need to put the input field (id = 'lookup') into the function's parameter (the function is called within the script).所以我需要将输入字段(id = 'lookup')放入函数的参数中(在脚本中调用 function)。

This is the code line in the script that needs the value from the input field (input field called 'lookup')这是脚本中需要输入字段(称为“查找”的输入字段)中的值的代码行

<?php $product_name = api_request(lookup);?>

My code is below.我的代码如下。

(NB: I know about 'PHP is server side, JS is client site'. But the button click does call the PHP function, and the code does display the return value of the function if you do this instead: (注意:我知道“PHP 是服务器端,JS 是客户端站点”。但是单击按钮确实调用了 PHP function,并且代码确实显示了返回值而不是 ZC1C425268E68385D1AB50441

<?php $product_name = api_request('1234');?>

which will return "My Product Name" via my api_request() function.)这将通过我的api_request() function 返回“我的产品名称”。)

What is needed to put a script variable in the api_request() 's parameter?将脚本变量放入api_request()的参数中需要什么?

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>
<p>Part Number = <input type="text" id="lookup" value= "12345"></p>
<button>Get product name for this part number</button>
<div id='product_name'>Product Name Goes Here </div>

<script>
$(document).ready(function(){
    $("button").click(function(){ 
        // returns value of input field 
        var lookup = document.getElementById('lookup'); 
        // need to put the input field 'lookup' into the function's parameter
        <?php $product_name = api_request(lookup);?>
        var product_name = "<?php echo $product_name; ?>";
    $('#product_name').text(product_name); // display it on the screen in that div ID

    });
});
</script>
</body>
</html>
<?php 

function api_request($partnumber) {
    // code that returns the product name for that part number, hard coded here
    $product_name = "My Product Name";
return $product_name;
}

As you already understand PHP is server and JS is client, you cannot directly use JS variables in PHP.正如您已经了解 PHP 是服务器,JS 是客户端,您不能直接在 PHP 中使用 JS 变量。

There are two ways by which you can.有两种方法可以。

  1. $_POST which you already said you don't want to use.您已经说过不想使用的 $_POST 。 I assume the reason for not using $_POST is that browser prompts to continue refreshing page.我认为不使用 $_POST 的原因是浏览器提示继续刷新页面。

  2. Use $_GET which will add a param in your url.使用 $_GET 它将在您的 url 中添加一个参数。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="get"> <p>Part Number = <input type="text" id="lookup" name="lookup" value= "12345"></p> <button>Get product name for this part number</button> </form>

Additionally add this small line in the beginning of your file to access is as a PHP variable.此外,在文件开头添加此小行以作为 PHP 变量访问。

$lookup = $_GET["lookup"]; 

ajax is possible. ajax 是可能的。

try this尝试这个

create file: api_request.php创建文件:api_request.php

<?php 
function api_request($partnumber) {
// code that returns the product name for that part number, hard coded here
$product_name = "My Product Name";
return $product_name;
}
$number = $_GET["part"];
$product_name = api_request($number);
echo json_encode(array( "product_name" => $product_name ));

And Modify the javascript with this并用这个修改 javascript

$(document).ready(function(){
$("button").click(function(){ 
    // returns value of input field 
    var lookup = document.getElementById('lookup'); 
    // need to put the input field 'lookup' into the function's parameter
    $.get(`api_request.php?part=${ lookup.value }`,(resp)=>{
$('#product_name').text(resp.product_name); // display it on the screen in that 
},"json");

});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM