Pass input values to PHP

Asked

Viewed 684 times

0

I have two inputs, one filled automatically by a PHP script, and the other the user enters the value, and a third input to calculate the subtraction or addition of the first two inputs.

I created the following topics:

Sum values of two inputs and show in third

https://forum.imasters.com.br/topic/559242-somar-valores-de-dois-inputs-e-mostrar-terceiro/#comment-2231252

These two topics I created to try to perform the direct calculation with jquery. But I ended up seeing that the values passed by PHP, from the first input, are interfering with the calculation with jQuery. When I gave alert in the first input it returned a written Alert: 'Undefined'

So I thought to pass the values of the two inputs to a PHP script and perform the calculation there, and then show the result in a DIV turret

But with the jQuery code I put together I’m not getting past the values, someone could help me?

I’ve already researched and found an article about:

Pass Jquery variable to PHP

But I couldn’t fix it.

With the code I made, I wanted the php var_dump to appear in the third input.

This is what HTML looks like:

<div class="form-group" id="formvalor">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Valor <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="valor_total">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="valor" id="valor" value="10" readonly><br>
  </div>
</div>

<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Entrada <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="valEntrada">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="entrada" id="entrada" value="0"><br>
  </div>
</div>

<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Saldo <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="saldo_total">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="saldo" id="saldo" value="" readonly>
  </div>
</div>

The jQuery to pass the values (It can be both GET with POST, for me it makes no difference):

    $("#entrada").change(function(){
  $.post("saldo_total.php",
  {entrada:''+$("#entrada").val()+'', valor:''+$("#valor").val()+''},
  function(valor){
    $("#saldo").html(valor);
  });
});

And PHP: var_dump($_POST);

This was the solution I found to perform the calculation, but if anyone knows of another where I can perform the calculation without PHP values interfering with jQuery they can say.

Could someone help me?

---------------- EDITION ----------------

Now jQuery is like this:

$("#entrada").change(function(){
    $.post("saldo_total.php",
    {entrada:''+$("#entrada").val()+'', valor:''+$("#valor").val()+''},
        function(valor){
            $("#saldo").val(valor);
        });
});

And PHP:

print_r($_POST);

And with that, in the input, appears the $_POST array like this:

Array( [entrada]=>50 [valor]=>undefined )
  • What you mean by "PHP values interfere with jQuery"?

  • That: [value] => Undefined

  • Ever tried to make $("#saldo").val(valor) in place of html?

  • When I take PHP out of that field, and leave a common input (in which the user fills), then yes jquery takes the information that is in it

  • With . val() instead of . html() it does not fill the div I want

  • You are not filling in a div, but an input.

  • See if this is how you say: https://gist.github.com/anonymous/db04466e001795c14b444b302a10f7ca

  • This way the balance value is not updated for PHP return?

  • In PHP I call $_POST inside a print_r. Then in the input it returns the input field in which I typed, and in the value field it continues as Undefined

  • Then edit the question, put the PHP code and this message from print_r that you quoted, because so far nothing is making sense.

  • I edited the question

Show 6 more comments

2 answers

0

I would do with javascript this way passing by putting the onchange in the input:

<input type="text" class="form-control col-md-7 col-xs-12 decimal" name="entrada" id="entrada" value="0" onchange="MUDAVALOR(this.id)">

and in the javascript function:

<script>
function MUDAVALOR(id) {
    var entrada = document.getElementById(id).value;
    var valor = document.getElementById('valor').value;
    entrada = entrada.replace(/\./g,'');
    entrada = entrada.replace(',','.');
    entrada = new Number(entrada);
    valor = valor.replace(/\./g,'');
    valor = valor.replace(',','.');
    valor = new Number(valor);  
    var total = valor + entrada;
    total = total.toFixed(2);
    document.getElementById('saldo').value = total;
}

</script>
  • I tried this way but the sum is not realized, the balance field nor is filled with any value

  • The way I passed works perfectly, create a screen with only your html (with the 3 inputs you passed here) and the javascript function, test because it works, must have missed something, you tested with your Jquery on the page too? because you have to take in this case

0

This probably solves your problem, check what is passed as input value valor by his code php.

(($) => {

  let correctValue = (elementId) => {
    //Javascript não reconhece valores com ',' como floats
    let elem = $('#' + elementId);
    elem.val(elem.val().replace(',', '.'));

  }

  $(document).ready(() => {
    correctValue('valor');

    $('#entrada').on('input', function() {
      correctValue('entrada');

      let valor = parseFloat($('#valor').val()),
        entrada = parseFloat($('#entrada').val()) || 0;

      $('#saldo').val(valor + entrada);

    })
    
  })

})(jQuery)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group" id="formvalor">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Valor <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="valor_total">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="valor" id="valor" value="10" readonly><br>
  </div>
</div>

<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Entrada <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="valEntrada">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="entrada" id="entrada" value="0"><br>
  </div>
</div>

<div class="form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12">Saldo <small>(R$) </small><span class="required">*</span></label>
  <div class="col-md-3 col-sm-3 col-xs-12" id="saldo_total">
    <input type="text" class="form-control col-md-7 col-xs-12 decimal" name="saldo" id="saldo" value="" readonly>
  </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.