Error php when you press a button

0

Hi, I'm new to php. I have a question with the button on my page. At the moment of clicking the button, it does not show me anything, but in my code, if it is configured to see if it is pressed or not. When I use the question mark in front of the isset, if I get the click, but I do not understand prq. From what I was seeing, when it's isset .. it tells me that the button was pressed, but when it is! Isset tells me that the button was not pressed.

.boxlogin{
				border-radius:4px;
				box-shadow: 2px 2px #d6d6d6;
				margin: 75px auto;
				width: 320px;
				-webki-border-radius:4px;
				-moz-border-radius:4px;
			}
			.btn{
				margin-top: 14px;
			}	
 
<html lang="en">
<head>
	<meta charser="UTF-8">
	
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<title>Document</title>
		
	
	<link rel="stylesheet" href="css/bootstrap.min.css">
	<link rel="stylesheet" href="css/font-awesome.min.css">
	<link rel="stylesheet" href="css/estilos.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">	
	<script src="js/jquery.js"></script>
	<script src="~/js/bootstrap.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	

</head>
<body>
	<header>
</header>

<div class="jumbotron boxlogin">
		 
		<form method="POST" name="flogin" id="flogin" action="">
		<label>Usuario:</label>
		<input type="text" name="username"     class="form-control">
		<label>Contraseña:</label>
		<input type="password" name="password"    class="form-control">

		 

		<button name="btnClickI" type="button" class="btn btn-success" data-toggle="modal" data-target="#ingreso">Ingresar</button> 
		<a href="registro.php"><button value="btnClickR" type="button" class="btn btn-info">Registrar</button></a>
		 
			 
			<?php	 
			if(isset($_GET['btnClickI'])){
				if( !isset($_POST['username'])  or  !isset($_POST['password']) ) { 		        
				       echo '<div id="ingreso" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-diolag">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
									</div>
									<div class="modal-body">
										<h3>Por favor llene todo los campos!</h3>									 
									</div>
								</div>
							</div>
				       	</div>';
				}else{
				}
			} 
			?>
		</form>
	</div>

	<script type="http://code.jquery.com/jquery-latest.js"></script>
	<script src="js/bootstrap.min.js"></script>
</body>
</html>
    
asked by Luis Fernando Vargas 25.10.2017 в 01:51
source

3 answers

0

The method of the form is POST and you try to receive the variable by GET so it will never be defined.

    
answered by 25.10.2017 в 02:03
0

Try it this way:

<div class="jumbotron boxlogin">

        <form method="POST" name="flogin" id="flogin" action="">
            <label>Usuario:</label>
            <input type="text" name="username"     class="form-control">
            <label>Contraseña:</label>
            <input type="password" name="password"    class="form-control">

            <button name="btnClickI" type="button" class="btn btn-success" data-toggle="modal" data-target="#ingreso">Ingresar</button> 
            <a href="registro.php"><button value="btnClickR" type="button" class="btn btn-info">Registrar</button></a>

        </form>
    </div>

    <?php    
    if(isset($_POST['btnClickI'])){
      if( !isset($_POST['username'])  or  !isset($_POST['password']) ) {                
                           echo '<div id="ingreso" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-diolag">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        </div>
                                        <div class="modal-body">
                                            <h3>Por favor llene todo los campos!</h3>                                    
                                        </div>
                                    </div>
                                </div>
                            </div>';
        }else{
        }
    } 
    ?>
    
answered by 25.10.2017 в 06:01
0

You are mixing methods; GET and POST in the same script. Basically you tell PHP that if you find the variable btnClickI that arrived by the GET method then look for user and password by the POST method; this can not be done.

In your script I have changed your isset for a if that will check if it was GET or POST and then print a list of the variables you are sending. with this you can then know what you are looking for with isset and be sure that the info you are looking for is arriving:

.boxlogin{
				border-radius:4px;
				box-shadow: 2px 2px #d6d6d6;
				margin: 75px auto;
				width: 320px;
				-webki-border-radius:4px;
				-moz-border-radius:4px;
			}
			.btn{
				margin-top: 14px;
			}	
 
<html lang="en">
<head>
	<meta charser="UTF-8">
	
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<title>Document</title>
		
	
	<link rel="stylesheet" href="css/bootstrap.min.css">
	<link rel="stylesheet" href="css/font-awesome.min.css">
	<link rel="stylesheet" href="css/estilos.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">	
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	

</head>
<body>
	<header>
</header>

<div class="jumbotron boxlogin">
		 
		<form method="POST" name="flogin" id="flogin" action="">
		<label>Usuario:</label>
		<input type="text" name="username"     class="form-control">
		<label>Contraseña:</label>
		<input type="password" name="password"    class="form-control">

		 

		<button name="btnClickI" type="button" class="btn btn-success" data-toggle="modal" data-target="#ingreso">Ingresar</button> 
		<a href="registro.php"><button value="btnClickR" type="button" class="btn btn-info">Registrar</button></a>
<div id="ingreso" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-diolag">
								<div class="modal-content">
									<div class="modal-header">
										<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
									</div>
									<div class="modal-body">
										<h3>Por favor llene todo los campos!</h3>									 
									</div>
								</div>
							</div>
		 
			 
			<?php	 
				if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                              var_dump($_POST);
				       echo '<h2>Había una vez un código HTML...</h2>';
				}else{

                     echo '<h1>ESTO ES UN GET</h1>';
                     var_dump($_GET);
				}
			?>
          </div>
		</form>
	</div>
</body>
</html>

EDITING:

If you need to know when a specific element was clicked, it is easier to do it with JavaScript: here I wrote an answer for this problem.

    
answered by 25.10.2017 в 04:35