0

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.

Cengez
  • 11
  • 1
  • 6

2 Answers2

0

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 . "\n";
}

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

Christoph
  • 291
  • 2
  • 7
  • Thanks for the answer. But I already have javascript (jquery) stroke $.post(path, formData, success, "json"); My aim is to get json data from js. I don't want to get data from form through PHP. JUST accept data from js – Cengez Apr 27 '19 at 11:27
  • @Cengez In that case the 'form' data can be serialized using 'serializeArray', turned into JSON and then sent in the POST request. Like so: `var formData = JSON.stringify($("#form").serializeArray());` - that way you will send a single string. Note that 'json' in '$.post' is the format in which you'd like to receive data, not send it. – Christoph Apr 27 '19 at 11:40
  • ok. I edited my php: `$data = $_POST['formData'] ?? '';` just pass name of js variable into $_POST but it doesn't works And change js: `var formData = JSON.stringify($("#form").serializeArray());;` – Cengez Apr 27 '19 at 11:54
  • Try passing the `POST` data as `{formData: formData}`. – Christoph Apr 27 '19 at 11:58
  • Sorry) I can't got you – Cengez Apr 27 '19 at 11:59
  • you mean like that `var data = { fn: "filename", str: "this_is_a_dummy_test_string" }; ` https://stackoverflow.com/questions/406316/how-to-pass-data-from-javascript-to-php-and-vice-versa – Cengez Apr 27 '19 at 12:01
  • I was actually using `$.ajax` instead of `$.post`. Try sending the data this way: `$.ajax({ url: path, method: "POST", data: {formData: formData} }).done(function(res) { success(res); });` – Christoph Apr 27 '19 at 12:01
  • Ok but what to write in php file? – Cengez Apr 27 '19 at 12:05
  • This is a way of sending your form data as JSON. With those changes `$_POST['formData']` should be populated, not empty. After that you can parse the JSON in the php file as you like. – Christoph Apr 27 '19 at 12:07
0

Try changing this name="calc-activity" on all input to maybe name="calc-activity1", name="calc-activity2" etc... and access it in your do using the php $_POST for each name

Example: in your do.php

$_POST[“calc-activity1"] $_POST[“calc-activity2"]

  • Thanks for answer. Good solution. But this stroke of js `var formData = $("#form").serialize();` outputs this `calc-ownership=ooo&calc-activity=restaurant&calc-tax=usn&calc-tax-2=charge&calc-bank=partners&calc-who-payments=client&operations_count=0&calc-nomenclature=slim&documents_count=0&calc-who-docs=client&staff_count=0&calc-more%5B%5D=patent&calc-more%5B%5D=alcohol&period=&price=&price_sber=&rate-name=&email-to=` It means that I need to read this query then work with that – Cengez Apr 27 '19 at 12:11