weixin_33724059 2017-11-08 12:29 采纳率: 0%
浏览 40

来自PHP API的自动填充地址

I'm trying to make a autofill for addresses in Javascript. On Google and Stackoverflow I have read a few topics.

However, I don't get it working. I don't get results in my disabled forms.

I use this code:

Index.php

For the form and for the check

<form name="form1" method="post" action="<? echo $PHP_SELF;?>">
<input id="postcode" type="text" class="form-control" name="postcode" placeholder="bijv. 0000AA" required="" aria-required="true">
<input id="number" type="text" class="form-control" name="number" placeholder="bijv. 000" required="" aria-required="true">
<input id="straat" type="text" class="form-control" name="straat" placeholder="" disabled>
<input id="plaats" type="text" class="form-control" name="plaats" placeholder="" disabled>
<input class="btn btn-primary" type="submit" name="submit" value="Controleer" />
</form>

<script type="text/javascript">
$(document).ready(function() {
    function myrequest(e) {
        var name = $('.username').val();
        $.ajax({
            method: "GET",
            url: "pcget.php", /* online, change this to your real url */
            data: {
                postcode: pcode
                number: pnumber
            },
            success: function( responseObject ) {
                alert('success');
                $('#straat').val( 'straat' );
                $('#plaats').val('plaats');
                /*
                once you've gotten your ajax to work, then go through and replace these dummy vals with responseObject.whatever
                */
            },
            failure: function() {
                alert('fail');
            }
        });
    }

    $('#fetchFields').click(function(e) {
        e.preventDefault();
        myrequest();
    });
});
</script>

pcget.php

This is the place for the check.

<?php

include_once '../php/config.php';

$postcode = $_GET['pcode'];
$number = $_GET['pnumber'];

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.postcodeapi.nu/v2/addresses/?postcode=". $postcode ."&number=". $number ."",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/hal+json",
    "x-api-key: <snip>"
  ),
));

$response = curl_exec($curl);

$err = curl_error($curl);

$object = json_decode($response, true);
$postcode = $object['_embedded']['addresses']['0']['postcode'];
$plaats = $object['_embedded']['addresses']['0']['city']['label'];
$straat = $object['_embedded']['addresses']['0']['street'];
$number = $object['_embedded']['addresses']['0']['number'];

if(empty($_POST) === false)
{
echo ''. $plaats .', ';
echo ''. $straat .', ';
}

?>

I get the inspiration from here

  • 写回答

1条回答 默认 最新

  • weixin_33725722 2017-11-09 10:35
    关注

    I have change the code a little bit. But now when I click on the fetch button on the index.php after fill in the postcode and nummer field, I go to the page pcget.php and see the right output. But i want the output in the readonly fields on the Index.php page.

    The code i have now:

    Index.php

    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>
    
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css" type="text/css" rel="stylesheet">
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js" type="text/javascript"></script>
    
    </head>
    
    <body><form method="POST" action="pcget.php">
    <fieldset>
    <legend>Form</legend>
    <label for="postcode">postcode: </label>
    <input type="text" name="postcode" id="postcode"> 
    <label for="postcode">nummer: </label>
    <input type="text" name="nummer" id="nummer"> 
    <button id="fetchFields">fetch</button>
    <label for="plaats">Plaats: </label>
    <input type="text" size="20" name="plaats" id="plaats">
    <label for="straat">Straat: </label>
    <input type="text" size="20" name="straat" id="straat">
    
    
    
    <p><input type="submit" value="Submit" name="submitBtn"></p>
    
    </fieldset>
    </form>
    <script type="text/javascript">
    $(document).ready(function() {
        function myrequest(e) {
            var name = $('.username').val();
            $.ajax({
                method: "GET",
                url: "website.com/pcget.php", /* online, change this to your real url */
                data: {
                    postcode: pcode
                    number: pnumber
                },
                success: function( responseObject ) {
                    alert('success');
                    $('#plaats').val( 'plaats' );
                    $('#straat').val('straat');
                    /*
                    once you've gotten your ajax to work, then go through and replace these dummy vals with responseObject.whatever
                    */
                },
                failure: function() {
                    alert('fail');
                }
            });
        }
    
        $('#fetchFields').click(function(e) {
            e.preventDefault();
            myrequest();
        });
    });
    
    </script>
    
    
    
    </body></html>
    

    pcget.php

    <?php
    
    session_destroy();
    
    include_once '../php/config.php';
    
    PRINT_R($_POST);
    
    $postcode = $_POST['postcode'];
    $number = $_POST['nummer'];
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.postcodeapi.nu/v2/addresses/?postcode=". $postcode ."&number=". $number ."",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "accept: application/hal+json",
        "x-api-key: xxx"
      ),
    ));
    
    $response = curl_exec($curl);
    
    $err = curl_error($curl);
    
    $object = json_decode($response, true);
    $postcode = $object['_embedded']['addresses']['0']['postcode'];
    $plaats = $object['_embedded']['addresses']['0']['city']['label'];
    $straat = $object['_embedded']['addresses']['0']['street'];
    $number = $object['_embedded']['addresses']['0']['number'];
    
    $_SESSION['postcode'] = $postcode;
    $_SESSION['plaats'] = $plaats;
    $_SESSION['straat'] = $straat;
    
    if(empty($_POST) === false)
    {
    echo ''. $plaats .', ';
    echo ''. $straat .', ';
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿