MECU
07-28-2007, 09:55 PM
If you look at: http://www.bcsfanpoll.com/newvoteframe.php and just click "submit", you'll get an alert, and if you wait you'll see where the "need username" and "need password" comes up, but as soon as you hit the alert, that content goes away. If I didn't have the alert there, you wouldn't even see the content at all. It's like it just reloads the page.
I'm new to AJAX and prettymuch copied the tutorial stuff from w3schools ( http://www.w3schools.com/php/php_ajax_xml.asp ). Here's what I've got:
The newvoteframe.php:
<html>
<head>
<script src="vote.js"></script>
</head>
<body>
<div id="poll">
<p>Not registered yet? Signup in the <a href="phpBB2/">forums</a>.</p>
<form name="entry">
<table>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username" size="40" value=""></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" size="40" value=""></td>
</tr>
<tr class="row-a"><td colspan="2"><input type="submit" value="Submit" onclick="getVote(document.entry.username.value, document.entry.password.value);"></td></tr>
</table></form>
</div>
<span id="login"></span>
</body>
</html>
The vote.js:
var xmlHttp
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;
}
function getVote(username, password)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="loginvote.php"
url=url+"?username="+username
url=url+"&password="+password
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("POST",url,true)
xmlHttp.send(null)
alert("got here");
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("login").innerHTML=xmlHttp.responseText
}
}
and the loginvote.php:
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$checkok = true;
if ($username == ""){
print "<p>You must enter your username.</p>";
$checkok = false;
}
if ($password == ""){
print "<p>You must enter your password.</p>";
$checkok = false;
}
if ($checkok){
//do stuff (edited this out)
}
?>
This AJAX stuff is sweet, once I figure out how to get it to stick around.
I'm new to AJAX and prettymuch copied the tutorial stuff from w3schools ( http://www.w3schools.com/php/php_ajax_xml.asp ). Here's what I've got:
The newvoteframe.php:
<html>
<head>
<script src="vote.js"></script>
</head>
<body>
<div id="poll">
<p>Not registered yet? Signup in the <a href="phpBB2/">forums</a>.</p>
<form name="entry">
<table>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username" size="40" value=""></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" size="40" value=""></td>
</tr>
<tr class="row-a"><td colspan="2"><input type="submit" value="Submit" onclick="getVote(document.entry.username.value, document.entry.password.value);"></td></tr>
</table></form>
</div>
<span id="login"></span>
</body>
</html>
The vote.js:
var xmlHttp
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;
}
function getVote(username, password)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="loginvote.php"
url=url+"?username="+username
url=url+"&password="+password
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("POST",url,true)
xmlHttp.send(null)
alert("got here");
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("login").innerHTML=xmlHttp.responseText
}
}
and the loginvote.php:
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$checkok = true;
if ($username == ""){
print "<p>You must enter your username.</p>";
$checkok = false;
}
if ($password == ""){
print "<p>You must enter your password.</p>";
$checkok = false;
}
if ($checkok){
//do stuff (edited this out)
}
?>
This AJAX stuff is sweet, once I figure out how to get it to stick around.