Page 1 of 1
How to make it happen: Streetname validation
Posted: 2020-08-19 15:20
by RonP
Hi,
In one of my forms you can register yourself and you have to fill out Street name, House number, Zip code.
What I like to offer is that on base of Zip code and House number the street name is been searched and filled into the field "Street name"
That is possible, however how to incorporate this into my form?
Hereby a link to a site that can handle this and the PHP code to be used, but where and how to place this code ???
(this link works for the adresses in The Netherlands!
https://bwnr.nl/pcapi.php
Code: Select all
(change 1234AB for a zip code!)
<?php
$pc = "1234AB";
$hn = 1;
$tv = "a";
$getadrlnk = 'https://bwnr.nl/postcode.php?pc='.urlencode($pc).'&hn='.urlencode($hn).'&tv='.urlencode($tv).'&tg=data';
$result = file_get_contents($getadrlnk);
if ($result=="Geen resultaat.") {echo $result;} else {
$adres = explode(";",$result);
$str = $adres[0];
$pl = $adres[1];
$lat = $adres[2];
$lon = $adres[3];
$gm = $adres[4];
echo "
straat : $str<br>
plaats : $pl<br>
lat : $lat<br>
lon : $lon<br>
googlemaps : $gm<br>";
}
?>
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-08-23 15:37
by RonP
Hi,
Anyone a suggestion / solution for me?
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-01 20:26
by pbottcher
Hi Ron,
you can put this code (+some minor adjustments) as a ajax callable file.
On change (input of the user data) call via ajax the .php file and pass the parameters.
$pc = "1234AB";
$hn = 1;
$tv = "a";
would look something like
$pc = $_GET['pc'];
$hn = $_GET['hn'];
$tv = $_GET['tv'];
and at the end you would add an
echo $str;
So the ajax call would return the street which you can put into the correspoding field.
Hope that gives you a start.
Re: How to make it happen: Streetname validation
Posted: 2020-09-02 15:14
by RonP
Hi Pböttcher,
Thank you for the reaction.
Still a question:
Do I have to place the "Call" in the hooksfile?
Where do I place the .php file
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-02 16:15
by pbottcher
Hi Ron,
you can place the .php file where you want.
Best place would be the hooks directory.
Call the file in your ajax call then with
url: 'hooks/FILENAME.php',
Re: How to make it happen: Streetname validation
Posted: 2020-09-02 16:48
by RonP
Hi,
I'll give it a try.
no guarantee that it will work
In that case.... I'll be back
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-07 14:18
by RonP
Hi,
Well after several tries I got the message:
Parse error: syntax error, unexpected '.', expecting variable (T_VARIABLE) or '{' or '$' in D:\xampp\htdocs\ledenhe-V584\hooks\contacten.php on line 189
What did I do:
1 I've created an extra file within Hooks directory: named:
staatnaam.php
Code: Select all
<?php
//- Passing Postcode en huisnummer naar bwnr.nl, resultaat moet worden: een straatnaam --
$pc = $_GET['Postcode'];
$hn = $_GET['Huisnummer'];
$tv = $_GET['Toevoeging'];
$getadrlnk = 'https://bwnr.nl/postcode.php?pc='.urlencode($pc).'&hn='.urlencode($hn).'&tv='.urlencode($tv).'&tg=data';
$result = file_get_contents($getadrlnk);
if ($result=="Geen resultaat.") {echo $result;} else {
$adres = explode(";",$result);
$str = $adres[0];
$pl = $adres[1];
$lat = $adres[2];
$lon = $adres[3];
$gm = $adres[4];
echo "
straat : $str<br>
plaats : $pl<br>
lat : $lat<br>
lon : $lon<br>
googlemaps : $gm<br>";
}
echo $str;
?>
2. I've added in the hooks file 'contacten'
Code: Select all
function contacten_after_update($data, $memberInfo, &$args) {
function fetchdata(){
$.ajax({
url: 'hooks/straatnaam.php',
type: 'post',
success: function(data){
// Perform operation on return value
alert(data);
}
});
}
What do I miss?
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-07 15:59
by pbottcher
Hi Ron,
just as an idea,
you may call this directly in the
hooks/contacten-dv.js
Code: Select all
$j(function() {
$j('#Postcode, #Huisnummer, #Toevoeging').on('change', function(e) {
var postcode=$j('#Postcode').val().length ? $j('#Postcode').val(): '';
var huisnummer=$j('#Huisnummer').val().length ? $j('#Huisnummer').val(): '';
var toevoeging=$j('#Toevoeging').val().length ? $j('#Toevoeging').val(): '';
if (postcode != '' && huisnummer != '') {
$j.ajax({
url: 'https://bwnr.nl/postcode.php?pc='+postcode+'&hn='+huisnummer+'&tv='+toevoeging+'&tg=data',
type: 'get',
success: function(data){
// Perform operation on return value
alert(data);
}
});
}
});
})
Warning, the code has not been tested, so there might be a typo somewhere.
Re: How to make it happen: Streetname validation
Posted: 2020-09-08 14:45
by RonP
Hi,
This works well

.
I was suprised to see that file `contacten-dv.js`is activated after pushing the "update"button.
However, the folowing step wil be to get the right part into the form / database
(Always a following question )
What I get back from Java is:
In this date I just need the first 2 fields to be passed to:
How is that be done?
Sorry but my programming skills are poor
Re: How to make it happen: Streetname validation
Posted: 2020-09-08 19:49
by pbottcher
Hi Ron,
instead of
you can try
Code: Select all
var result=data.split(';');
$j('#Straatnaam').text(result[0]);
$j('#Woonplaats').text(result[1]);
Re: How to make it happen: Streetname validation
Posted: 2020-09-09 16:13
by RonP
Hi,
Thank you, it partially works
When I fill out the form the Street name and City is searched correctly and shown into the appropriate places in the form.
However it won't be updated into the database.
The field definitions are, according to me, correct.
Hereby some screenshots.
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-11 18:47
by pbottcher
Hi Ron,
so, you have couple options to get the data into the database.
one would be to have 2 additional input fields that you catch in the after_update hook.
e.g.
expand
var result=data.split(';');
$j('#Straatnaam').text(result[0]); // I guess it needs to be $j('#adres').text(result[0]);
$j('#Woonplaats').text(result[1]);
to
Code: Select all
var result=data.split(';');
$j('#Adres').text(result[0]);
$("<input>").attr({name: "hiddenAdres", id: "hiddenAdres", type: "hidden", value: result[0] }).appendTo("form");
$j('#Woonplaats').text(result[1]);
$("<input>").attr({name: "hiddenWoonplaats", id: "hiddenWoonplaats", type: "hidden", value: result[1] }).appendTo("form");
In your after_update function you can add
Code: Select all
$update_string="";
if (isset($_REQUEST['hiddenAdres']) $update_string=" Adres='".$_REQUEST['hiddenAdres']."', ";
if (isset($_REQUEST['hiddenWoonplaats']) $update_string.=" Woonplaats='".$_REQUEST['hiddenWoonplaats']."'";
if (strlen($update_string) > 1) sqlvalue("UPDATE contacten set ".$update_string." where id="$date['selectedID']); // Assuming the PK is id
The code is not tested, so there might be some typos.
Re: How to make it happen: Streetname validation
Posted: 2020-09-12 12:38
by RonP
Hi Pascal,
Thank you to deliver the codes, however there is an error in the first line in the "insert_after_update" section.
Parse error: syntax error, unexpected '$update_string' (T_VARIABLE) in D:\xampp\htdocs\ledenhe-V584\hooks\contacten.php on line 201
This is what I have "adjusted", I thought that will do.
Code: Select all
// Submitting found "Adres" en "Woonplaats" to database: 12-09-2020 , Pascal Böttcher
$update_string="";
if (isset($_REQUEST["hiddenAdres"]) $update_string=" Adres='".$_REQUEST["hiddenAdres"]."', ");
if (isset($_REQUEST["hiddenWoonplaats"]) $update_string.=" Woonplaats='".$_REQUEST["hiddenWoonplaats"]."'");
if (strlen($update_string) > 1) sqlvalue("UPDATE `contacten` set ".$update_string." where `id`="$date['selectedID']); // Assuming the PK is id
I've added an "
) " after the first 2 isset lines AND changed (without luck) 'hiddenAdres' to "hiddenAdres" and 'hiddenWoonplaats' to "hiddenWoonplaats"
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-12 17:47
by pbottcher
Hi Ron,
sorry., true there is a missing ), but it need to be set earlier.
Code: Select all
// Submitting found "Adres" en "Woonplaats" to database: 12-09-2020 , Pascal Böttcher
$update_string="";
if (isset($_REQUEST["hiddenAdres"])) $update_string=" Adres='".$_REQUEST["hiddenAdres"]."', ";
if (isset($_REQUEST["hiddenWoonplaats"])) $update_string.=" Woonplaats='".$_REQUEST["hiddenWoonplaats"]."'";
if (strlen($update_string) > 1) sqlvalue("UPDATE `contacten` set ".$update_string." where `id`="$date['selectedID']); // Assuming the PK is id
Re: How to make it happen: Streetname validation
Posted: 2020-09-13 12:41
by RonP
Hi,
The error is gone
However the data, after update, isn't stored into the database yet.
So I've checked the code, as far as I good, but I can’t find a solution.
Hereby I've the code made more readable for me, hopefully you can help me out again
Code: Select all
// Submitting found "Adres" en "Woonplaats" to database: 12-09-2020 , Pascal Böttcher
$update_string="";
if (isset($_REQUEST["hiddenAdres"])) $update_string.=" Adres=" .$_REQUEST["hiddenAdres"] ."', ";
if (isset($_REQUEST["hiddenWoonplaats"])) $update_string.=" Woonplaats=" .$_REQUEST["hiddenWoonplaats"]."', ";
if (strlen($update_string) > 1) sqlvalue("UPDATE `contacten` set ".$update_string." where id= " . $data['selectedID'] . ";");
I've made the end of the line""hiddenwoonplaats" equal to the line "hddenAdress"
I've changed $date into $data and closed that line with . ";"
(against better judgment

)
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-13 12:43
by RonP
Addition:
"Woonplaats" isn't filled out on the form
Ron
Re: How to make it happen: Streetname validation
Posted: 2020-09-14 06:42
by pbottcher
Hi Ron,
new try:
contacten-dv.js:
Code: Select all
$j(function() {
$j('#Postcode, #Huisnummer, #Toevoeging').on('change blur keyup', function(e) {
var postcode=$j('#Postcode').val().length ? $j('#Postcode').val(): '';
var huisnummer=$j('#Huisnummer').val().length ? $j('#Huisnummer').val(): '';
var toevoeging=$j('#Toevoeging').val().length ? $j('#Toevoeging').val(): '';
if (postcode != '' && huisnummer != '') {
$j.ajax({
url: 'https://bwnr.nl/postcode.php?pc='+postcode+'&hn='+huisnummer+'&tv='+toevoeging+'&tg=data',
type: 'get',
success: function(data){
// Perform operation on return value
var result=data.split(';');
$j('#Adres').text(result[0]);
$j("<input>").attr({name: "hiddenAdres", id: "hiddenAdres", type: "hidden", value: result[0] }).appendTo("form");
$j('#Woonplaats').text(result[1]);
$j("<input>").attr({name: "hiddenWoonplaats", id: "hiddenWoonplaats", type: "hidden", value: result[1] }).appendTo("form");
}
});
}
});
})
contacten.php (after_insert and after_update)
Code: Select all
if (isset($_REQUEST["hiddenAdres"])) $update_string.=" Adres='" .$_REQUEST["hiddenAdres"] ."', ";
if (isset($_REQUEST["hiddenWoonplaats"])) $update_string.=" Woonplaats='" .$_REQUEST["hiddenWoonplaats"]."'";
if (strlen($update_string) > 1) sqlvalue("UPDATE `contacten` set ".$update_string." where id= " . $data['selectedID']);
Re: How to make it happen: Streetname validation
Posted: 2020-09-14 10:11
by RonP
Pascal,
You MADE MY DAY
Thank you for being patience with me and your great support.
Ron