How to receive data from a form via the HTML class attribute?

Asked

Viewed 2,234 times

2

I know it is possible to "call" the name of a form in a php file. But in a php file, it is possible to "call" a class made in a form to get the same effect of "calling" the name, i.e., how to send data from a form to a php file through a class ?

Specifying, to send via name I use the following php code:

$variável = $_POST['name'];

But how do I if instead of name is a class ?

OBS 1; I’ve tried to just replace the name with the class, but it didn’t work.
OBS 2; I don’t have access to the form file, so I can’t just enter a name to use the aforementioned code.

Form:

<table>
    <form name="form" method="post" action="">
        <tr>
            <td>
                <label>Email</label>
            </td>
            <td>
                <input type="text" name="email">
            </td>
        </tr>
        <tr>
            <td>
                <label>Senha</label>
            </td>
            <td>
                <input type="password" name="senha">
            </td>
        </tr>
        <input type="submit" class="botao-login" value="Login">
    </form>
</table>

Note that the login button does not have the name. I am trying to do a php function for login authentication, whose function is "isset". Namely, by pressing the button the following php code runs:

Php code

<?php
$usuario = $_POST['usuario'];
$senha   = $_POST['senha'];
$botao   = $_POST['é aqui que costumo por o name do botão'];

if (isset($botao)) {
    if ($usuario == "" or $senha == "") {
        $mensagem = "Campo em branco";
    } elseif ($usuario != $_COOKIE['usuario'] or $senha != $_COOKIE['senha']) {
        $mensagem = "Usuáriorio ou senha inválido";
    } elseif ($usuario == $_COOKIE['usuario'] && $senha == $_COOKIE['senha'] && $_COOKIE['tipo_conta'] == "administrador") {
        header("Location:php/administrador.php");
    } elseif ($usuario == $_COOKIE['usuario'] && $senha == $_COOKIE['senha'] && $_COOKIE['tipo_conta'] == "registrado") {
        header("Location:php/registrado.php");
    } elseif ($usuario == $_COOKIE['usuario'] && $senha == $_COOKIE['senha'] && $_COOKIE['tipo_conta'] == "indefinido") {
        header("Location:php/indefinido.php");
    }
}
?>

The php code is not on the same page as the form.As said before, the php code is executed by pressing the button, but for the code to work, it is possible that it has a name so that I can use the code $variável = $_POST['name'];.Is there any php code for the "isset" function to work without the need for a name ?

  • Why check if that button was clicked? This same php receives data from other forms?

  • 1

    complicated to understand, but it is not possible for PHP to see the class attribute of an HTML tag this way.

1 answer

4


The selectors class and id are attribute selectors, and are normally used to identify an element html. In common cases, these elements are used to identify specific elements to be stylized using style sheets (CSS), where the attribute id, is used to identify elements with unique properties, and the attribute class for several elements with the same properties.

The attribute name is used to reference an element in javascript, or a reference to the form, after it has been submitted. Basically they act as flags when sending a form. For example, if you have several radio with different id, and in them we place the same attribute name, during the submission of the form, only the value of the button radio selected will be passed. «1»

In the end, one comes to the conclusion that, receiving form data through attributes class or id is not possible.

You can even try a few laps using Ajax, and reading the values of the fields identified with the attribute id instead of name, but in the end, it ends up defining a method for request, so that there is exchange of data typed in the form, with the script on the server, remembering that requests to the server only accept identifiers of the type name.

Example:

<html>
<body>
<form>
<input type="text" name="nome" value="Meu nome com name"/>
<input type="text" id="nome" value="Meu nome com id"/>  
<button onclick="mostra()">Mostrar</button>
</form>
<script>
function mostra(){
    var nome = document.getElementsByName('nome')[0].value;
    var id = document.getElementById('nome').value;

    alert('atributo name: ' + nome + '\n  atributo id: ' + id);
}
</script>

</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.