如何使用jquery获取textfield的值并将其分配给php变量

时间:2016-04-06 19:52:41

标签: javascript php jquery

我有一个简单的网页,它从输入框中获取值并将其传递给下一页或表单。唯一的问题是我不知道如何使用jquery获取值并将其指定为php值。我想将此值传递到URL

这是我的代码:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<?php
    print
        "<input type='text' class='form-control' id='usr'>".
        "<a href='calculate.php?id={45}&tt='><button class='btn btn-success'>Calculate</button></a>";

?>

如何将结果值传递给"<a href='calculate.php?id={45}&tt='>,其中tt是框的值?

由于

2 个答案:

答案 0 :(得分:0)

您应该使用函数来获取值并更改按钮以使用该函数:

所以你最后的PHP代码是:

//        count                 count
// n      (after triple loop)   (by T(n) formula)
// ----   -------------------   -----------------
// 10     400                   400
// 100    70 000                70 000
// 1000   10 000 000            10 000 000

我删除了<!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <script> function calculate() { var tt = $("#usr").val(); location.href = "calculate.php?id={45}&tt=" + tt; } </script> <input type='text' class='form-control' id='usr'> <button class='btn btn-success' onclick="calculate()">Calculate</button> ,因为它只是简单的HTML。

答案 1 :(得分:0)

对于这种情况,使用jquery非常简单。

<?php
print
    "<input type='text' class='form-control' id='usr'>".
    "<a id='button' href='#'><button class='btn btn-success'>Calculate</button></a>";

?>

在这里,我已经为您的锚标记添加了id属性,或者链接了您所称的任何内容。

现在是jquery部分:

<script type="text/javascript"> 
    $(document).ready(function(){
        var usr = $("#usr").val();
        $("#button").attr("href","calculate.php?id={45}&tt="+usr);
    });
</script>

或者如果您希望在特定文本字段的值发生更改时更改网址,则可以尝试关注。

<script type="text/javascript"> 
    $(document).ready(function(){
        $("#usr").change(function(){
            var usr = $("#usr").val();
            $("#button").attr("href","calculate.php?id={45}&tt="+usr);
        });            
    });
</script>

希望这会有所帮助。如果您想了解有关jquery change事件的更多信息,那么您可以查看jQuery Change Event

**********从这里编辑************

<!DOCTYPE html>
<html>
<head>
    <title>Whatever your title is</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
    <?php
    print
        "<input type='text' class='form-control' id='usr'>".
        "<a id='button' href='#'><button class='btn btn-success'>Calculate</button></a>";

    ?>

    <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
    <script type="text/javascript"> 
        $(document).ready(function(){
            $("#usr").change(function(){
                var usr = $("#usr").val();
                $("#button").attr("href","calculate.php?id={45}&tt="+usr);
            });            
        });
    </script>
</body>
</html>

所以整个文件看起来像下面的