1

First of all I want to note that I am very new with HTML and PHP, JS and web design at all, but I have to learn and create registration form for my personal page, and should find out with proper approach and possible method.

I have index.php with user login and registration forms. Inside <!DOCTYPE html><html><body> ... </body></html> login form includes input type="text" and type="password":

<div id="loginWrap">
 <div class="container">
  <form action="login.php" method="post" id="logform">
    <div class="row">
      <div class="col-25">
        <label for="fname">Username:</label>
      </div>
      <div class="col-75">
        <input type="text" id="yuser" name="username" placeholder="Username.." required>
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="lname">Password:</label>
      </div>
      <div class="col-75">
        <input type="password" id="ypass" name="password" placeholder="Password.." required>
      </div>
    </div>
  <br/>
    <div class="row">
      <div class="col-25">
        <label for="lname"></label>
      </div>
      <input type="submit" value="Login" id="logBtn">
    </div>
  </form>
</div> 

sends name="username" to PHP with action="login.php" in <Form>, which was separate login.php code document used this way from index.html. Now it is included to index.php just as it was separately, without any edit:

<?php
require 'connect.php';

$Username = mysqli_real_escape_string($con, $_POST['username']);
$Password = mysqli_real_escape_string($con, md5($_POST["password"]));

  $SQL = "INSERT INTO registration (username, password) VALUES ('$Username', '$Password')";  
  if (!mysqli_query($con, $SQL))
  {
      echo 'Not Inserted';
  }
  else
  {
     echo 'Inserted';
  }      
?>

How to pass inserted name="username" and name="password" to $_POST['username'] and $_POST['password'] in same index.php page with press on <input type="submit" value="Login" id="logBtn"> and avoid processing of this code with index.php page loading automatically.

Any guide, explanation or advice would be very helpful

15
  • not getting what you want to achieve. Commented Aug 20, 2019 at 6:07
  • 2
    you can use AJAX for the same. Commented Aug 20, 2019 at 6:07
  • 4
    Warning! Don't use md5 for password hashing! The manual even states: "Warning - It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm". You should use PHP's password_hash() to create a secure hash. Then you can use password_verify() to verify a password against a hash. Commented Aug 20, 2019 at 6:09
  • 2
    @ladoo111 then use AJAX. Commented Aug 20, 2019 at 6:22
  • 1
    Also, see about prepared and bound queries Commented Aug 20, 2019 at 7:12

2 Answers 2

1

just check for if the submitted value is present . then run the query.

<?php
require 'connect.php';

if( isset( $_POST['username'] ) && isset( $_POST['password'] )  ){

$Username = mysqli_real_escape_string($con, $_POST['username']);
$Password = mysqli_real_escape_string($con, md5($_POST["password"]));

  $SQL = "INSERT INTO registration (username, password) VALUES ('$Username', '$Password')";  
  if (!mysqli_query($con, $SQL))
  {
      echo 'Not Inserted';
  }
  else
  {
     echo 'Inserted';
  }      
} 


?>
Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer, but If you just help us to make it a habit of using prepared statements instead of mysqli_real_escape_string I will be happy by upvoting this answer :)
@shihab Hello, thank you for your answer, it works fine, but I will remark it to Pathik Vejani answer, because my goal is retrieve data without refreshing the page and use separated .php-s
0

You can use AJAX to achieve this.

HTML:

<div id="loginWrap">
  <div class="container">
      <div class="row">
        <div class="col-25">
          <label for="fname">Username:</label>
        </div>
        <div class="col-75">
          <input type="text" id="yuser" name="username" placeholder="Username.." required>
        </div>
      </div>
      <div class="row">
        <div class="col-25">
          <label for="lname">Password:</label>
        </div>
        <div class="col-75">
          <input type="password" id="ypass" name="password" placeholder="Password.." required>
        </div>
      </div>
      <br/>
      <div class="row">
        <div class="col-25">
          <label for="lname"></label>
        </div>
        <input type="button" value="Login" id="logBtn">
      </div>
  </div>

AJAX:

<script type="text/javascript" >
    $("#logBtn").click(function() {
      var uname = $("#yuser").val();
      var pass = $("#ypass").val();
      if(uname == '') {
        alert('Please enter username.')
      } else if (pass == '') {
        alert('Please enter password.')
      } else {
        $.ajax ({
          url: 'login.php',
          data: {username: uname, password: pass},
          type: 'post',
          success: function(result)
          {
            // here you will get result
            console.log(result);
          }
        });
      }
    });
  </script>

Login.php:

require 'connect.php';
  if( isset($_POST['username']) && isset($_POST['password'])) {
    $uname = mysqli_real_escape_string($con, $_POST['username']);
    $pwd = mysqli_real_escape_string($con, md5($_POST["password"]));
    if($uname == '' || $pwd == '') {
      echo 'Please enter required information';
    } else {
      $query = "INSERT INTO registration (username, password) VALUES ('$uname', '$pwd')";
      if (!mysqli_query($con, $query))
      {
        echo $uname." cannot be registered! Please try again.";
        <!-- echo 'Not Inserted'; -->
      }
      else
      {
        // instead of msg you can return username like below:
        echo $uname." registered successfully!";
        // echo 'Inserted';
      }
    }
  } else {
    echo 'Something is wrong!';
  }

10 Comments

I've added your HTML, AJAX JS code in index.php, and I've tried as include your PHP externally as url: 'login.php', in index.php and inside index.php as url: 'index.php' for own self, as it works with shihab statement. Inside 'index.php' 'I've used statement, with external login.php I've tried both, but nothing with console.log(result); or document.getElementById("demo3").innerHTML = result; with AJAX. Please, confirm if your example means adding 'login.php' separately from 'index.php' and check if nothing is missed in your example
@Lado_O yes, there should be separate file login.php in which you have insert statement
HTML and AJAX code will be in index.php file and insert statement will be in login.php file
I'm not sure what is wrong here: connect.php works with previous login or anything else. I did not changed anything in your login.php except removing of comment <!-- echo 'Not Inserted'; -->, AJAX included to <script type="text/javascript"> with url: 'login.php', HTML login code matches all values without edit, but I don't have record and console.log(result); or document.getElementById("demo3").innerHTML = result; from does not gives anything. I want figure out with your answer, because separated .php-s seems to me useful, but by some reason it does not works
check login.php getting data or not by debugging
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.