Input type image does not send the form?

Asked

Viewed 1,018 times

2

I’m having a problem to give Ubmit with input type image, it simply is not recognized as Submit and does not send the data to be processed by POST.

Below are the forms that did not work.

Trying to send with input id:

<input type="image" name="botao" id="buttonSubmit" onClick="document.getElementById('buttonSubmit').submit();" src="css/img/buttonImg.png" style="width:223px;height:41px"/>

Taking the form and trying to give Submit

<input type="image" name="botao" id="buttonSubmit" onClick="document.getElementById('formulario').submit();" src="css/img/buttonImg.png" style="width:223px;height:41px"/>

Normal without js:

<input  type="image" name="botao" id="buttonSubmit" src="css/img/buttonImg.png" style="width:223px;height:41px" />

I’ve tried every way I’ve seen on the net but none of it worked, so I’m coming to you!

How it worked out:

<input type="submit" value="Cadastrar" id="buttonSubmit" name="botao"/>

The way above works perfectly... but I want to use an image like Ubmit... and I’m not getting at all!

My complete code:

<?php
require ("includes/connection.php");
?>
    <!DOCTYPE html>
    <html>
       <head>
          <meta charset="UTF-8">
          <title> KNAUTILUZ </title>
       </head>
       <body>
          <link href="css/main-index.css" rel="stylesheet">
          <img src="css/img/backmenu.png" alt="" title="" id="field0"/>
          <script type="text/javascript" src="js/pegarValor.js"></script>
          <img src="css/img/gameLogo.png" alt="" title="" id="gameLogo"/>
          <div id="text" >Knautiluz é um jogo online de navegador (browser).<br>Trazendo uma proposta inovadora de um RPG de mesa.<br> Cada jogador pode ser o que desejar, tudo depende de <br> sua imaginação.</div>
          <div id="upperMenu"></div>
          <form name="formulario" id="formulario" action="" method="post">
             <label id="nomediv">Nome</label>
             <input type="text" required placeholder="SEU NOME" name="name" id="name" pattern="[A-z]{3,}" maxlength="12">
             <br>
             <label id="sobrenomediv">Sobrenome</label>
             <input type="text" required placeholder="SOBRENOME" name="surname" id="surname" pattern="[A-z]{3,}" maxlength="12">
             <br>
             <label id="nascimentodiv">Nascimento</label>
             <input type="date" required  name="birthday" id="birthday" min="1915-01-01" max="2006-01-01"> 
             <br>
             <label id="sexdiv">Sexo</label>
             <select name="sex" id="sex">
                <option value="Male">MASCULINO</option>
                <option value="Female">FEMININO</option>
             </select>
             <br>
             <label id="userdiv">Usuário</label>
             <input type="text" required placeholder="USUARIO" name="username" id="username" pattern="[A-z]{3,}" maxlength="15">
             <br>
             <label id="emaildiv">E-mail</label>
             <input type="email" required placeholder="[email protected]" name="email" id="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$">
             <br>
             <label id="senhadiv">Senha</label>
             <input type="password" required placeholder="SENHA" name="password" id="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="Precisa ter pelo menos uma letra maiúscula uma letra minúscula, um numero e 6 ou mais caracteres">
             <input type="hidden" name="status" id="status" value="1">
             <br>
             <label id="csenhadiv">Confirme a senha</label>
             <input id="repassword" name="repassword" type="password" required  placeholder="CONFIRME A SENHA" title="Coloque a senha igual a anterior para confirma-la" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" title="Precisa ter pelo menos uma letra maiúscula uma letra minúscula, um numero e 6 ou mais caracteres" oninput="validaSenha(this)" />
             <br>
             <br>
             <br>
             <input type="image" name="botao" id="buttonSubmit" onClick="this.form.submit();" src="css/img/buttonImg.png" style="width:223px;height:41px"/>
             <br>
             <br>
          </form>
       </body>
    </html>
    <?php
if (isset($_POST["botao"]))
{
    $name = mysqli_real_escape_string($mysqli, $_POST["name"]);
    $surname = mysqli_real_escape_string($mysqli, $_POST["surname"]);
    $birthday = mysqli_real_escape_string($mysqli, $_POST["birthday"]);
    $sex = mysqli_real_escape_string($mysqli, $_POST["sex"]);
    $username = mysqli_real_escape_string($mysqli, $_POST["username"]);
    $email = mysqli_real_escape_string($mysqli, $_POST["email"]);
    $password = mysqli_real_escape_string($mysqli, $_POST["password"]);
    $repassword = mysqli_real_escape_string($mysqli, $_POST["repassword"]);

    if ($name == "" || $surname == "" || $birthday == "" || $sex == "" || $username == "" || $email == "" || $password == "" || $repassword == "")
    {
        echo "<script> alert('Preencha todos os campos.'); </script>";
        return true;
    }
    if ($password != $repassword)
    {
        echo "<script> alert('As senhas devem ser iguais!'); </script>";
        return true;
    }
    // FAZ A BUSCA NO BDD PRA VER SE O EMAIL JA EXISTE
    $select = $mysqli->query("SELECT * FROM data WHERE email='$email'");
    if (select)
    {
        $row = $select->num_rows;
        if ($row > 0)
        {
            echo "<script> alert('Já existe um usuário com este e-mail!'); </script>";
        }
        else
        {
            // FAZ A BUSCA NO BDD PRA VER SE O USUARIO JA EXISTE
            $select = $mysqli->query("SELECT * FROM data WHERE username='$username'");
            if (select)
            {
                $row = $select->num_rows;
                if ($row > 0)
                {
                    echo "<script> alert('O Usuário já existe.'); </script>";
                }
                else
                {
                    // CASO TUDO ESTIVER OK ELE INSERE AS INFORMAÇÕES NO BDD
                    $insert = $mysqli->query("INSERT INTO `data`(`name`, `surname`, `birthday`, `sex`, `username`, `email`, `password`, `status`) VALUES ('$name', '$surname', '$birthday', '$sex', '$username', '$email', '" . md5($password) . "', '0')");
                    if ($insert)
                    {
                        echo "<script> alert('Usuario registrado com sucesso!'); location.href='login.php' </script>";
                    }
                }
            }
            else
            {
                echo $mysqli->error;
            }
        }
    }
}
?>

2 answers

3


Every case has a different problem.

When you use the input of the kind image it generates two values, of X and Y.

Example:

<form method='post'>
    <input type='image' src='/caminho/para/imagem' name='biscoito'>
</form>

This will send a POST with biscoito.x and biscoito.y.

Therefore, you are declaring an invalid condition of the type:

if(isset($_POST["biscoito"])) { }

The biscoito will never exist, but in your place there will be biscoito.x and the biscoito.y, plain as that.

In that case use the condition:

if(isset($_POST["biscoito.x"], $_POST["biscoito.y"])) { }
// OU 
if(isset($_POST)){ }

Would solve the problem.

Note:

I changed the name of botao for biscoito so that the button input with the button name, I think you can understand the problem more easily!

But and with Javascript?

So with the use of .submit() (or similar) you ignore the existence of name from the button itself, so you will not send the name which is chosen in the input `!

  • Inkeliz, I don’t quite understand... these values, x and y would have some name? Because that way it didn’t work either... what I did was create a Hidden input and put in the name the value I wanted to get through php... ai worked pretty pretty...

  • This . x and . y is inserted when it is an image type button, automatically. If the input name is "OK" you will have OK. x and OK. y

1

Put a value on the input so it’s not working I tested it here and it worked <input type="image" value="cadastrar">

Browser other questions tagged

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