Javascript将值传递给另一个函数

时间:2016-09-15 16:00:02

标签: php

我正在尝试将搜索值传递给另一个函数。搜索值是动态的,即用户输入。搜索值从api

调用

这是代码 HTML

<html>

<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

</head>

<body> 

<script src="app/d.php"></script>
<script src="app/d.js"></script>

<form id="searchform" onsubmit="searchform(this)">

<input id="searchterm" type="text" class="form-control input-sm" placeholder="Search Directory">

<button type="submit">Submit</button>

</form>

<ul class="nav nav-tabs">
    <li><button id="all" onclick="searchAll(this)">All</button></li> 
    <li><button id="staff" onclick="searchAll(this)">Staff</button></li>
    <li><button id="faculty" onclick="searchAll(this)">Faculty</button></li>
   <li><button id="students"onclick="searchAll(this)">Students</button></li>
 </ul>
    <p id="demo">
        <br></p>
</body>

    <script>


    </script>
</html>

的javascript:

var fil="";
var search="";
function searchAll(filValue){
   fil=filValue.id;
}
function searchform(searchterm){
 search=($('#searchterm').val());
    search(search);
}
function search(search,fil){
$(document).ready(function(){
var query_json = {"search_string":search, "filter":fil};
    json_call(query_json); 
       });

function json_call (values) {

$.post("app/d.php", {values} ,function(response) {
var str = "";
for(var i in response.Contacts){
console.log(response.Contacts[i].displayName);
 str += response.Contacts[i].displayName;
        }
document.getElementById("demo").innerHTML = str;

    }, 'json');
}



 php
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://olddominion.campusdish.com/api/service.svc/menu/locations",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json"),
                 ));
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

如何将用户选择的搜索词值和过滤值传递给搜索功能,以便从API调用值。

0 个答案:

没有答案