javascript variable 2 php variable
ik heb een javascript die resultaten haalt uit een database zodat je keuzes krijgt als je het formulier aan het invullen bent
nu wil ik een javascript variable omzetten naar een php variable zodat ik meet gegevens uit de database kan halen
iemand een idee hoe ik dit kan doen zonder de pagina te vernieuwen..
ik had al iets gevonden maar wil tot nu toe niet echt werken
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
This worked for me.
I didnt wanna do a page refresh.
I wanted to pass the value of a JS variable to a php function on the same page without having a page refresh or calling a 2nd php script
<script type='text/javascript'>
<?php $abc ?> = document.write(document.getElementById('report_nam e').value ); <?php ;?>;
</script>
<?php $obj1->getItem( $abc); ?>
$abc is the variable in which i get the JS variable value.
JS variable value is read using "document.getElementById"
I then pass the value of $abc in a php function as:-
<?php $obj1->getItem( $abc); ?>
I didnt wanna do a page refresh.
I wanted to pass the value of a JS variable to a php function on the same page without having a page refresh or calling a 2nd php script
<script type='text/javascript'>
<?php $abc ?> = document.write(document.getElementById('report_nam e').value ); <?php ;?>;
</script>
<?php $obj1->getItem( $abc); ?>
$abc is the variable in which i get the JS variable value.
JS variable value is read using "document.getElementById"
I then pass the value of $abc in a php function as:-
<?php $obj1->getItem( $abc); ?>
Javascript is een client-side scripting-taal. PHP is server-side. Dit betekent dat wanneer je een pagina opvraagt op de server, deze pagina wordt gegenereerd door de PHP parser. Het resultaat hiervan wordt vervolgens naar de browser gestuurd. In de browser wordt de javascript uitgevoerd. Andersom kan niet. De enige manier om een waarde mee te geven aan PHP is door een AJAX-request te doen naar een PHP-bestand met bijvoorbeeld die waarde in de querystring. Deze waarde zou je vervolgens (onder water) in een sessie of een database kunnen zetten.
rpc.php
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
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
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'User' ,'pass', 'db');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE '$queryString%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $queryString = 'Uni';
// Returned data = 'United States, United Kindom';
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever DESC");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->opdrachtgever.'\');">'.$result->opdrachtgever.'</li>';
$adres = "$result->adres1";
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'User' ,'pass', 'db');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE '$queryString%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $queryString = 'Uni';
// Returned data = 'United States, United Kindom';
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever DESC");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->opdrachtgever.'\');">'.$result->opdrachtgever.'</li>';
$adres = "$result->adres1";
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
dan de pagina met het formulier
orders.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<html>
<head>
<script type="text/javascript">
function toonVensterNieuweOrder()
{
document.getElementById("EE").style.display = "block";
var randomWaarde = Math.floor(10000 * Math.random() );
document.getElementById("mijnd").src = "http://www.hws-koeriers.nl/hws/alerts.php&random_waarde="+ randomWaarde;
document.getElementById("mijnd").style.visibility = 'hidden';
}
function verbergVensterNieuweOrder()
{
document.getElementById("EE").style.display = "none";
}
</script>
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
<?php $abc ?> = document.write(thisValue); <?php ;?>
<?php
$order = "SELECT * FROM HWS_Orders WHERE opdrachtgever = '$abc' GROUP BY opdrachtgever DESC ";
$order = mysql_query($order);
$order1 = mysql_fetch_array($order);
?>
$('#inputString').val(thisValue);
$('#inputString1').val("<?php echo $order1["adres1"]; ?>");
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style>
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: absolute;
left: 90px;
top: 60px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #ffffff;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 1px solid #4368AD;
color: #4368AD;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
#EE {background-color: transparant; position:absolute; top:30%; left:35%; display: none;}
</style>
</head>
<body>
<div id="EE" align=center>
<table width="365" height="185">
<form action=orders.php?nieuweorder=1 method=post AUTOCOMPLETE = "off">
<tr bgcolor=#4368AD><td>
<table width="370" class="tblBaseTblData" height="190">
<thead><tr NOWRAP><th> Nieuwe Order</th><th><img src=images/orders.png border=0 valign=right align=right> </th></tr></thead>
<tr class="evenRow"><td> Opdrachtgever</td><td> <input type=text name=opdrachtgever size=40 value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;">
<div class="suggestionsBox" id="suggestions" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList">
</div></div></td></tr>
<tr class="oddRow"><td> Adres</td><td> <input type=text name=adres1 size=40 value="" id="inputString1" onblur="fill();" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="evenRow"><td> Postcode</td><td> <input type=text name=postcode1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="oddRow"><td> Plaats</td><td> <input type=text name=plaats1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td> Land</td><td> <input type=text name=land1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="oddRow"><td> Telefoon</td><td> <input type=text name=tel1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td> Contact</td><td> <input type=text name=contact1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="oddRow"><td> Email </td><td> <input type=text name=email1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td></td><td><center><input type=submit value=Opslaan style="color: #4368AD; font-style: normal; font-family: calibri; font-size: 11;">
<button type=reset onClick="javascript:verbergVensterNieuweAlert();" style="color: #4368AD; font-style: normal; font-family: calibri; font-size: 11;">Annuleer</button></center></td></tr>
</form></table></td></tr></table>
</div>
</body>
</html>
<head>
<script type="text/javascript">
function toonVensterNieuweOrder()
{
document.getElementById("EE").style.display = "block";
var randomWaarde = Math.floor(10000 * Math.random() );
document.getElementById("mijnd").src = "http://www.hws-koeriers.nl/hws/alerts.php&random_waarde="+ randomWaarde;
document.getElementById("mijnd").style.visibility = 'hidden';
}
function verbergVensterNieuweOrder()
{
document.getElementById("EE").style.display = "none";
}
</script>
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
<?php $abc ?> = document.write(thisValue); <?php ;?>
<?php
$order = "SELECT * FROM HWS_Orders WHERE opdrachtgever = '$abc' GROUP BY opdrachtgever DESC ";
$order = mysql_query($order);
$order1 = mysql_fetch_array($order);
?>
$('#inputString').val(thisValue);
$('#inputString1').val("<?php echo $order1["adres1"]; ?>");
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style>
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: absolute;
left: 90px;
top: 60px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #ffffff;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 1px solid #4368AD;
color: #4368AD;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
#EE {background-color: transparant; position:absolute; top:30%; left:35%; display: none;}
</style>
</head>
<body>
<div id="EE" align=center>
<table width="365" height="185">
<form action=orders.php?nieuweorder=1 method=post AUTOCOMPLETE = "off">
<tr bgcolor=#4368AD><td>
<table width="370" class="tblBaseTblData" height="190">
<thead><tr NOWRAP><th> Nieuwe Order</th><th><img src=images/orders.png border=0 valign=right align=right> </th></tr></thead>
<tr class="evenRow"><td> Opdrachtgever</td><td> <input type=text name=opdrachtgever size=40 value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;">
<div class="suggestionsBox" id="suggestions" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList">
</div></div></td></tr>
<tr class="oddRow"><td> Adres</td><td> <input type=text name=adres1 size=40 value="" id="inputString1" onblur="fill();" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="evenRow"><td> Postcode</td><td> <input type=text name=postcode1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="oddRow"><td> Plaats</td><td> <input type=text name=plaats1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td> Land</td><td> <input type=text name=land1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="oddRow"><td> Telefoon</td><td> <input type=text name=tel1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td> Contact</td><td> <input type=text name=contact1 size=40 value="" style="outline: none; background-color: #f0f0f0; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></td></tr>
<tr class="oddRow"><td> Email </td><td> <input type=text name=email1 size=40 value="" style="outline: none; background-color: #ffffff; border:0; color: #4368AD; font-style: normal; font-family: calibri; font-size: 12;"></td></tr>
<tr class="evenRow"><td></td><td><center><input type=submit value=Opslaan style="color: #4368AD; font-style: normal; font-family: calibri; font-size: 11;">
<button type=reset onClick="javascript:verbergVensterNieuweAlert();" style="color: #4368AD; font-style: normal; font-family: calibri; font-size: 11;">Annuleer</button></center></td></tr>
</form></table></td></tr></table>
</div>
</body>
</html>
De opdrachtgever vult hij nu automatisch in als je deze aanklikt
maar nu is het dus eigenlijk de bedoeling dat hij dit ook doet voor adres, postcode, plaats enz...
Als ik een opdrachtgever aan klik pakt hij altijd de eerste ingevoegde rij terwijl ik wil dat hij de laatste rij pakt aangezien de adres gegevens zijn gewijzigd
het maakt niet uit of ik asc of desc gebruik hij blijft de eerste rij uit de database gebruiken
iemand een idee hoe ik dit kan oplossen
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
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
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'user' ,'pass', 'db');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE '$queryString%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $queryString = 'Uni';
// Returned data = 'United States, United Kindom';
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever ORDER BY id DESC LIMIT 8");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->opdrachtgever.'|'.$result->adres1.'|'.$result->postcode1.'|'.$result->plaats1.'|'.$result->land1.'|'.$result->tel1.'|'.$result->contact1.'|'.$result->email.'\');">'.$result->opdrachtgever.'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'user' ,'pass', 'db');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE '$queryString%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $queryString = 'Uni';
// Returned data = 'United States, United Kindom';
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
$query = $db->query("SELECT * FROM HWS_Orders WHERE opdrachtgever LIKE '$queryString%' GROUP BY opdrachtgever ORDER BY id DESC LIMIT 8");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->opdrachtgever.'|'.$result->adres1.'|'.$result->postcode1.'|'.$result->plaats1.'|'.$result->land1.'|'.$result->tel1.'|'.$result->contact1.'|'.$result->email.'\');">'.$result->opdrachtgever.'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
alvast bedankt
Gewijzigd op 23/06/2011 09:10:01 door Jeroen van Welzen
Zolang de id AUTO_INCREMENT is, moet ORDER BY id DESC gewoon altijd beginnen bij laatst toegevoegde rijen. De fout zal ergens anders moeten zitten lijkt me. (Correct me if i'm wrong)
en als ik de GROUP BY eruit haal dan staat hij inderdaad boven aan alleen staat dan een opdrachtgever er meer als 1 keer in
Maar je wilt maar 1 rij terug hebben? Dan doe je toch LIMIT 1, zonder de GROUP BY?
Maar volgens mij heb ik een oplossing gevonden
Je zou ook nog DISTINCT kunnen gebruiken (is wel traag)