How do I check if a query is successful? And if it is, how do I return a value to the previous page?

How do I check if a query is successful? And if it is, how do I return a value to the previous page?

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

I have the following code in a server side php script that is called via ajax. This is where I'd like to see if the queries are true or not and pass a value back to index2.php so I can either prompt for the correct credentials or load another page because authentication was successful.

login3.php

<?php
session_start();
 $_POST['password'] = hash('sha512',  $_POST['password'] .  'password'); 
// $_SESSION['postpassword']=$_POST['password'];


// DataTables PHP library
include( "php/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

/*
 * Example PHP implementation
 */

$out=Editor::inst( $db, 'cms_users', 'userID' )
    ->field(
        Field::inst( 'userEmail' )
          ->validator( 'Validate::email' ),
        Field::inst( 'password' )
          ->validator( 'Validate::notEmpty' )
          ->setFormatter( function ( $val ) {
          return hash('sha512',  $val .  'password');
        })
    )
    ->where( 'userEmail', $_POST['email'], '=')
    ->where( 'password', $_POST['password'], '=')
    ->process($_POST)
    ->json();

and my Ajax (index2.php)...

<form id="login" action="login3.php" method="POST">
  <table width="35%" class="login_toolbar" align="center">
    <tr><td>
    <table id="toolbar" border="1">
    <tr>
      <td colspan="2"><b id="large" class="nowrap">CCIU CASE MANAGEMENT SYSTEM</b></td>
    </tr>

    <tr>
      <td class="pad2"><label for="email">Email:</label></td>
      <td><input type="text" name="email" id="email" class="input-xs-small"></td>
    </tr>

    <tr>
      <td class="pad2"><label for="password" align="right">Password:</label></td>
      <td><input type="password" name="password" id="password" class="input-xs-small"></td>
    </tr>

    <tr>
      <td>&nbsp;</td>
      <td class="pad2"><button type="submit" class="btn btn-success"><i class="fa fa-sign-in"></i> Login</button>
      </td>
    </tr>
    </table>

  </td>
  <td id="toolbar" class="pad2">
    <img src="images/040Logo2.png" width=112px height=114px>
  </td>
  </tr>
  </table>
</form>


<script>
editor.on( 'preSubmit', function ( e, obj, action ){
        //--- Compare Password and Confirm fields. ---//

            if ( this.get( 'password' ) === '' ) {
            this.field('password').error('Password is required!');
            return false;
            }

    var frm = $('#login');
    frm.submit(function (ev) {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('ok');
            }
        });

        ev.preventDefault();
    });
</script>

<script>
$("#login").submit(function(e) {

    var url = "login3.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#login").serialize(), // serializes the form's elements.
           success: function(data)
           {
             if(isset(data['password'])){
               alert(document.write("No Match "));
               alert(document.write(typeof data['userEmail']));
             }else{
               alert(document.write("Matched "));
               alert(document.write(typeof data['userEmail']));
             }
               alert(data); // show response from the php script.
               alert($_POST['password']);
           }
         });

//    e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    Which query? It looks like you might be using the Editor class to try and log the user in - is that correct? If so, it isn't designed for that - it is designed for modifying data on a database.

    A simple SELECT query would be the best way to go. If you want to use the Editor database connection for that, the documentation for it is available here.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    I was referring to...

    ->where( 'userEmail', $_POST['email'], '=')
    ->where( 'password', $_POST['password'], '=')
    

    I'm trying to see if that query is true and if it is send the user to a home page, if it is not true take the user back to the login page. What should I use instead of editor. I need some way to sha512 hash the supplied user password and check it against the sha512 hash in the database.

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    Thanks for the clarification. Yes, that is not what the Editor class is designed for.

    Instead, just make a standard SELECT statement against your database. Are you using a PDO connection? Use that if so, there are lots of tutorials around the web on how to query a database from PHP.

    Or, if you want to use the Editor Database class, I linked to the documentation for it above.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Thank you Alan. I'm using mysql. I do know how to use php to query a database. I have been doing that for over 15 years.
    I think I know what you are saying though. My logon page can check the email and password using php by itself. Pretty simple. I guess I was just over thinking it, or lost :)

This discussion has been closed.