Jump to content

[SOLVED] checking a form


ngreenwood6

Recommended Posts

I have 3 files:

 

index.php

<?php
include("includes/variables.php");
?>
<html>
<head>
<script type="text/javascript" src="includes/login.js"></script>
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="includes/check_username.js"></script>
<title><?php echo $sitename; ?></title>
</head>

<body>

<center>

<h1>Login Here!</h1>

<form name="login_form">
<table id="login_table">
<tr>
<td>Username</td>
<td>:</td>
<td><input name="username" type="text"></td>
<td id="username_empty"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password"></td>
<td id="password_empty"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="button" name="submit_login" value="Log In" onClick="check_user(this.value);"></td>
</tr>
</table>
</form>

</center>

</body>
</html>

 

login.js

 

//check that a username has been put into the form
function check_user(username, password)
{
//set value for form to be ready to submit
var form_ready = true;

//get username from form
var username = document.login_form.username.value;

//get password from form
var password = document.login_form.password.value;

//if nothing is set for username
if(username == "")
{
//set the error for no username
var no_username = "Please enter a username!";

//set the form to not be ready for submission
form_ready = false;

//If nothing is set show the error
document.getElementById("username_empty").innerHTML=no_username;
}
else
{
//if something is there remove the error
document.getElementById("username_empty").innerHTML="";
}

//if nothing is set for password
if(password == "")
{
//set the error for no password
var no_password = "Please enter a password!";

//set the form to not be ready for submission
form_ready = false;

//if nothing is set for password show the error
document.getElementById("password_empty").innerHTML=no_password;
}
else
{
//if something is set remove the error
document.getElementById("password_empty").innerHTML="";
}

if (form_ready == true)
{
alert("The form is ready");
}

}


 

and check_user.php

 

<?php
//include the variables
include("includes/variables.php");

//get the variables from the form
$username = $_POST['username'];
$password = $_POST['password'];

//connect to the database
$connect = mysql_connect($host,$db_user,$db_pass);

//select the database
$select_db = mysql_select_db($db);

//query the database
$query = "SELECT * FROM $table WHERE username = '$username'";

//get the results
$results = mysql_query($query);

?>

 

What this does at this point is check to make sure that the user puts in a username and if not it tells them to enter a username. The check_user.php is not in use yet. What I want to be able to do is check the username and password before the form is submitted. If the username does not exist or the password is incorrect I want to be able to display those errors. At this point if they enter a username and password it alert's them that everything is good. The problem is that I am having a time trying to figure out how to pass the variables to the check_user.php form and how to display the error if it isn't there. Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/127014-solved-checking-a-form/
Share on other sites

Hope this help your query? You have to add this file as your header javascript. and call the main function from your html > onmouseover or onClick event.

 

/**
* @xmlHttp used to detect the browser object
* related pages
* --------------------------------------------
* Basic standard useing
*/

var xmlHttp;

/**
* @GetXmlHttpObject used to detect the browser and set
* xmlHttp
* Firefox, Opera , Safari, IE
*
*/
function GetXmlHttpObject(){
  var xmlHttp=null;
  try{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }catch (e){
    // Internet Explorer
    try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}


/**
* @ajax_main used inside the html action events
* Function to be called from the main pages.
* ajax_main(option1, option2, option3, option4)
* example: 
* ajax_main( price.options[price.selectedIndex].value + ' ' + this.options[this.selectedIndex].value );
*
*/
function ajax_main(str1, str2, str3, str4){
  // Set xmlHttp object for browser
  xmlHttp=GetXmlHttpObject();
  // Condition for browser
  if (xmlHttp==null){
    alert ("This browser does not support AJAX!");
    return;
  }
  /* 
   * Variable setup for php or asp or jsp
   * Prepare your page to return 
   * username ok or password ok
   */
  var url="username_password.php";
  url=url+"?username="+str1+"&password="+str2+"&q3="+str3+"&q4="+str4;
  // Must be used , dont change
  url=url+"&sid="+Math.random();

  /* 
   * Call output event
   * Query output represent
   * *condition can be removed
   */
  if(str1=="2"){
      xmlHttp.onreadystatechange=ajax_output1;
  }else{
    xmlHttp.onreadystatechange=ajax_output2;
  }
  // Execute GET method
  xmlHttp.open("GET",url,true);
  // Send Get or Post method
  xmlHttp.send(null);
}

/**
* @ajax_output1
* Login failure
*
*/
function ajax_output1(){
  if (xmlHttp.readyState==4){
    /* 
     * Following line is used to get query result output
     * & Presentation to your page back with 
     * Alert box or etc..........
     */
    if(xmlHttp.responseText.match("username ok")!=null) {
      // Usename returned from SQL
      str1 = "update table set session='online' where username='" + usenames + "' and password='password'";
      ajax_call_sql(str1);
      // Show the page usename is found
      document.getElementById("s1").innerHTML="Username valid";
    }
  }
}

/**
* @ajax_output2
* reserved event...
*
*/
function ajax_output2(vAcmd,vBcmd){
    // for something else use
    // On Fly , SQL insert, update, delete
    
}

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.