我究竟做错了什么?
我正试图从下拉列表中创建一个新的curl请求我做错了什么?
<form action="" method="POST">
<div class="form-group">
<label for="comment">Select Page:</label>
<select class="form-control" name="pagecall" id="pagecall" style="width:75%;float:right;" onchange="davsele(this.value);">
<?php
$totalPage = $dataget['total'] / 20;
for($i=1;$i<=$totalPage;$i++)
{
$selc = ($getPageCount==$i)?'selected':'';
echo "<option value=".$i." $selc>Page No. ".$i."</option>";
}
?>
</select>
</div>
</form>
我试图打电话的功能
<?php
function proCall() {
$ch = curl_init();
$getPageCount == sval;
curl_setopt($ch, CURLOPT_URL,"https://products-export-api.g2a.com/v1/products?page=$getPageCount");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
//Gets the API data
$response = curl_exec($curl);
$err = curl_error($curl);
//curl_close($curl);
$dataget = json_decode($response,true);
print_r($dataget);
echo "test";
curl_close ($ch);
}
?>
什么是从下拉列表发送到js函数
function davsele(sval)
{
var result = "<?php proCall();?>";
alert(result);
return false;
}
你正在以某种方式混合Javascript和PHP,这是行不通的。您尝试在PHP函数中访问JS-Variable(sval),它不起作用。
您需要使用AJAX-Request调用服务器上的PHP函数,并将JS-Variable作为GET参数传递。
因此,您应该将PHP代码放在一个文件中(例如api.php)并在JS-Function中调用它:
function davsele(sval) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("GET", "api.php?value=" + sval, true);
xhttp.send();
}
api.php的内容可能如下所示:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://products-export-api.g2a.com/v1/products?page=' . $_GET['value']);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($curl);
//Gets the API data
$response = curl_exec($curl);
$err = curl_error($curl);
//curl_close($curl);
$dataget = json_decode($response,true);
print_r($dataget);
echo "test";
curl_close ($curl);
?>