dongyi1982 2018-06-11 07:07 采纳率: 0%
浏览 71
已采纳

PHP输入表单到ajax请求json

I have a file search.php where I get the input from the form and I create an url. I want the data(url_api) to pass it to an AJAX script, where I can request an json. How can I pass the variable api_url to ajax data ?

Here is my code:

if(filter_input(INPUT_POST, 'submit')){

$area=filter_input(INPUT_POST, 'm', FILTER_SANITIZE_STRING);



// The access url is created with data from the form
$api_url = "http://wwww/api/v1/wwww?";

if ($m !== "") {
    $api_url = $api_url . "m=" . $m;
}



$api_url = $api_url . "&api_key=wxaaaaaaaaaaaaaaaaaaaaaaaa";

And the ajax script

 $.ajax({
    url: 'search.php', //This is the current doc
    type: "POST",
    dataType:'jsonp', // add json datatype to get json
    data: ?,
    success: function(data){
        console.log(data);
    }
});
  • 写回答

1条回答 默认 最新

  • dseigqk7443 2018-06-11 07:30
    关注

    In your JS script:

    $.ajax({
        url: 'search.php', //target PHP script
        type: 'POST', //data will be send with POST method
        dataType: 'json', //data will be sent as JSON
        data: { //data sent to PHP script
            key1: 'val1', //keys / values
            key2: 'val2'
        },
        success: function(data) {
            //get the url sent back by PHP and do whatever you want with it
            console.log(data.url);
        }
    });
    

    In your PHP script:

    //get data post by Ajax as POST parameters
    $key1Val = $_POST['key1']; // === 'val1'
    $key2Val = $_POST['key2']; // === 'val2'
    
    //build your $url
    
    //send back built URL to your JS script as JSON
    echo json_encode([
        'url' => $url
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化