Enable and disable a form edit button - with PHP

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 question is, at the end of this EDIT.PHP page, will it have a save button (where it will be redirected to a function that will make the INSERT in the base). I would like to know if with PHP I would have that, when opening this page had some validation to do with a query to the bank because when that particular user was of sex = M, for example, did not enable the SAVE button, leaving view but not save.

YOU ARE BRINGING BOTH SEX WITH THE DISABLED BUTTON

<?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>

NO BANK IS THIS:

    
asked by anonymous 17.09.2015 / 21:34

3 answers

2

Instead of doing

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

else{
<input type="submit">

You can also use the ternary operator 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 it as follows.

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

The GROUP BY will group all rows that contain sexo = M

    
17.09.2015 / 22:31
4

If you agree with your query you ask if status is equal to 2.

So, just check to see if you've returned any records (> 0). If so, 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">

?>
    
17.09.2015 / 21:49
1
  

EDIT to meet the need with old version without using 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 ?> />
    
18.09.2015 / 15:12