doutonghang2761 2018-05-29 13:13
浏览 172
已采纳

向php请求信息

I'm trying to request information on a php file and insert a html code on the <div id="1"... but it doesn't show errors and don't insert nothing to the div. Its something wrong at AJAX code, or the problem is in the html?

PD: The php extract correctly the information from json file.

PHP

<?php
$jsonContents = file_get_contents('../data/data.json');
$data         = json_decode($jsonContents, true);

foreach ($data as $key => $value) {
    echo($value['url']);
};

html

<!Doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>SSL Checker</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom box-shadow">
    <h5 class="my-0 mr-md-auto font-weight-normal">SSL Checker</h5>
</div>

<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
    <h1 class="display-4">SSL Checker</h1>
    <p class="lead">SSL Checker</p>
</div>

<div class="container">
    <div class="row">
</div>
    <footer class="pt-4 my-md-5 pt-md-5 border-top">
        <div class="row">
            <div id="1" class="col-12 col-md">
                <form method="GET" onsubmit="start()">
                    <button type="submit" class="btn btn-lg btn-block btn-primary">Contact us</button>
                </form>
            </div>
        </div>
    </footer>
</body>
</html>

JS

function start() {
    $.ajax({
        type: 'GET',
        url: '/api/domain/showall.php',
        success: function (response) {
            document.getElementById("1").innerHTML = '<input type="text" value="<?php  foreach ($data as $key => $value) {echo($value['url']);?>'
        }
    });
}
  • 写回答

2条回答 默认 最新

  • douzhaocheng4533 2018-05-29 15:00
    关注

    You should take into consideration what @Quentin explained to you. Now if you want to access your php data on your ajax success function you can do the following:

    First on your php you must return the data you want to access via js. so:

    <?php
        $jsonContents = file_get_contents('../data/data.json');
        $data         = json_decode($jsonContents, true);
    
        return $data;
    

    Now since data is JSON, you need to specify that your ajax response is in JSON format adding 'dataType: 'json' to your ajax:

    $.ajax({
        type: 'GET',
        url: '/api/domain/showall.php',
        dataType: 'json',
        success: function (data) {
            // data contains all the info you need from PHP
            // Now you will need to loop through data and append it to your html
        }
    });
    

    You can check this question on how to loop through json data using jquery

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀