繁体   English   中英

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

[英]how to get element attribute value in php form variable?

我试图在运行时将html中的element属性转换为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
}

有什么办法可以让我这样工作吗?

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

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

然后,您可以通过以下方式捕获它:

$_POST['buttonID'].

您很困惑,因为您尝试从服务器端获取客户端的元素属性,而服务器不知道此信息。 在客户端,您可以使用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/zh/reserved.variables.cookies.php

嗨,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'];
    }   
 ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM