[英]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.