With the help of a plugin I add a shortcode to WordPress, which inserts a form for calculating on a page. The server part of the form is such a function:
function ajax_calc(){ // ΠΡΠΈΠ΅ΠΌ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° POST Π·Π°ΠΏΡΠΎΡΠ° if(isset($_POST['action']) && $_POST['action'] == 'ajax_calc'){ //ΡΠΈΡΠΈΠ½Π°, ΡΠΊΡΠ°Π½ΠΈΡΡΠ΅ΠΌ Π²ΡΠ΅ Π»ΠΈΡΠ½Π΅Π΅, ΡΠ±ΠΈΡΠ°Π΅ΠΌ ΠΏΡΠΎΠ±Π΅Π»Ρ ΠΈ Π·Π°ΠΌΠ΅Π½ΡΠ΅ΠΌ "," Π½Π° ".", Ρ.ΠΊ. ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ ΠΌΠΎΠΆΠ΅Ρ Π²Π²Π΅ΡΡΠΈ Π»ΡΠ±ΠΎΠΉ ΠΈΠ· Π·Π½Π°ΠΊΠΎΠ² $width = str_replace(',','.',trim(htmlspecialchars($_POST['width']))); //Π΄Π»ΠΈΠ½Π° $length = str_replace(',','.',trim(htmlspecialchars($_POST['length']))); //Π²ΡΡΠΎΡΠ° $height = str_replace(',','.',trim(htmlspecialchars($_POST['height']))); $m_rul = str_replace(',','.',trim(htmlspecialchars($_POST['m_rul']))); $k_zap = str_replace(',','.',trim(htmlspecialchars($_POST['k_zap']))); // Π²ΠΎΠ·Π²ΡΠ°ΡΠΈΠΌ ΠΎΡΠΈΠ±ΠΊΡ, Π΅ΡΠ»ΠΈ ΠΏΡΠΈΡΠ΅Π΄ΡΠΈΠΉ ΠΊ Π½Π°ΠΌ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ Π½Π΅ ΡΠΈΡΠ»ΠΎ if(!(float)$width || $width ==''){ echo implode(array('loadmsgerr'=>'Error! Width should be number only!')); } elseif(!(float)$length || $length ==''){ echo implode(array('loadmsgerr'=>'Error! Lenght should be number only!')); } elseif(!(float)$height || $height ==''){ echo implode(array('loadmsgerr'=>'Error! Height should be number only!')); } else{ // ΠΈΠ½Π°ΡΠ΅ Π²Π΅ΡΠ½Π΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ $Perimeter = ($width + $length) * 2 ; //ΠΏΠ΅ΡΠΈΠΌΠ΅ΡΡ $Square = $Perimeter * $height; //ΠΏΠ»ΠΎΡΠ°Π΄Ρ Π±ΠΎΠΊΠΎΠ²ΡΡ
ΠΏΠΎΠ²Π΅ΡΡ
Π½ΠΎΡΡΠ΅ΠΉ // Π΄ΡΡΠ³ΠΈΠ΅ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ, ΠΏΡΠ΅Π΄ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΡ... // Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡ ΠΌΠ°ΡΡΠΈΠ² Ρ Π΄Π°Π½Π½ΡΠΌΠΈ $it_test = 10*$k_zap + $m_rul; $answers = array('S' => $Square, 'P' => $Perimeter, 'it_test' => $it_test); echo json_encode($answers); } } } And JS with this:
jQuery(document).ready(function(){ // Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ ΠΏΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΠΊΠ°Π»ΡΠΊΡΠ»ΡΡΠΎΡΠ° jQuery('#calc #form_calc_id').on('submit', function(e){ jQuery('#calc .result').show().text(ajax_calc_object.loadingmessage);//ΠΠ±ΡΠ°Π±ΠΎΡΠΊΠ°... //AJAX Π·Π°ΠΏΡΠΎΡ ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ POST Π½Π° ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ url. Π’ΠΈΠΏ Π΄Π°Π½Π½ΡΡ
ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ ΠΊΠ°ΠΊ json jQuery.ajax({ type: 'POST', dataType: 'json', url: ajax_calc_object.ajaxurl, data: { //ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅ΠΌ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΠΎΡΠΌΡ Π² ΡΠ°Π±Π»ΠΎΠ½Π΅ 'action': 'ajax_calc', //Π²ΡΠ·ΠΎΠ² Π½Π°ΡΠ΅ΠΉ ΡΡΠ½ΠΊΡΠΈΠΈ 'width': jQuery('#calc #width').val(), 'length':jQuery('#calc #length').val(), 'height': jQuery('#calc #height').val(), // 'security': jQuery('#security').val() 'm_rul': jQuery('#pa_ΠΌ-Π²-ΡΡΠ»ΠΎΠ½Π΅').text(), 'k_zap': jQuery('#pa_k-zap').text(), }, success: function(data){ var resperr = data.loadmsgerr;//ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π·Π°Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ if(!resperr){//Π΅ΡΠ»ΠΈ Π½Π΅Ρ ΠΎΡΠΈΠ±ΠΎΠΊ json.parse(data); jQuery('#calc .result').html(data.S + data.P); //Π²ΡΡΠ°Π²Π»ΡΠ΅ΠΌ Π² ΡΠ΅Π³ Ρ ΠΊΠ»Π°ΡΡΠΎΠΌ .result ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠΉ ΠΎΡΠ²Π΅Ρ jQuery('#ΠΠ±ΠΎΠΈ').val(); } else{ jQuery('#calc .result').text(resperr); //ΠΈΠ»ΠΈ ΡΡΠ΄Π° ΠΆΠ΅ Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΠΎΡΠΈΠ±ΠΊΡ } }, error: function(xhr, textStatus, errorThrown) {//ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΠΈ Π²ΡΠ²ΠΎΠ΄ Π² ΠΊΠΎΠ½ΡΠΎΠ»Ρ ΠΎΡΠΈΠ±ΠΎΠΊ if (xhr.status != 0) { var msg = ' (' + xhr.status + ') '; if (textStatus) msg += ': ' + textStatus; if (xhr.status < 200) { msg = 'AJAX Informational ' + msg; } else if (xhr.status < 300) { msg = 'AJAX Success ' + msg; } else if (xhr.status < 400) { msg = 'AJAX Redirection ' + msg; } else if (xhr.status < 500) { msg = 'AJAX Client Error' + msg; } else { msg = 'AJAX Server Error' + msg; } console.log(msg); } else { console.log(errorThrown); } } }); e.preventDefault(); }); }); The problem is that all this does not display anything on the page. An error is written to the console.
AJAX Success (200): parsererror
And in the data transferred from the function, I see the line
{"S": "", "P": "", "it_test": "19"} 0
So, I suspect that the whole problem is exactly this 0 at the end of the json line. But where does he come from, I donβt understand at all. Maybe someone familiar with the mechanism?