To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-13-2008, 09:02 PM   #1
alphasynaptic
Junior Member
 
Join Date: Dec 2005
Posts: 17
resolved [RESOLVED] PHP and Ajax

Well the below code is an incomplete rewrite of a login script I wrote. It's still very much under construction so please no criticism. Anyway, it calls itself through Ajax to perform the login and such and is then supposed to call itself through Ajax again with a message stating the login was successful. The only problem is that where the Ajax function is called the second time, nothing is happening. I placed a simple echo to ensure that the block is being stepped into and it in fact is but the Javascript function is simply not executing the second time.

Code:
<?php
//If not logged in.
include 'mysql_connect.php';
$form_data = explode(',',$_POST['var1']);
Session_start();
if (!isset($_SESSION['username']))
	{
		//Perform login.
		if (isset($form_data[2]))
			{
				$username = strip_tags($form_data[0]);
				$password = md5(strip_tags($form_data[1]));
				//Query user table for user information.
				$userinfo = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
				list($id, $username, $password, $email) = mysql_fetch_row($userinfo);
				$_SESSION['username'] = $username;
				//Query character table for char info.
				$charinfo = mysql_query("SELECT name,class FROM characters WHERE id='$id'");
				list($charname, $class) = mysql_fetch_row($charinfo);
				$_SESSION['char'] = $charname;
				$_SESSION['class'] = $class;
				$_SESSION['id'] = $id;
				echo "poop<script language='javascript'>alert('poop')</script>"; <---javascript code right here isn't executing
				/*echo "<script language='javascript' TYPE='text/javascript'>
					alert('Poop');
					send(\'You are now logged in.\',\''.$path.'/display_files/statistics.php\',\'stats\');
					</script>";*/
			}
		//Display login form.
		else
			{
				echo'<center><br>
				Username: <input type="text" onchange="varArr.push(this.value)" size="15" maxlength="15"><br>
				Password: <input type="password" onchange="varArr.push(this.value)" size="10" maxlength="10"><br>
				<input type="submit" value="Login" onclick="varArr.push(this.value);send(varArr,\'display_files/statistics.php\',\'stats\')">
				<br>
				<a href="index.php?act=register">Register</a></center>';
			}
		}
//If logged in.
else
	{
		//Logout.
		if (isset($_POST['logout']))
			{
				session_destroy();
			}
		//Show statistics.
		else
			{
				$class = classname($_SESSION['class']);
				$charname = $_SESSION['char'];
				echo"<center>$charname</center>
				<center>$class</center><br>
				<center><form action='$_SERVER[PHP_SELF]' method='POST'>
				<input type='hidden' name='header' value='index'>
				<input type='submit' name='logout' value='Logout'>
				</form></center>";
			}
	}
?>
Thanks in advance for any help. I have a suspicion that the problem is that Ajax or any Javascript may not be executed within an Ajax stream, but this is only a thought. Any help or suggestions would be greatly appreciated. Also refrain from unrelated statements such as "you are not validating your login" as I said this script is under construction still and I am simply trying to debug this one error before I fix the rest.

Last edited by alphasynaptic; 05-14-2008 at 09:31 PM.
alphasynaptic is offline   Reply With Quote
Old 05-14-2008, 06:00 AM   #2
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
I am not completely sure how you are doing this (It does not make sense to me to call the same login script twice in the same procedural run).

However, if you replace the content of a <div> and you spit out that javascript during your second ajax run, it might not be considered as loading the script, and as such it is not interpreted & executed. You might have to add the function to your ajax handler instead.

J.
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 05-14-2008, 04:48 PM   #3
alphasynaptic
Junior Member
 
Join Date: Dec 2005
Posts: 17
Sorry, forgot to mention this is included within the index.php page and the scripts are included on there.
alphasynaptic is offline   Reply With Quote
Old 05-15-2008, 02:29 AM   #4
alphasynaptic
Junior Member
 
Join Date: Dec 2005
Posts: 17
Hey guys think I came to my own conclusion while discussing this with a friend. I think it's because of the fact that Ajax processes the requested page and then sends you the output. This means the Javascript function executes in nowhere land. So my solution would be to comment it or insert the function into a text string and then uncommenting it with Javascript after it gets outputted from Ajax. Any comments or suggestions are welcome.
alphasynaptic is offline   Reply With Quote
Old 05-16-2008, 11:02 PM   #5
alphasynaptic
Junior Member
 
Join Date: Dec 2005
Posts: 17
I implemented the solution. I commented the JScript function call and appended some special words to allow commenting for it's intended use. I am using two replace functions in the Ajax handler script to edit out the commenting before the response text is written back to inner html. I tried this on its own and the regular expressions do work and do cause the function call to fire when it is written back to inner HTML. The problem is that for some reason the script still isn't firing when built into my web site. I alerted the text within the JScript before I wrote it to inner HTML and found the text to be what I intended. I cannot figure out the issue here.
alphasynaptic is offline   Reply With Quote
Old 05-17-2008, 06:41 AM   #6
alphasynaptic
Junior Member
 
Join Date: Dec 2005
Posts: 17
Found my solution on my own. It turned out the last post I made was very close to the final solution. I simply had to eval() the string instead of just setting the inner html to it. I also used some regular expressions, to allow both text and functions to work properly, in the Ajax response text handling function.
alphasynaptic is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 06:46 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.