我如何处理从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.
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
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"]