How to make it happen: Streetname validation

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

How to make it happen: Streetname validation

Post by RonP » 2020-08-19 15:20

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

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-08-23 15:37

Hi,
Anyone a suggestion / solution for me?
Ron

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-01 20:26

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.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-02 15:14

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-02 16:15

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',
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-02 16:48

Hi,
I'll give it a try.
no guarantee that it will work :x
In that case.... I'll be back

Ron

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-07 14:18

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-07 15:59

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.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-08 14:45

Hi,
This works well :D .
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:

Code: Select all

alert(data);
De Brink;Heiloo;52.58926429223;4.70971613058;https://www.google.nl/maps?q=loc:52.589 ... 0971613058
In this date I just need the first 2 fields to be passed to:

Code: Select all

'Straatnaam' en 'Woonplaats'
How is that be done?

Sorry but my programming skills are poor

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-08 19:49

Hi Ron,

instead of

Code: Select all

alert('data');
you can try

Code: Select all

var result=data.split(';');
$j('#Straatnaam').text(result[0]);
$j('#Woonplaats').text(result[1]);
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-09 16:13

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
Attachments
Before_update.PNG
Before_update.PNG (10.72 KiB) Viewed 4698 times
After_update.PNG
After_update.PNG (7.67 KiB) Viewed 4698 times
Definition_Street.PNG
Definition_Street.PNG (29.2 KiB) Viewed 4698 times
Definition_City.PNG
Definition_City.PNG (30.24 KiB) Viewed 4698 times
Structure_Mysql.PNG
Structure_Mysql.PNG (12.22 KiB) Viewed 4698 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-11 18:47

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.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-12 12:38

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-12 17:47

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
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-13 12:41

Hi,
The error is gone :D
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

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-13 12:43

Addition:
"Woonplaats" isn't filled out on the form
Ron

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to make it happen: Streetname validation

Post by pbottcher » 2020-09-14 06:42

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']);
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
RonP
Veteran Member
Posts: 219
Joined: 2013-08-27 13:36
Location: Heiloo, The Netherlands
Contact:

Re: How to make it happen: Streetname validation

Post by RonP » 2020-09-14 10:11

Pascal,

You MADE MY DAY :idea: :)
Thank you for being patience with me and your great support.

Ron

Post Reply