0

大家好,我知道这个问题已经发布,但作为一个菜鸟,我无法得到答案的含义。请帮忙。我想将输入框值动态传递给 php 变量。我在这里使用 javascript 请建议是否有其他方法不使用表单提交、_GET 或 _POST。我希望它在没有任何提交的情况下动态完成。

function showHint(str)
    {
    document.getElementById('TK').innerHTML = str;
    var str = str

    }

</script>


<html>
<head>Inputbox</head>
<title>TEST PAGE </TITLE>
<body>
<input type='text' id='TK' name='TK' onkeyup='showHint(this.value)'/>
<?php
$str = var  str ;  
echo "<a href = 'newpage.php?S=$str'/>" ; ?>
</body>
</html>
4

3 回答 3

1

No. You can't. PHP is NOT a dynamic language, and it does NOT run client side. PHP runs once, and only once, and that's when the page is loaded. It runs its script and it stops. What you can do is get javascript to do an AJAX call. AJAX is basically a way of passing information to another page and getting the data, all in JavaScript. Do some research on it, but in short, you can't make PHP run once it's already been run

于 2012-08-14T01:05:27.860 回答
0
<script type="text/javascript" >
function process(){
var field1 = 'whatever';
var field2 = 'more whatever';

$.post("go.php",{field:field1,bext_field:field2},function(result){
         alert(result); 


});

};
</script>

This will alert out whatever you ECHO from GO.PHP.

You will also need a handler like:

onClick="process();"

on a div, button, image, just about anything you want to "initiate" your post

于 2012-08-14T01:07:49.013 回答
0

我想您找到的其他答案可能会说以下内容:

PHP 在用户有机会看到页面之前执行。

JS让你控制之后发生的事情。

因此,您的问题是您正在尝试使用 PHP 来做一些它根本做不到的事情。

在开发应用程序时,使用这些要点来帮助指导您的决策。在这种情况下,如果您尝试根据用户在框中键入的内容来构建链接,那么您解决问题的方法根本不是 PHP(页面已经加载,您来不及了!)--你的解决方案是 JS。

这样想:

/* 
  assumes you already have an <a> on the page.  if not, you'll 
  have to create a new <a> element dynamically.  (google "mdn createElement"
  for help)
*/
function showHint (str) {
    document.getElementById('TK').innerHTML = str;
    var link = document.getElementById('your-a-link');
    link.setAttribute('href', 'newpage.php?S=' + str);
}
于 2012-08-14T01:18:36.930 回答