Jump to content

AJAX LOGIN FORM


suma237

Recommended Posts

Hai,

 

im unable to go to next page after database validation....pls find the error in this page

Main form--

<!-- theform.php -->

<div style="padding: 10px;">

<div id="messagebox"></div>

<form method="get" name="myForm" action="" >

<input type="hidden" name="submit" value="send">

Your Name<br />

<input id="yourname"  type="text" value=""  />

<br />

Your MailId<br />

<input id="mailid"  type="text" value=""  />

<input type="button" value="Submit" onclick="validateform()" />

<div align="right"><a href="javascript:close()">close</a></div>

</form>

</div>

<script language="javascript">

 

 

function validateform (){

var ajaxRequest;

try{

// Opera 8.0+, Firefox, Safari

ajaxRequest = new XMLHttpRequest();

} catch (e){

// Internet Explorer Browsers

try{

ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try{

ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e){

// Something went wrong

alert("Your browser broke!");

return false;

}

}

}

thevalue=document.getElementById('yourname').value;

themail=document.getElementById('mailid').value;

 

serverPage = "validator.php?sstring=" + thevalue+ "&themail=" + themail ;

//alert(serverPage);

objID = "messagebox";

var obj = document.getElementById(objID);

ajaxRequest.open("GET", serverPage,true);

//ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

ajaxRequest.onreadystatechange = function() {

if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {

obj.innerHTML = ajaxRequest.responseText;

}}

ajaxRequest.send(null);

 

}

 

</script>

 

 

2.validator.php

 

<?php

include("includes/db.inc.php");

$db=new DB;

$db->open();

//validator.php

//A list of valid names.

//Again, this would usually come from a database.

 

//echo'<pre>';print_r($_REQUEST);echo'</pre>';

 

if($_GET['sstring']==""){

//Then return with an error.

?>

<span style="color: #FF0000;">Name should not be blank...</span><?php

} else if($_GET['themail']=="") {?>

<span style="color: #FF0000;">mailid should not be blank...</span>

<?php

} else if(!ereg ("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_GET['themail'])) {?>

<span style="color: #FF0000;">Email format is incorrect ...</span>

<?

}else if($_GET['sstring']!="" && $_GET['themail']!=""){

?><span style="color: #FF0000;">Form would now submit...</span><?php

//echo'<pre>';print_r($_GET);echo'</pre>';

$nam=$_GET['sstring'];

$ma=$_GET['themail'];

  $sql="select * from  tbl_login where name='$nam' and mail='$ma'";

$result=$db->query($sql)or die($db->error());

 

$numrows=$db->numRows($result);

if($numrows>0)

{

 

//header('Location:form2.php');

 

 

?>

 

 

<!--<script language="javascript">

alert("sss");

window.location.href="form2.php";

</script>

<span style="color: #FF0000;">Thanks

</span> -->

<?

 

 

}

else{?>

<span style="color: #FF0000;">Invalid Login</span>

<?}

}

?>

 

Link to comment
Share on other sites

suma237,

 

After getting rid of the syntax errors in your posted code, I got it to work, but this doesn't really seem to be something that needs AJAX.

 

Anyway, here is the code I got to work.

 

(Your original code was complaining about headers already sent, because of : <span style="color: #FF0000;">Form would now submit...</span>)

 

Scot L. Diddle, Richmond VA

 

 

theForm.html

 

 


<html>

<head>
<script language="javascript">


function validateform (){
var ajaxRequest;
try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   thevalue=document.getElementById('yourname').value;
   themail=document.getElementById('mailid').value;

serverPage = "validator.php?sstring=" + thevalue+ "&themail=" + themail ;
//alert(serverPage);
objID = "messagebox";
var obj = document.getElementById(objID);
ajaxRequest.open("GET", serverPage,true);
//ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
obj.innerHTML = ajaxRequest.responseText;
}}
ajaxRequest.send(null);

}

</script>

</head>

<body>


<!-- theform.php -->
<div style="padding: 10px;">
<div id="messagebox"></div>
<form method="get" name="myForm" action="" >
<input type="hidden" name="submit" value="send">
Your Name

<input id="yourname"  type="text" value=""  />


Your MailId

<input id="mailid"  type="text" value=""  />
<input type="button" value="Submit" onclick="validateform()" />
<div align="right"><a href="javascript:close()">close</div></a>
</form>
</div>

</body>

</html>


 

 

validator.php

 


<?php
//include("include/db.inc.php");
//$db=new DB;
//$db->open();
//validator.php
//A list of valid names.
//Again, this would usually come from a database.
//echo
// print_r($_REQUEST);

if($_GET['sstring']==""){
//Then return with an error.
?>
<span style="color: #FF0000;">Name should not be blank...</span>
<?php
} else if($_GET['themail']=="") {?>
<span style="color: #FF0000;">mailid should not be blank...</span>
<?php
} else if(!ereg ("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_GET['themail'])) {
?>
<span style="color: #FF0000;">Email format is incorrect ...</span>
<?php
}else if($_GET['sstring']!="" && $_GET['themail']!=""){

   header('Location:processSubmit.php');
   
exit;

$nam=$_GET['sstring'];
$ma=$_GET['themail'];
  $sql="select * from  tbl_login where name='$nam' and mail='$ma'";
$result=$db->query($sql)or die($db->error());

$numrows=$db->numRows($result);
if($numrows>0)
   {
   
   header('Location:form2.php');    
      
      
   ?>
   
      
   <!--<script language="javascript">
   alert("sss");
               window.location.href="form2.php";
            </script>
            <span style="color: #FF0000;">Thanks
            </span>   -->
      <?php
      
      
   }
   else{?>
   <span style="color: #FF0000;">Invalid Login</span>   
   <?php }
}
?>

 

 

processSubmit.php


<?php

echo "Were Here ... Submit Worked ! <br /> \n";

?>

Link to comment
Share on other sites

Hai,

Thanks for ur reply.my requirment is after valid the feild name and mailid,i want to go to next page.

After submitimg,if the feild value will match with the databse value then i want to go to next page.

 

Example-Same function of gmail main page..if u have any code like that pls send me.please.....

Link to comment
Share on other sites

suma237,

 

Hello, That's what I thought you wanted to do, which is why I posted that I did not think that AJAX was the solution you were looking for.  The power of AJAX is that it allows for writing portions of the current page without re-writing the whole page.

 

It sounds like you just need a simple action="somePHP.php" linked to a form submit button... The validation could take place after the submit is entered.

 

Scot L. Diddle, Richmond

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.