Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php变量中存储按钮id_Php_Jquery_Ajax - Fatal编程技术网

在php变量中存储按钮id

在php变量中存储按钮id,php,jquery,ajax,Php,Jquery,Ajax,我按照建议对代码进行了更改。以下是我的更新代码: <form action="" method="POST"> <div class="btn-group btn-group-justified" role="group" aria-label="..."> <div class="btn-group" role="group"> <input type="submit" clas

我按照建议对代码进行了更改。以下是我的更新代码:

<form  action="" method="POST">
        <div class="btn-group btn-group-justified" role="group" aria-label="...">
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$country?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$state?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$city?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$user_name?>">
            </div>
        </div>
        </form>
<?php
if(isset($_POST['navbtnplace'])) {
                    echo  $_POST['navbtnplace'];

                }
                ?>


PHP无法将html的id捕获为表单数据。如果您想捕获html的id并将其插入变量,则必须执行一个简单的技巧,插入与id相同的按钮值。因此,您的按钮应如下所示:

<form method="post">
    <input type="submit" id="a" name="idVal" value="a" class"navbtnplace">
    <input type="submit" id="b" name="idVal" value="b" class"navbtnplace">
    <input type="submit" id="c" name="idVal" value="c" class"navbtnplace">
    <input type="submit" id="d" name="idVal" value="d" class"navbtnplace">
</form>

<?php

if(isset($_REQUEST['idVal'])) {
    ext($_REQUEST['idVal']);
}

function ext($id) {
   echo $id;
}


PHP只接受表单数据-无论
value
属性中有什么,因此在PHP代码中都无法获得
id
值。 如果要检查按下了哪个按钮,只能使用
value
属性进行检查

test.html

<html>
    <body>
        <form action="index.php" method="POST">
            <!-- other form fields -->
            <input type="button" id="a" name="idVal" value="a" class"navbtnplace">
            <input type="button" id="b" name="idVal" value="b" class"navbtnplace">
            <input type="button" id="c" name="idVal" value="c" class"navbtnplace">
            <input type="button" id="d" name="idVal" value="d" class"navbtnplace">
        </form>
    </body>
</html>

index.php

<?PHP
    echo $_POST['idVal'];
?>


注意:如果发送AJAX请求来存储值,然后(单独)提交表单,则会丢失第一个值,因为这两个是独立的请求。但是,会话和COOKIE将在这种情况下工作。一旦通过AJAX或正常表单提交将值保存在会话中,它将被全局保存,您可以在刷新页面后访问保存的值。

无需切换,只需使用表单发送的值调用
extact()
函数即可

<?php
if(isset($_REQUEST['idVal'])) {
    $navBtnVal = $_REQUEST['idVal'];
    extract($navBtnVal);
}

function extract($id) {
    //some event
}
?>

<!-- Change action attribute to some other page if you want to send data to somewhere else.  -->
<!-- You also missed equal operator(=) in class -->
<!-- Give an extra attribute value and put its value as like id as below. Because php will receive your value attribute's data-->
<form method="post" action="">
    <input type="button" id="a" name="idVal" value="a" class="navbtnplace" />
    <input type="button" id="b" name="idVal" value="b" class="navbtnplace" />
    <input type="button" id="c" name="idVal" value="c" class="navbtnplace" />
    <input type="button" id="d" name="idVal" value="d" class="navbtnplace" />
</form>

您能解释一下extract在做什么吗?它是否
echo
something,它是否需要访问HTML文档(在这种情况下,您不应该使用PHP执行此操作),它是否检索一些应该在浏览器中输出的数据?PHP在服务器上运行,并将HTML传递给用户的浏览器。当他们在浏览器中单击时,PHP已经运行。JavaScript-ajax请求是路径go,因为它向服务器发出另一个请求,您可以通过该请求运行更多PHP。