php - Javascript and Cross Origin -
i've been working on lookup using third party api, completes form, submits it, looks record @ third party , returns array.
i've got working on 2 seperate pages i'd roll 1 page , remove need refresh, looked @ converting code , using , xhr request data thrid party have said wont work because don't support cross origin data/lookups.
i know tha php , javascript totally different , php executed server side there way use onclick on button pass form data using php results pass javascript?
any or pointers appreciated
so in short wanted able condense these 2 pages 1 enter lookup details, click submit, runs lookup page 2 , fills in second form.
code follows: page 1:
<?php //login , authtoken $curl = curl_init(); curl_setopt_array($curl, array( curlopt_url => "https://thirdpartyurl.com", curlopt_returntransfer => true, curlopt_encoding => "", curlopt_maxredirs => 10, curlopt_timeout => 30, curlopt_http_version => curl_http_version_1_1, curlopt_customrequest => "post", curlopt_postfields => "{\r\n\"grant_type\" : \"client_credentials\",\r\n\"client_id\" : \"newtocode\",\r\n\"client_secret\": \"passhere\"\r\n}\r\n", curlopt_httpheader => array( "accept: application/json", "authorization: bearer da97e45a4690fb34fe409571a40ef325412d576b", "cache-control: no-cache", "content-type: application/json", ), )); // $response = curl_exec($curl); curl_close($curl); ?> <div id="content" class="full-width" style="padding-left: 100px;"> <p>for demo purposes </p> <form method="post" action="page2.php"> <label for="name">name</label> <input type="text" name="name" value=""> <br/> <label for="email">email</label> <input type="text" name="email" value=""> <br/> <input type="submit" value="submit"> </form> </div>
page 2:
<?php $name1= $_post["name"]; $email= $_post["email"]; $curl = curl_init(); // complete lookup using posted data curl_setopt_array($curl, array( curlopt_url => "https://thirdpartyurl.com/lookup", curlopt_returntransfer => true, curlopt_encoding => "", curlopt_maxredirs => 10, curlopt_timeout => 30, curlopt_http_version => curl_http_version_1_1, curlopt_customrequest => "post", curlopt_postfields => "{\r\n \"name\": \"$name1\",\r\n \"email\":\"$email1\"\r\n}", curlopt_httpheader => array( "accept: application/thirdpartyurl.com.v2+json", "authorization: bearer 6323arad0", "cache-control: no-cache", "content-type: application/thirdpartyurl.com.v2+json", ), )); $response = curl_exec($curl); curl_close($curl); $userdata = json_decode($response, true); ?> <form> <label for="name">name</label> <input type="text" name="name" value=""> <br/> <label for="email">mileage</label> <input type="text" name="email" value=""> <br/> <label for="add1">address1</label> <input type="text" name="add1" value=""> <br/> <label for="add2">address2</label> <input type="text" name="add2" value=""> <br/> </form> <script type="text/javascript"> // pass php array javascript array var userdetails = <?php echo $response ?>; var name = userdetails['user']['personal']['name']; var email= userdetails['user']['personal']['email']; var add1= userdetails['add1']; var add2 = userdetails['add2']; //populate new form document.getelementsbyname("name")[0].value = name; document.getelementsbyname("email")[0].value = email; document.getelementsbyname("add1")[0].value = add1; document.getelementsbyname("add2")[0].value = add2; </script>
Comments
Post a Comment