There is such a form with javascriptom

<script> $(document).ready(function(){ $('.calc select').change(function(){ $country = $('select#choose-country').val(); if ($country == 'austria') { $koef = 1; } if ($country == 'great-britain') { $koef = 2; } if ($country == 'nederland') { $koef = 3; } $totalprice = $koef * 100; $('#price').text($totalprice); }) </script> 
  <section id="main"> <form method="post" id="ajax_form" action="" > <select class="form-control" name="country" id="choose-country"> <option value="austria">Австрия</option> <option value="great-britain">Великобритания</option> <option value="nederland">Голландия</option> </select> <div class="price-final"> <h5>Цена зависит от страны</h5> <span name="price" id="price">0</span>EUR</div> <button id="calc-price" type="button" class="btn-warning">Посчитать</button> <button id="send-price-to-us" type="submit" class="btn-success">Выслать нам заявку</button> </form> </section> 

After that I send the data using Ajax like this

 <script src="https://какой-то.сайт/calc-price/email.js"></script> 
The script looks like this

 $(document).ready(function() { $("#ajax_form").submit(function() { $.ajax({ type: "POST", url: "/calc-price/e-mail-form/send-form1.php", data: $(this).serialize() }).done(function() { $(this).find("input, span").val(""); alert("Спасибо за заявку! Скоро мы с вами свяжемся."); $("#ajax_form").trigger("reset"); }); return false; }); }); 

Further the class code for sending an email looks like this

 <?php $recepient = "myemail@gmail.com"; $sitename = "mypage"; $country = trim($_POST["country"]); $price = trim($_POST["totalprice"]); $key_to_txt = array('austria' => 'Австрия'); 

Now the question is: The problem is that the $ price variable does not come to me, that is, it is considered to be in the form, but it does not come to me. And the names of the countries also come only in Latin. How can I solve the problem of translation and pulling the result from the form?

  • one
    What do you want? There is no input on the form where the price would be contained. And at select the data from attribute value comes to the server. - u_mulder
  • Is it possible to pull data from a span? - Dima
  • Naturally possible. - u_mulder pm
  • Please tell me how to do this? - Dima
  • There is a text() method. - u_mulder

2 answers 2

The element with name = "price" must be a form element, for example input. If it is required that it cannot be edited then use readonly. You can also collapse it so that it will be like plain text. Well or at the worst to hold span and hidden input.

  • And how to transfer the value of value to the input with a certain ID? I added it and it does not work for me like this $ ('# input-price'). Val (.text ($ price); - Dima
  • there is a syntax error: $ ('# input-price'). val (.text ($ price); - Vladimir Artamonov
  • You need to specify a jquery object: $ ('# input-price'). Val ($ ('# price'). Text ($ price)); - Vladimir Artamonov

Thanks for the advice. Added hidden input near value

 <input name="emailprice" id='price' class='hidden' value=''> 

Passed variable in value by adding a line of code

 $('#price').val($totalprice); 

And I added a code line in php

 $price = trim($_POST["emailprice"]) 

Everything is working. Thanks for the advice.