谁通过ajax从PHP函数用curl HTTP响应填充select


who to populate select with curl http response from php function via ajax?

我有以下index.html:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<title>galshan ajax</title>
<script>
$(document).ready(function(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "add.php",
        action: "get_prof",
        data: { "par" : '' },
        dataType: "json",
        async: true,
        success: function(data) {
            var select = $('#mySelect');
            $(data).find('root').each(function(){
                $(this).find('p').each(function(){
                    var textVal = $(this).val('n');
                    var valueVal = $(this).val('i');
                    select.append("<option class='ddindent' value='"+ valueVal +"'>"+textVal+"</option>");
                });
            });
            select.children(":first").text("pick one").attr("selected",true);
        } //sucess close
    }); 
}); 
</script>
</head>
<body>
    <div id="page-wrap">
        <select id="mySelect">
            <option>loading</option>
        </select>
    </div>
</body>
</html>

和add.php文件是:

<?php
function get_prof(){
    $par = isset($_POST['par'])?$_POST['par']:'';
    // where are we posting to?
    $url = 'example.com/GetProf';
    // what post fields?
    $logins = array(
       'clientID' => 'idnum',
       'username' => 'name',
       'password' => 'password',
       'par'      => $par
    );
    // build the urlencoded data
    $postvars = http_build_query($logins);
    // open connection
    $curl_handle = curl_init();
    // set the url, number of POST vars, POST data
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_POST, count($logins));
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postvars);
    // execute post
    $result = curl_exec($curl_handle);
    // close connection
    curl_close($curl_handle);
}
?>

,响应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <p i="9107" n="content01" />
  <p i="9267" n="content02" />
  <p i="9106" n="content03" />
  <p i="9078" n="content04" />
  <p i="9282" n="content05" />
  <p i="1000" n="content06" />
  <p i="1200" n="content07" />
  <p i="9097" n="content08" />
  <p i="1400" n="content09" />
  <p i="9242" n="content10" />
</root>

我基本上试图填充#mySelect选项,文本是n值,实际值是I。我有几个问题:

  1. 我似乎不能从ajax脚本调用add.php文件中的函数,如果我没有在php文件中声明函数,我得到响应,但我似乎不能返回它。我需要做什么从ajax脚本调用php文件中的特定函数?
  2. 当我从外部网站得到响应时,我似乎无法将数据附加为选择元素中的选项,我做错了什么?

这个项目必须在本周完成,如果有人能帮我的话,我会很感激的。

这个html的主要问题是我使用的jquery ver 1.3不能与dataType: "xml"或"json"一起工作。

第二,n和i可以像$(this).attr("i")那样到达,而不是。val。

第三,你不能用ajax执行php函数,只有当你传递一个值给php文件,并定义你想要执行的函数。

我的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" title="main css" type="text/css" media="screen" charset="utf-8">
<title>ajax</title>
<script>
$(document).ready(function(){
    $.ajax({
        type: "POST",
        url: "add.php",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        data: { "par" : "500"},
        dataType: "xml",
        async: true,
        success: function(xml) {
            var select = $('#profSel');
            $(xml).find('root').each(function(){
                $(this).find('p').each(function(){
                    var textVal = $(this).attr("n");
                    var valueVal = $(this).attr("i");
                    select.append("<option class='ddindent' value='"+ valueVal +"'>"+textVal+"</option>");
                });
            });
            select.children(":first").text("בחר ענף").attr("selected",true);
        } //sucess close
    }); 
}); 
</script>
</head>
<body>
    <div id="page-wrap">
        <select id="profSel">
            <option>loading</option>
        </select>
    </div>
</body>
</html>

还在做,会更新的