Creating an autopopulate api in phprad classic 2.7.3++


hello there! this is a tutorial specifically for the older version of phprad which as of the making of this tutorial it at 2.7.3! currently available at https://phprad.com ! this example will work on all of its releases!
what we will look at!
-MAKING AUTOPOPULATE OF FIELDS BASED ON A SELECTED VALUE
- USING FETCH
-MAKING AND CALLING AN API IN PHPRAD 2.7.3
with that out of the way let get down to business

i have two tables ! us and us2! i want to get data from the us table from us2 table based on a selection! here is my structure

as you can see i have those two tables now let’s make the select drop down
we go to us2 table and create a drop down select!

from my query you can tell im selecting the id and masking it with the name as the label! to technically we are actually working the id here! reason being the id is unique to each record ! so there is no way we can go wrong with it !! think of it this way!
we have two records with the name β€œwahome” if we used the name instead of the id ! then we would get all records with the name wahome! so to only get unique records we use the id as it will never be the same in more than one record! good!

now that we have our select drop down ! you can click publish and confirm your drop down is working

You should now have something like in the above image! ! now we move to the next step! this will involve some hands on coding! we want to now write some javascript or rather the fetchapi !
use case is when we click on a record from the drop down we want to call our api!
but as of now we dont have the api yet! let us first write our javascript code! go to your add page design

now drag and drop a custom view component this is where we will write our javascript
your page should now look like this

great! now we write the javascript! click on the custom view to edit it and write this code in there!

function showCustomer(str) {

        const url = '<?php print_link('api/getUser');?>?id='+str;

            fetch(url)
              .then(response => response.json())
              .then(repos => {
                const reposList = repos.map(repo => repo);
                
                 $("#ctrl-age").val(reposList[0].age)
                 $("#ctrl-location").val(reposList[0].location)
                
              })
            .catch(err => console.log(err))


                                   }

FOR NOW WE HAVE NOT YET CREATED THE API SO IGNORE THE ** const url = β€˜<?php print_link('api/getUser');?>?id=’+str;**

now your custom view should look like this

let me explain
the

 $("#ctrl-age").val(reposList[0].age)
                 $("#ctrl-location").val(reposList[0].location)

in the code **IS JUST JQUERY TO ACCESS IDS MY TABLE FIELDS THAT I WANT TO POPULATE WITH SOME DATA! YOUR FIELDS WILL BE DIFFERENT!! **
assume you have 7 fields in your table you want to populate ! those fields will have to be added here! in phprad all fields have ids starting with ctrl-fieldname where fieldname is the name of the field
great ! moving on! now we create our api! open your project and go to appname/app/controllers/ApiController.php this is where you will write all your apis !
now paste the following code in there

function getUser(){
		
		 $db = $this->GetModel();
$params1 = array($_GET['id']);
		$bool   = $db->rawQuery("SELECT * FROM us WHERE id = ?",$params1);
		$responde= json_encode($bool);
		$respond=json_decode($responde);
		render_json($respond);
		
	}

	

so your controller should look like this now

soo far we are one more step to finishing this thing now!
now remember the code i told you to forget? this one ** const url = β€˜<?php print_link('api/getUser');?>?id=’+str;**
please Now Remember it! haha! okay! if you look keenly in the php function i shared above! your function is called getUser and if you look at this ** const url = β€˜<?php print_link('api/getUser');?>?id=’+str;**
you notice it looks like api/getUser thats exactly what it is! you can access the function from a url! so if you are to share your api url it would be something like https://example.com/api/getUser!! BUUUUUUUT there is a parameter expected! the id of whatever we want to query the database for ~!you see that $_GET[β€˜id’] we have in the code! it basically means there is something you need to enter which should be an id in the database! if you want other things like name assuming its in your database! you do $_GET[β€˜name’] ;
now ladies and gentlemen we are in our last section of our code!
we now want to create an onlick that will get the selected value in the dropdown


this dropdown !
and send the value to our api! that is why we have ** const url = β€˜<?php print_link('api/getUser');?>?id=’+str;** where str is what that will come from the dropdown to complete our link ! so if we select a record it would look like example if id of selected record is 2 https://example.com/api/getUser/?id=2
! so we now go back to our add page in phprad

Click on the custom edit checkbox to display the code and we will add the code to finalize this thing now
in the select add this

onchange="showCustomer(this.value)"
so now your code looks like this


! that is all! and now my friend ! you can now publish and see the autopopulate!! have fun! incase of any questions! leave them in the comments

6 Likes