Hi daredevil14,
I have been trying to do something every similar to yours and while debugging, I had found another post of yours (adding xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")) but from this thread, I read that yours is writing to the database... mine won't!! Can you see something in my code?
The JavaScript
<script type="text/javascript">
<!--
function addUser()
{
<!-- debugger; -->
var email = document.getElementById("myemail").value;
var pass = document.getElementById("mypassword").value;
var alias = document.getElementById("myalias").value;
if (email.length == 0) { alert("Form field 'Electronic Mail' is blank"); return; }
if (pass.length == 0) { alert("Form field 'Login Passowrd' is blank"); return; }
if (alias.length == 0) { alert("Form field 'Alias' is blank"); return; }
var xmlHttp;
try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } };
xmlHttp.onreadystatechange=function()
{
switch (xmlHttp.readyState)
{
<!-- The request is not initialized -->
case 0:
alert("request error");
break;
<!-- The request has been set up -->
case 1:
alert("request is set");
break;
<!-- The request has been sent -->
case 2:
alert("request was sent");
break;
<!-- The request is in process -->
case 3:
alert("request is in progress");
break;
case 4:
alert("xmlHttp.responseText: " + xmlHttp.responseText);
switch (xmlHttp.responseText)
{
case true:
<!-- username/password was incorrect -->
alert("username/password was incorrect");
break;
case false:
<!-- username/password was correct -->
alert("username/password was correct");
break;
}
break;
}
}
var query = "myemail=" + escape(email) + "&mypassword=" + escape(pass) + "&myalias=" + escape(alias);
xmlHttp.open("POST","addUser.php",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
alert("send: " + query);
xmlHttp.send(query);
}
-->
</script>
The Form
<div id="page">
<div id="addList"></div>
<div id="addUser">
<h1>Add User</h1><br />
<form>
<p><span class="formLeft">Electronic Mail</span><input id="myemail" type="text"></p>
<p><span class="formLeft">Login Password</span><input id="mypassword" type="text"></p>
<p><span class="formLeft">Alias</span><input id="myalias" type="text"></p>
<p><br/><span class="formLeft"> </span><input type="submit" value="Add Me!" onclick="addUser()"></p>
</form>
</div>
</div>
I am running this locally so my PHP looks like
<?php
# dBNames
$db_name="MylesAlexander";
$db_table="db_users";
# connection to db
$link = mysql_connect("localhost", "root", "root");
mysql_select_db($db_name, $link);
# GET/SET users post login info
$email=htmlentities(mysql_real_escape_string($_POST['myemail']);
$password=htmlentities(mysql_real_escape_string($_POST['mypassword']);
$alias=htmlentities(mysql_real_escape_string($_POST['myalias']);
# Get Tables Data
$query = "INSERT INTO '$db_table' (users_ID,users_login,users_pass,users_alias,users_registered) VALUES (NULL, '$email', '$password', '$alias', NOW())";
if (mysql_query($query, $link))
{
echo "Success";
}
else
{
echo "Error";
}
mysql_close($link);
?>
The alert let me know that the fields value are been sent properly but once the page reloads, there was nothing added to the database and and xmlHttp.responseText is blank?
Thanks for any help.
edit: updated PHP code; still same results