【问题标题】:Passing input value via button click to php function in script通过按钮单击将输入值传递给脚本中的php函数
【发布时间】:2019-10-01 00:27:16
【问题描述】:

我需要使用输入字段中的值 - 没有 POST - 从外部源进行查找。

输入字段有一个与之关联的按钮,它在按钮按下时调用脚本。所以我需要将输入字段(id = 'lookup')放入函数的参数中(该函数在脚本中被调用)。

这是脚本中需要输入字段(称为“查找”的输入字段)中的值的代码行

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

我的代码如下。

(注意:我知道'PHP是服务器端,JS是客户端'。但是按钮点击确实调用PHP函数,并且代码确实显示函数的返回值如果你可以这样做:

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

这将通过我的api_request() 函数返回“我的产品名称”。)

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;
}

【问题讨论】:

  • 当你在调用中加入一个常量时,例如api_request('1234') 在页面加载时可以(并且)评估该值。单击按钮时不会对其进行评估。查看您的页面源代码,您将在文本中看到var product_name = &lt;some constant value&gt;。您只能在页面加载后通过 ajax 调用等访问 PHP 函数。

标签: javascript php jquery


【解决方案1】:

由于您已经了解 PHP 是服务器,JS 是客户端,所以您不能直接在 PHP 中使用 JS 变量。

有两种方法可以。

  1. $_POST 您已经说过不想使用。我认为不使用 $_POST 的原因是浏览器提示继续刷新页面。

  2. 使用 $_GET 将在您的网址中添加一个参数。

<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>

另外在文件开头添加这一小行以作为 PHP 变量访问。

$lookup = $_GET["lookup"]; 

【讨论】:

  • 请注意,问题指出输入字段没有POST,它不在FORM标签中。只有单击按钮“提交”表单。所以我的理解是没有使用 POST 或 GET 值。输入字段值需要作为 apirequest() 函数的参数发送。
  • 再次因为 PHP 是服务器代码,它需要将客户端的值传递给服务器。 $_GET 和 $_POST 是您可以将数据发送回 PHP 代码的方法。另一种方法是创建一个端点来执行一个更加复杂的 PHP 代码。
  • - 也许用 jquery 设置一个 cookie,然后在 jquery 函数内的 php 代码中使用 $_COOKIE?我四处搜索并找到了用 jquery 设置 cookie 的各种方法,但没有一个有效。而且我需要允许不允许使用 cookie 的访问者。那么 - 也许是一种在 jquery 函数中设置 PHP 会话变量的方法?
  • 您不能使用 jQuery 执行 PHP 操作。它必须是服务器调用。
  • 实际上,如果您在 $product_name = api_request("xxxx"); 中输入任何值(字符串),上面的代码就可以工作。 (至少它在我上次测试时做到了。如果我在 JS 中创建一个 cookie,然后在 PHP 函数中使用该 cookie,它似乎可以工作,尽管不一致。在任何一种情况下,我似乎都可以调用脚本代码中的php函数。
【解决方案2】:

ajax 是可能的。

试试这个

创建文件: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 ));

并用这个修改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");

});

});

【讨论】:

  • 问题不在于调用 api_request 函数——它工作得很好——而是将“查找”值(输入字段的值)作为函数参数提供给 api_request 函数。请注意,输入字段没有使用表单,因此(我认为)没有要使用的 $_GET 或 $_POST 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2016-12-23
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多