如何在php表单变量中获取元素属性值?

时间:2016-12-11 17:04:56

标签: javascript php jquery html

我试图在运行时将我的html中的元素属性转换为我的php表单。这是元素。

<input type="submit" id="myButtonId" name="uploadpublic" class="uploadbutton btn btn-success btn-sm" value="Upload"/>

现在我需要的是将这个按钮的id输入到我的phpform中。类似的东西:

if(isset($_POST['uploadpublic']) && $_POST['uploadpublic'] == 'Upload') {
//get the id of the button and store it to a variable
}

我有办法以这种方式工作吗?

3 个答案:

答案 0 :(得分:2)

一种解决方法是将id设置为等于名称:

<button type="submit" name="buttonId" id="buttonId" value="value">Submit</button>

然后你可以抓住它:

$_POST['buttonID'].

答案 1 :(得分:0)

您感到困惑,因为您尝试从服务器端获取元素的属性(即客户端),并且服务器不知道此信息。 在客户端,您使用JavaScript,您可以做的是将此值存储在cookie中。然后使用PHP从服务器端获取此值。

以下代码由http://abarcarodriguez.com/365/show?e=10

提供
// mini-jQuery
var $ = function (id) { return document.getElementById(id); };

// Caché
$set = $('set');
$read = $('read');
$delete = $('delete');
$logs = $('logs');

// Logs en textarea
var log = function (log) { $logs.value = log + '\n' + $logs.value; }

// Crear Cookie
var crearCookie = function (key, value) {
    expires = new Date();
    expires.setTime(expires.getTime() + 31536000000);
    cookie = key + "=" + value + ";expires=" + expires.toUTCString();
    log("crearCookie: " + cookie);
    return document.cookie = cookie;
}

// Leer Cookie
var leerCookie = function (key) {
    keyValue = document.cookie.match("(^|;) ?" + key + "=([^;]*)(;|$)");
    if (keyValue) {
        log("getCookie: " + key + "=" + keyValue[2]);
        return keyValue[2];
    } else {
        log("getCookie: " + key + "=" + "null");
        return null;
    }
}

// Eliminar Cookie
var eliminarCookie = function (key) {
    log("eliminarCookie: " + key);
    return document.cookie = key + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

// Botones para Demo
$set.onclick = function () {
    key = $('key').value;
    value = $('value').value;
    crearCookie(key, value);
}
$read.onclick = function () {
    key = $('key-read').value;
    leerCookie(key);
}
$delete.onclick = function () {
    key = $('key-delete').value;
    eliminarCookie(key);
}

以及获取cookie值的PHP代码:

$_COOKIE["nombre"])

请阅读:http://php.net/manual/en/reserved.variables.cookies.php

答案 2 :(得分:0)

嗨Neha我会举个例子..

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
    <label for="file">Upload a file</label>
    <input type="file" id="file" name="file"/>
    <input type="submit" name="upload" value="Upload"/>
</form>

<?php
    if (isset($_POST['upload'])) {
        echo $_POST['file'];
    }   
 ?>
相关问题