Jump to content

Recommended Posts

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).

Link to comment
https://forums.phpfreaks.com/topic/205407-ajax-with-php-no-error-not-working/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.