1

I have a form where the user submits info (name, address, phone, etc.) and on submit, the data will be inserted into the API url, sent to the API, data is returned and put into hidden fields then posted. Is it possible to hide the AJAX URL inside a php file so I can hide the API KEY so it's not public? Like:

$.ajax({
    url: whitepages.php

Inside whitepages.php

Here is my existing code:

var wpfname = $('#customer_person_name_f input').val();
var wplname = $('#customer_person_name_l input').val();
var wpfullname = wpfname + " " + wplname;
var wpaddress = $('#customer_address_street input').val();
var wpcity = $('#customer_address_city input').val();
var wpstate = $('#address_state_abbr input').val();
var wpzip = $('#customer_address_zipcode input').val();
var wpphone = $('#customer_phone_number input').val();
var wpemail = $('#customer_email_address input').val();

$.ajax({
    url: "https://proapi.whitepages.com/3.3/lead_verify.json?name=" + wpfullname +"&phone=" + wpphone + "&address.street_line_1=" + wpaddress + "&api_key=" + KEYGOESHERE + "&address.city=" + wpcity + "&address.postal_code=" + wpzip + "&address.state_code=" + wpstate + "&email_address=" + wpemail,
    dataType: "text",
    method: "GET",
    crossDomain: "true",
    success: function(data) {

        var json = $.parseJSON(data);
        console.log(json);
        $('#wp_phone_contact_score').val(json.phone_checks.phone_contact_score);
        $('#wp_subscriber_name').val(json.phone_checks.subscriber_name);
        $('#wp_subscriber_age_range').val(json.phone_checks.subscriber_age_range);
        $('#wp_subscriber_address').val(json.phone_checks.subscriber_address.street_line_1 +', ' + json.phone_checks.subscriber_address.city +', ' + json.phone_checks.subscriber_address.state_code + ' ' + json.phone_checks.subscriber_address.postal_code);
        $('#wp_line_type').val(json.phone_checks.line_type);
        $('#wp_is_commercial').val(json.phone_checks.is_commercial);
        $('#wp_address_contact_score').val(json.address_checks.address_contact_score);
        $('#wp_is_active').val(json.address_checks.is_active);
        $('#wp_address_to_name').val(json.address_checks.address_to_name);
        $('#wp_resident_age_range').val(json.address_checks.resident_age_range);
        $('#wp_resident_phone').val(json.address_checks.resident_phone);
        $('#wp_type').val(json.address_checks.type);
    }

<form>
<input id="wp_phone_contact_score" type="hidden" name="customer_wp_phone_contact_score" value="">
<input id="wp_subscriber_name" type="hidden" name="customer_wp_subscriber_name" value="">
<input id="wp_subscriber_age_range" type="hidden" name="customer_wp_subscriber_age_range" value="">
<input id="wp_subscriber_address" type="hidden" name="customer_wp_subscriber_address" value="">
<input id="wp_line_type" type="hidden" name="customer_wp_line_type" value="">
<input id="wp_is_commercial" type="hidden" name="customer_wp_is_commercial" value="">
<input id="wp_address_contact_score" type="hidden" name="customer_wp_address_contact_score" value="">
<input id="wp_is_active" type="hidden" name="customer_wp_is_active" value="">
<input id="wp_address_to_name" type="hidden" name="customer_wp_address_to_name" value="">
<input id="wp_resident_age_range" type="hidden" name="customer_wp_resident_age_range" value="">
<input id="wp_resident_phone" type="hidden" name="customer_wp_resident_phone" value="">
<input id="wp_type" type="hidden" name="customer_wp_type" value="">

<input type="text" name="customer_person_name_f">
<input type="text" name="customer_person_name_l">
<input type="text" name="customer_phone_number">
<input type="text" name="customer_address_street">
<input type="text" name="customer_address_city">
<input type="text" name="customer_address_state_abbr">
<input type="text" name="customer_address_zipcode">
<input type="submit" id="submitBtn" class="btn btn-primary btn-large" value="Submit"/>
</form>
Dawn
  • 175
  • 1
  • 14
  • 4
    Well you could certainly make an AJAX request to your **own** PHP page which stores the key, and then use PHP to make a cURL request to the white pages. Then you would have your PHP page return back the response to AJAX. – Obsidian Age Apr 26 '18 at 21:41
  • `dataType: "text"` and `var json = $.parseJSON(data);`? Just use `dataType: "json"` in the first place. – Quentin Apr 26 '18 at 21:42
  • `crossDomain: "true",` — This is pointless. It matters only if you make a same origion request that is going to get an HTTP redirect to a different origin. (And it should be the boolean `true`, not a string). – Quentin Apr 26 '18 at 21:43

2 Answers2

1

No.

You can't have the browser make an HTTP request with some data in the URL without giving the data to the browser.

You would have to make the HTTP request to the whitepages.com from the server instead of the browser.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Yes, but your user can't directly call whitepages like you are doing in your script.

You will have to write your own script that the user will call using ajax and to use your server to make the request to whitepage.

Either using CURL or file_get_contents() Call a REST API in PHP

All the user will know is that he is sending you his (name, address, phone, etc.).

Lou
  • 866
  • 5
  • 14
  • This sounds like something I'd like to do. I'm really new at PHP so I just need to find a good tutorial to walk me through. – Dawn Apr 26 '18 at 22:14