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
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:
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"?
– Woss
That: [value] => Undefined
– Gabriel
Ever tried to make
$("#saldo").val(valor)
in place ofhtml
?– Woss
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
– Gabriel
With . val() instead of . html() it does not fill the div I want
– Gabriel
You are not filling in a div, but an input.
– Woss
See if this is how you say: https://gist.github.com/anonymous/db04466e001795c14b444b302a10f7ca
– Gabriel
This way the balance value is not updated for PHP return?
– Woss
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
– Gabriel
Then edit the question, put the PHP code and this message from
print_r
that you quoted, because so far nothing is making sense.– Woss
I edited the question
– Gabriel