dongyou6909 2019-04-27 10:52
浏览 139
已采纳

我如何处理从javascript传递的php中的数据(jquery)

I added two inputs in my html

<form  method="post" id="form">
    <input type="radio" name="calc-ownership" id="calc-ownership-1" value="ooo">
   <label for="calc-ownership-1" class="left-border">ООО</label>
    <input type="radio" name="calc-ownership" id="calc-ownership-2" value="ip">
    <label for="calc-ownership-2" class="right-border">ИП</label>

   <input type="radio" name="calc-activity" id="calc-activity-1" value="service">
   <label for="calc-activity-1" class="col-1">Услуги</label>

   <input type="radio" name="calc-activity" id="calc-activity-2" value="building">
   <label for="calc-activity-2" class="col-2">Строительство</label>

   <input type="radio" name="calc-activity" id="calc-activity-3" value="trade">
   <label for="calc-activity-3" class="col-1">Торговля оптовая и розничная</label>

    <input type="radio" name="calc-activity" id="calc-activity-4" value="restaurant">
   <label for="calc-activity-4" class="col-2">Ресторанный бизнес</label>

   <input type="radio" name="calc-activity" id="calc-activity-5" value="production">
   <label for="calc-activity-5" class="col-1">Производство</label>

   <input type="radio" name="calc-activity" id="calc-activity-6" value="other">
   <label for="calc-activity-6" class="col-2">Прочее</label>

And I added some logic with js.

$('.form_calc .btn_wrap .btn').click(function(){
    var path = '/intercompforme2/do.php';
    var formData = $("#form").serialize();
    var success = function( response ){
    if (response.status == "OK") {
        $('#result0').text(response.rate);
    $('#result1').text(response.sber);
    $('#result2').text(response.classic);
    $('input[name=price]').val(response.classic);
    $('input[name=price_sber]').val(response.sber);
    $('input[name=rate-name]').val(response.rate);
    if (response.rate == "ИП УСН") {
        $('.period').text('год');
        $('input[name=period]').val('год');
    }else {
        $('.period').text('месяц');
        $('input[name=period]').val('месяц');
    }
        $('.form_calc .results_wrap, .form_calc .btns_wrap').slideDown();
       }else {
           alert ("Ошибка. Обратитесь к разработчику");
       }
   }
   $.post(path, formData, success, "json");
   return false;
});

I want to pass this formData to my do.php. But I don't know how to accept and work with this data. Do PHP have global variables to accept?

I tried to just

$data = $_POST['calc-ownership'] ?? '';

$fp = fopen('log.txt', 'w+');
fwrite($fp, $data);

Then it succesfully writes to log.txt input value of calc-ownership. But I need to pass not just one input value. I want to pass all fromData with js.

  • 写回答

2条回答 默认 最新

  • duanchigeng4313 2019-04-27 11:19
    关注

    If you're not sure what data will be sent in a 'POST' request you can just loop through the '$_POST' array in the php file and do whatever you like with the data:

    foreach($_POST as $field=>$data) {
        echo $field . ": " . $data . "
    ";
    }
    

    From PHP manual, '$_POST' is:

    An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

    Link: https://www.php.net/manual/en/reserved.variables.post.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line