1

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?

3
  • 2
    No. Ids are not inherently submitted as form data. You would have to either alter your form to have it as a hidden field some how or have your ajax put that into its data when it submits to have it available to the backend. In either case you have to put that information in the request. Commented Dec 11, 2016 at 17:06
  • and how do i do that? Commented Dec 11, 2016 at 17:08
  • 1
    I don't know which way you are submitting so I can't comment further. You just do it. Though to play devils advocate I should ask, in what case would the backend get a value of "Upload" for "uploadpublic" and it not originate from clicking "myButtonId"? Commented Dec 11, 2016 at 17:09

3 Answers 3

2

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'].
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.