简体   繁体   中英

how to get element attribute value in php form variable?

I am trying to get the element attribute in my html into my php form at run time. Here is the element.

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

Now what I need is to get the id of this button into my phpform. Something like :

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

Is there a way I can have it working this way?

One workaround is to set the id equal to the name:

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

Then you can catch it with:

$_POST['buttonID'].

You are confused because you try to get the attribute of the element, that is in client side, from the server side and the server doesn't know this information. On the client side you works with JavaScript and what you can do is store this value in a cookie, for example. Then get this value from the server side with PHP.

The following code is provided by: 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);
}

and the code of PHP to get the value of the cookie is:

$_COOKIE["nombre"])

read this in: http://php.net/manual/en/reserved.variables.cookies.php

Hi Neha i gonna make an example..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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