Results 1 to 6 of 6

Thread: SAJAX login script...need help

  1. #1
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question SAJAX login script...need help

    Hi, the sajax forum is completely spammed out and nobody replys to anything so I thought I'd try here. So I basically need a bit of help with making a sajax login script.



    Code:
    <?php
    	require("Sajax.php");
    	
    	sajax_init();
    	sajax_export("login");
    	sajax_handle_client_request();
    	
    	function login($username, $password){
    		@include("usersystem/include/session.php");
    		
    		$success = "Login Success!";
    		$error = "Username And Password Do Not Match!";
    		
    		global $session, $form;
    		/* Login attempt */
    		$retval = $session->login($username, $password);
    
    		/* Login successful */
    		if($retval){
    		 return $success;
    		}
    		/* Login failed */
    		else{
    		 return $error;
    		}   
    	}
    ?>
    
    <html>
    
    <head>
    	<title>Login</title>
    	
    	<style type="text/css">
    		@import url("css/loginphp.css");
    		@import url("css/style.css");
    	</style>
    	<script type="text/javascript">
    	<?
    	sajax_show_javascript();
    	?>
    		function get(re){
    			document.getElementById("login_re").innerHTML = re;
    		}
    		
    		function login(){			
    			username = document.getElementById("user").value;
    			password = document.getElementById("pass").value;
    					
    			sajax_request_type = "POST";
    			x_login(username, password, get);
    		}
    	</script>
    </head>
    
    <body>
    
    <table>
    <tr><td>
    
    
    <?
    /**
     * User has already logged in, so display relavent links...
     */
    if($session->logged_in){echo "<a href='usersystem/process.php'>Logout</a>";}
    else{
    ?>
    
    <h1>Login</h1>
    
    <span id="login_re">&nbsp;</span>
    <form action="javascript:login();">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" id="user" maxlength="30" value="" /></td><td></td></tr>
    <tr><td>Password:</td><td><input type="password" id="pass" maxlength="30" value="" /></td><td></td></tr>
    <tr><td colspan="2" align="right"><input type="submit" value="Login" /></td></tr>
    </table>
    </form>
    
    <?}?>
    
    
    </td></tr>
    </table>
    
    </body>
    </html>

    If anybody can help me out, that would be grate.

    PS. if you need to see what sajax is, check out http://www.modernmethod.com/sajax/

  2. #2
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by franky20 View Post
    grate
    Great.


    Could you be more specific to the problem you are having? Any error messages?
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  3. #3
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    k, what I want this script to do is send the username and password the user inputed into the form to the php via the javascript (using sajax), then show what the php returns (either the error, or the success messsage). All I'm am getting is "undefined", and not one of the messages. My problem is with the javascript (as fare as I know my php is fine). The error would be with the send-receive function:
    Code:
    function login(){			
     username = document.getElementById("user").value;
     password = document.getElementById("pass").value;
     					
     sajax_request_type = "POST";
     x_login(username, password, get);
    }
    I think its sending the username and password to the php, but its just not getting the return message. Thats were I need help. How would I make the javascript send the username and password to the php and display what the php returns within one function? I know how to do this with two functions on each side (two javascript functions and two php functions as shown below)

    Code:
    <?php
    	require("Sajax.php");
    	
    	sajax_init();
    	sajax_export("getpageid", "submit");
    	sajax_handle_client_request();
    	
    	function getpageid() {
    		include('inc/database.php');
    		return $pageid;
    	}	
    	
    	function submit($data){
    		include('inc/database.php');
    		mysql_query("UPDATE pages SET pageid='$data' WHERE pageid='$pageid'");
    		return getpageid();
    	}
    ?>
    <html>
    <head>
    	<title>SQL DB</title>
    	<script>
    	<?
    	sajax_show_javascript();
    	?>
    	
    	function show_pid(z) {
    		document.getElementById("z").value = z;
    	}
    	
    	function get(){
    		setTimeout("x_getpageid(show_pid)", 10);
    	}
    	
    	function dis(){
    		document.getElementById("savestat").innerHTML = "";
    		document.getElementById("y").value = "";
    		change('savestat', 'hidden');
    		document.getElementById("z").disabled=false;
    	}
    	
    	function savestat(){
    		if(document.getElementById("y").value != data){
    			change('savestat', 'show');
    			document.getElementById("z").disabled=true;
    			document.getElementById("savestat").innerHTML = "Saving...";
    			setTimeout("savestat()", 10);
    		}else{
    			document.getElementById("savestat").innerHTML = "Saved!";
    			setTimeout("dis()", 2500);
    		}
    	}
    	
    	function updateid(d){
    		setTimeout("document.getElementById('y').value = data", 0);
    	}
    	
    	function submit(){
    		data = document.getElementById("z").value;
    		sdata = document.getElementById("y").value;
    				
    		sajax_request_type = "POST";
    		x_submit("" + "" + "" + data, get());
    		
    		savestat();
    		updateid(data);
    	}
    	
    	
    	
    	function change(id, newClass)
    	{
    		identity=document.getElementById(id);
    		identity.className=newClass;
    	}
    	</script>
    	
    	<style type="text/css">
    		#savestat{
    			position:relative;
    			top:-24px;
    			background-color:#eeeeee;
    			color:#bf1900;
    			width:205px;
    			height:20px;
    			text-align:center;
    			padding-top:2px;
    			border:1px solid #000000;
    			
    			filter:alpha(opacity=90);
    			opacity: 0.9;
    			-moz-opacity:0.9;
    		}
    		.hidden{
    			display:none;
    		}
    		.show{
    			display:block;
    		}
    	</style>
    	
    </head>
    <body onload="get();dis();">
    	<form action="javascript:submit()" onsubmit="submit()">
    		<input type="hidden" name="y" id="y" value="" />
    		<input type="text" name="z" id="z" value="" size="30" />
    		
    		<input type="submit" name="check" value="Update ID" />
    		
    		<div id='savestat' class='hidden'></div>
    	</form>
    </body>
    </html>
    The above code gets the id of a page I pre-defined, shows it in a form input, and when the form is submitted, updates the id of that page with what-ever was in the input box when the form was submitted. This code should give you a better idea of what I'm after.

    I think I have explained my problem, and if you could help me solve if, that you be grate.

  4. #4
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    k, I just debugged it a bit more and found that for some reason the javascript cannot receive the return on any php function like the following:

    Code:
    function($string){
     $msg = "Some Message!";
     return $msg;
    }
    but can on a function like this:

    Code:
    function(){
     $msg = "Some Message!";
     return $msg;
    }
    Why is that?

  5. #5
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default OK the above is not true!

    I just wanted to double check if the above was true or not, and it wasn't! The proof is below:

    Code:
    <?php
    	require("Sajax.php");
    	
    	sajax_init();
    	sajax_export("testfunc");
    	sajax_handle_client_request();
    	
    	function testfunc($data_post) {
    		$data = "$data_post Is Cool!";
    		return $data;
    	}	
    ?>
    <html>
    <head>
    	<title>Who Is Cool?</title>
    	<script>
    	<?
    	sajax_show_javascript();
    	?>
    	
    	function show_pid(data) {
    		document.getElementById("output").value = data;
    	}
    	function get(){
    		sajax_request_type = "POST";
    		dd = document.getElementById("name").value;
    		x_testfunc("" + dd + "", show_pid);
    	}
    
    	</script>
    	
    </head>
    <body>
    	Enter A Name:
    	<input type="text" name="name" id="name" value="" size="30" />
    	<input type="text" name="output" id="output" disabled value="" size="30" />
    	<input type="button" name="check" value="GO" onclick="get();">
    </body>
    </html>

  6. #6
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I Found the problem!

    I found the problem and it was not in the javascript! it was in the php, in my included session.php file!

    No more help needed right now!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •