Enable and disable a form edit button - with PHP

Asked

Viewed 9,083 times

3

I have the following situation: I have a page (ex: EDIT.PHP) that will be where the information that was previously registered by a form (User registration form) will be edited. The point is, at the end of this EDIT.PHP page, you will have a save button (where you will be redirected to a function that will make the INSERT in the base). I wonder if with PHP I could, when opening this page had some validation to do with a query to the bank because when this particular user was sex = M, for example, did not enable the SAVE button, letting preview but not save.

IS BRINGING BOTH SEXES WITH BUTTON DISABLED

<?php
@ini_set('display_errors', '1');
error_reporting(E_ALL);

$id = $_GET["id"];
settype($id, "integer");

mysql_connect("localhost", "root", "");
mysql_select_db("banco");

$resultado = mysql_query("select * from tabela where id_tabela = $id");
$dados     = mysql_fetch_array($resultado);
if($dados["sexo"] == "M") {
    $checkedM   = "checked=\"checked\"";
    $checkedF   = "";
} else {
    $checkedM   = "";
    $checkedF   = "checked=\"checked\"";
}   
$sqlstatus = mysql_query("select * from tabela where sexo = 'M' GROUP BY sexo");
$res = mysql_num_rows($sqlstatus);



mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cadastro</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="salvar_edicao.php">
<input type="hidden" name="id" id="id" value="<?php echo $id;?>" />
  <h2 align="center"><strong>Edição de Cadastro PHP/MYSQL </strong></h2>
  <table width="390" border="1" align="center">
    <tr>
      <td width="165">Nome</td>
      <td width="209"><input name="nome" type="text" id="nome" value="<?php echo $dados["nome"];?>" /></td>
    </tr>
    <tr>
      <td>Sobrenome</td>
      <td><input name="sobrenome" type="text" id="sobrenome" value="<?php echo $dados["sobrenome"];?>" /></td>
    </tr>
    <tr>
      <td>Email</td>
      <td><input name="email" type="text" id="email" value="<?php echo $dados["email"];?>" /></td>
    </tr>    
    <tr>
      <td>Sexo</td>
      <td><input name="sexo" type="radio" value="M" <?php echo $checkedM;?> /> 
        Masculino 
        <input name="sexo" type="radio" value="F" <?php echo $checkedF;?> /> 
        Feminino </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Gravar" <?php echo $res > 0 ? 'disabled' : '' ; ?> /></td> //Com o 'disabled' na frente trás tudo desabilitado, e com ele na frente traz habilitado.
     </tr>
  </table>
</form>
</body>
</html>

IN THE BANK IT’S LIKE: inserir a descrição da imagem aqui

  • You came to give print_r($dados); die(); to see what returns?

3 answers

4

If according to your query you ask if the status is equal to 2.

So, just do a check asking if you returned any records ( > 0 ). If yes, it shows the disabled button, disabled.

<?php

    $sqlstatus = mysql_query("select status_atividade from atividades where status_atividade = 2");

    $res = mysql_num_rows($sqlstatus);

    if($res > 0)
       <input type="submit" disabled>
    else
       <input type="submit">

?>
  • 1

    Don’t need it on the button? @Diegosouza

  • I put the disabled.

  • I hadn’t seen your whole query... changed my code and I’m checking by the number of rows returned from your select.

  • Tested and error. Works if the field is a Letter instead of a number in activity = ’M' ? @Diegosouza

  • I’ve just edited the above code as per your example. See where it might be wrong. @Diegosouza

  • I recommend not using mysql_* but mysqli_* or PDO. I will give you the answer based on which you have already made the correction.

  • Naming the "table" table is the lazy people thing.

Show 2 more comments

2


Instead of doing

if($res > 0)
<input type="submit" disabled>

else{
<input type="submit">

You can also use the ternary operator by doing as follows.

$res = 2; // Exemplo de resultado
<input type="submit" <?php echo $res > 0 ? 'disabled' : ''; ?> />
// Se $res for maior que 0 será exibido disabled se não, não será exibido nada.

Your query may be returning more than 1 result, use as follows.

$sqlstatus = mysql_query("select * from tabela where sexo = 'M' GROUP BY sexo");

The GROUP BY will group all lines that have the sexo = M

  • It would work where you put $res = 2; I set a letter, type’M', ai in input as it would look?

  • I put it like this: $sqlstatus = mysql_query("select sex from table Where sex = ’M'"); $res = mysql_num_rows($sqlstatus); $res = '; E: <?php echo $res = ’M' ? disabled: '; '? > . Ta’s all disabled now. @ Rafael Acioly

  • @Tiagoib Try to use two equal signs == or even 3 to verify authenticity ===, but in this case Voce is using $res to catch a NUMERO (mysql_num_rows) line and not a string.

  • @ Rafael Acioly I adjusted the code of the above question, according to your suggestion however, is returning both options with the disabled button.

  • I put all my code as it is now. And yet it is bringing disabled both on users with sex: M or F. @ Rafael Acioly

  • @Tiagoib of course will always be disabled after using the $res to pick up the number of lines you define it as 2 deleting what was found in the bank, delete this line($res = 2), and in the tender condition that I passed you reverses the orders: <?php echo $res > 0 ? '' : 'disabled'; ?>

  • @ Rafael Acioly. I have deleted and is always disabled: $sqlstatus = mysql_query("select * from table Where sex = ’M' GROUP BY sex"); $res = mysql_num_rows($sqlstatus); and the button: <type input="Submit" name="Submit" value="Record" <?? 'disabled' : ''; ?>/>

  • As I said before, change the order to <?php echo $res > 0 ? '' : 'disabled'; ?> this ternary operator is basically a if super small and works like this: "mostre (echo)" (operacao) ($res > 0) ? "se for verdadeiro" : "se for falso" ?>

  • @ Rafael Acioly . I just put all the code at the top of the question. I did well what you said and for some reason is returning disabled. The type of data in the bank?

Show 5 more comments

1

EDITED to satisfy the need with old version without use of mysqli:

<?php

$params = array("M",1);

// query 
$sql = "select * from tabela where sexo ='{$params[0]}' and id ={$params[1]} ";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
   $permission='';
    if(count($row)){
        if ($row[0]['status'] == 2) {
           $permission = ' disabled = "disabled"';
        }
    }
?>
<input type="submit" name="Submit" value="Gravar"<?php echo $permission ?> />
  • This my work is all like Mysql connection, then it won’t work unless I change everything. Could you format this function in a simpler way, according to the oldest connection method? @Ivanferrer

  • 1

    I’ll do it here.

  • Gave this error: Parse error: syntax error, Unexpected '=' in C: wamp www cadaster-simple edit.php on line 32 . @Ivanferrer

  • The error is in the line of: permission='; @Ivanferrer

  • Thanks for warning, it was supposed to be a variable.

  • Just warning that I do not know if it will work, because there is no way I can test here, because I do not have old php here, on my server returns a message of deprecated code, and stops working.

Show 1 more comment

Browser other questions tagged

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