dixo1 Posted June 21, 2010 Share Posted June 21, 2010 Hello im doing a login box for a website. I heard AJAX are able to communicate with php and post data without refreshing the page. So i tryed this out: (Brugernavn = Username, Kodeord = Password) <script type="text/javascript"> function Login() { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { if(xmlhttp.responseText == "Du er nu logged ind!") { document.getElementById("answer").innerHTML=xmlhttp.responseText; setTimeout("location.reload(true);","2000"); } else { document.getElementById("answer").innerHTML=xmlhttp.responseText; } } else { document.getElementById("answer").innerHTML="<img width='18px' height='18px' src='http://www.fairtrade-music.com/images/loading/loading.png' /> Logger ind..."; } } xmlhttp.open("POST","data.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("action=login&brugernavn=" + document.getElementById("brugernavn").value + "&kodeord=" + document.getElementById("kodeord").value); document.getElementById("brugernavn").value=""; document.getElementById("kodeord").value=""; } </script> data.php: <?php function MysqlConnect() { include("./_inc/config.php"); $link = mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die(mysql_error()); mysql_select_db($mysql_db,$link) or die(mysql_error()); } if($_GET['action'] == "login") { if(!empty($_GET['brugernavn']) && !empty($_GET['kodeord'])) { $bruger_tegn = "#[^a-zA-Z0-9_\-]#"; $brugernavn = strtoupper($_GET['brugernavn']); $kodeord = strtoupper($_GET['kodeord']); if(preg_match($bruger_tegn,$brugernavn)) { echo 'Brugernavn indeholder bannede tegn!'; }else{ if(preg_match($bruger_tegn,$kodeord)) { echo 'Kodeord indeholder bannede tegn!'; }else{ MysqlConnect(); $brugernavn = mysql_real_escape_string($brugernavn); $kodeord = mysql_real_escape_string($kodeord); $query = mysql_query("SELECT brugernavn, kodeord FROM brugere WHERE brugernavn = '" . $brugernavn . "' AND kodeord = '" . $kodeord . "'") or die(mysql_error()); $rows = mysql_num_rows($query) or die(mysql_error()); if($rows == "1") { echo "Du er nu logget ind!"; }else{ echo "Forkert brugernavn eller kodeord!"; } } } }else{ echo "Udfyld begge felter!"; } } ?> Hope someone can help me out, page ant doing anything. No messages are pasted into answer (div box). Quote Link to comment https://forums.phpfreaks.com/topic/205407-ajax-with-php-no-error-not-working/ Share on other sites More sharing options...
trq Posted June 21, 2010 Share Posted June 21, 2010 You are sending the data via POST yet looking for it within the $_GET array. Quote Link to comment https://forums.phpfreaks.com/topic/205407-ajax-with-php-no-error-not-working/#findComment-1074930 Share on other sites More sharing options...
dixo1 Posted June 21, 2010 Author Share Posted June 21, 2010 Thx :] Did't see that one. Works perfectly now! Quote Link to comment https://forums.phpfreaks.com/topic/205407-ajax-with-php-no-error-not-working/#findComment-1074942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.