# Creating an autopopulate api in phprad classic 2.7.3++

**URL:** <https://forum.radsystems.io/t/creating-an-autopopulate-api-in-phprad-classic-2-7-3/841>\
**Category:** Sample Codes\
**Created:** [April 30, 2022, 12:54pm UTC](https://forum.radsystems.io/t/creating-an-autopopulate-api-in-phprad-classic-2-7-3/841 "2022-04-30T12:54:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [April 30, 2022, 12:54pm UTC](https://forum.radsystems.io/t/creating-an-autopopulate-api-in-phprad-classic-2-7-3/841/1 "2022-04-30T12:54:06Z")

</div>

![image](https://forum.radsystems.io/uploads/default/original/1X/d6dc1b7d1a898c9508c99cddb4235b0374b87dd5.png)  
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](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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/ac8ae71d2ca251d5e08beed4dd53fd8f01c82ab6.png)

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!

 ![image](https://forum.radsystems.io/uploads/default/original/1X/12a3edac00c97323d09ed9494523cf299d964b64.png)

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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/eaed33207d255daaab9d66aae340a6f491650813.png)

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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/ec867bf5dc74cf83971ed7c723448a17936091a0.png)

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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/e067a5918730c20b6713bb0583d40f4b6606e635.png)

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

```auto
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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/f8b1904f700bf86ae8eeaaa4e5f21688b0deb8d1.png)

**let me explain**  
the

```auto
 $("#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

```auto
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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/1f2ca539f704d68cbcd2c19b5683b513f8e0dbe3.png)

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](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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/eaed33207d255daaab9d66aae340a6f491650813.png)  
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](https://example.com/api/getUser/?id=2)**  
! so we now go back to our add page in phprad  
 ![image](https://forum.radsystems.io/uploads/default/original/1X/ffdcbdc46f2697ef90d497ee937c6d90b2f67ed9.png)  
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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/443a4f570ba016c5872d0edd3af7c0e994c6b049.png)  
! 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
