简体   繁体   English

我有一个调用PHP函数的JavaScript函数。 如何将结果显示在html中而不显示在警报框中?

[英]I have a JavaScript function that calls a PHP Function. How can I get the results to display in the html and not in an alert box?

I have a PHP function that makes a cURL call to an API. 我有一个PHP函数,可以对API进行cURL调用。 I want this call to be triggered by the click of a button using JavaScript. 我希望通过使用JavaScript单击按钮来触发此调用。 How can I get the result to display in the HTML and not in an alert box? 如何使结果显示在HTML中而不在警报框中? My code is as follows: 我的代码如下:

<script>
        function echoSkilljar() {
            alert("<?php skilljarApiCall(); ?>");
        }
        </script>

    <?php
        function skilljarApiCall() {
            // Get cURL resource
            $curl = curl_init();

            // Set some options - passing a username to Skilljar in the URL
            curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => 'https://api.skilljar.com/v1/users/***********/published-courses',
                CURLOPT_HEADER => 0,
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                CURLOPT_USERNAME => 'sk-live-***********'
                ));

                // Send the Request & save response to $response
                $response = curl_exec($curl);

                // Close request to clear up some resources
                curl_close($curl);

                // Trim response string to remove leading and trailing "[ and ]"
                $trimmed = trim($response, '"[');
                $newTrimmed = rtrim($trimmed, ']"');

                // Convert trimmed string to JSON
                $json = json_encode($newTrimmed);

                // Convert JSON to usable associative array
                $newJson = json_decode($json);
                $array = json_decode($newJson, true);

                // Make sure it worked... (fingers crossed);
                // var_dump($array);

                // Yay it worked! Now make sure we can access the elements of the array...
                echo $array['course']['id'] . "/ " . $array['course']['title'];
        }   
    ?>

    <button onclick="echoSkilljar()">Skilljar Stuff</button>
    <div id="info"></div>   

With your code, this part: 使用您的代码,这一部分:

<script>
    function echoSkilljar() {
        alert("<?php skilljarApiCall(); ?>");
    }
</script>

basically gets resolved to some fixed string. 基本上会解析为一些固定的字符串。 Let's assume the result is my-output . 假设结果是my-output That means your HTML looks like this when rendered: 这意味着您的HTML呈现时如下所示:

<script>
    function echoSkilljar() {
        alert("my-output");
    }
</script>

So, in this case, we're just talking about how to make a simple string output in the HTML. 因此,在这种情况下,我们只是在讨论如何在HTML中生成简单的字符串输出。

There are a large number of ways you could go about this. 有很多方法可以解决此问题。 You didn't specify, but I'm going to assume you wanted to put it inside of the <div id="info"></div> . 您没有指定,但我将假设您要将其放在<div id="info"></div>

To do that, we'll just select it and set it's innerText . 为此,我们只需选择它并将其设置为innerText

 <script> function echoSkilljar() { document.getElementById('info').innerText = "my-content"; } </script> <button onClick="echoSkilljar()">Click Me</button> <div id="info"></div> 

Basically, just replace this line: 基本上,只需替换此行:

alert("<?php skilljarApiCall(); ?>");

with this: 有了这个:

document.getElementById('info').innerText = "<?php skilljarApiCall(); ?>";

When rendered, it'll look like this: 渲染后,它将如下所示:

document.getElementById('info').innerText = "my-content";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VB.NET调用Javascript函数。 如何在VB.NET完成之前完成javascript函数? - VB.NET calls Javascript function. How do I get javascript function to complete before VB.NET completes? 如何在JavaScript中添加/附加所有函数调用的结果 - How can I prepend/append the results of all function calls in JavaScript 如何在 HTML 中显示 JavaScript 函数的结果? - How do I display the results of a JavaScript function in HTML? 如何使用JavaScript在警报框中显示特殊字符? - How can I display special chars in an alert box using javascript? 如何获得调用javascript函数的控件? - How can I get the control that calls javascript function? 错误:不是函数。 我该如何解决? - Error : is not a function. How can I fix it? 如何在 JavaScript 中组合函数调用? - How can I combine function calls in JavaScript? 我在警告框中获取了功能代码,为什么会这样? - I get the function code in the alert box, why is this? 当我在Javascript代码中添加特定行时,它会删除HTML函数具有的onclick函数。 - When I add a certain line to my Javascript code it removes the onclick function I have for my HTML function. 假设我有这个javascript函数。 如何将元素传递给它? - Suppose I have this javascript function. How do I pass the element to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM