[AJAX & PHP] Hulp met MySQL Data ophalen & versturen...
ik ben bezig met een AJAX Script. Hier de code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Dit is de INDEX.PHP code met de AJAX Function
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var username = document.getElementById('username').value;
var queryString = "?username=" + username;
ajaxRequest.open("GET", "getmember.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Client Code: <input type='text' id='username' onChange='ajaxFunction()' /> <br />
<br />
<input type='submit' value='Data versturen' />
</form>
<div id='ajaxDiv'>Results kome hier te staan</div>
</body>
</html>
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var username = document.getElementById('username').value;
var queryString = "?username=" + username;
ajaxRequest.open("GET", "getmember.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Client Code: <input type='text' id='username' onChange='ajaxFunction()' /> <br />
<br />
<input type='submit' value='Data versturen' />
</form>
<div id='ajaxDiv'>Results kome hier te staan</div>
</body>
</html>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Dit is de GETMEMBER.PHP code die de data uit de Database haalt
<?php
// Database Connection
require('connect.php');
$username = $_GET['username'];
$result = mysql_query("SELECT id, name, email FROM members WHERE name = '$username' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_array( $result );
$clientnr = $row['id'];
$clanrede = $row['name'];
$clname = $row['email'];
// Dit ff voor controle of er een result bestaat...
echo 'Results should be displayed...<br>';
echo $clientnr.', '.$clanrede.', '.$clname;
} else {
echo 'No data found!';
}
?>
<?php
// Database Connection
require('connect.php');
$username = $_GET['username'];
$result = mysql_query("SELECT id, name, email FROM members WHERE name = '$username' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_array( $result );
$clientnr = $row['id'];
$clanrede = $row['name'];
$clname = $row['email'];
// Dit ff voor controle of er een result bestaat...
echo 'Results should be displayed...<br>';
echo $clientnr.', '.$clanrede.', '.$clname;
} else {
echo 'No data found!';
}
?>
De code werkt goed. Ik ben van plan om het formulier nadat de data is opgehaald te versturen. Maar de data kan zo niet worden verzonden. Wat ik dus graag te weten wil komen is, hoe ik de data die via AJAX wordt opgehaald kan meesture via de POST methode...
Kan mij iemand helpen?
Alvast bedankt!
Greetz, Mike :)
Gewijzigd op 01/01/1970 01:00:00 door Mike
http://phphulp.nl/php/tutorials/8/666/
Dit is een voorbeeld van hoe je post variabelen kan mee sturen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
function registreer()
{
var naam = document.getElementById('naam').value;
var paswoord = document.getElementById('paswoord').value;
var passData = 'naam=' + escape(naam) +"&paswoord=" + escape(paswoord) ;
var url = "index.php?pagina=registreer";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = handleHttpResponseLogin;
http.send( passData );
}
{
var naam = document.getElementById('naam').value;
var paswoord = document.getElementById('paswoord').value;
var passData = 'naam=' + escape(naam) +"&paswoord=" + escape(paswoord) ;
var url = "index.php?pagina=registreer";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = handleHttpResponseLogin;
http.send( passData );
}
Pas dan wel de id's aan naar jouw id's bij getElementById()
Gewijzigd op 01/01/1970 01:00:00 door Emmanuel Delay