I have the need to receive javascript variables in PHP so I set up my Ajax call and script, I have verified that AJAX is sending back the proper response it just seems like server side is not wanting to actually pull the information. Here is my javascript
$("#payments2").on("change", function() {
$("#p1").val($(this).find("option:selected").text());
var Id = $(this).find("option:selected").attr('value');
var Ids = $("#payments").find("option:selected").attr('value');
$.post("postit.php", {
Id: Id,
Ids: Ids
}, function(data){
var theResult = data;
}, 'json' );
});
$("#exchange").on("change", function() {
var am = $(this).attr('value');
var fee = $("#fee").attr('value');
var cost = $("#cost").attr('value');
var perc = fee / 100;
var tot = perc * am;
var total = am - tot - cost;
$("#return").val(total);
});
and here is my PHP variable call
<li>
<label for="exchange">Amount to Exchange</label>
<input type="text" id="exchange" name="exchange" maxlength="100">
<span>Amount to Exchange</span>
</li>
<?php
$amounts = $db->query("SELECT * FROM `exchange` WHERE `fromp` = '".$_POST['fromp']."' AND `top` = '".$_POST['top']."'");
$amo = $amounts->fetch_assoc();
?>
<input type="hidden" id="fee" name="fee" value="<?php echo $amo['fee'] ?>" />
<input type="hidden" id="cost" name="pay" value="<?php echo $amo['cost'] ?>" />
<li>
<label for="return">Amount to Receive</label>
<input type="text" id="return" name="return" maxlength="100" value="">
<span>Amount you will receive</span>
</li>
Since I can clearly tell that my Ajax call is sending back the proper information and I am not getting any errors in my query then I am unsure as to why my values for fees
and cost
aren't changing causing me to receive NaN
in my return
text box.
Here is also the contents of my postit.php
file
$Id= isset($_POST['Id']) ? $_POST['Id'] : '';
$Ids= isset($_POST['Ids']) ? $_POST['Ids'] : '';
include "connect.php";
$que = $db->query("SELECT * FROM `exchange` WHERE `fromp` = '".$Ids."' AND `top` = '".$Id."'");
/* Get query results */
$results = $que->fetch_assoc();
/* Send back to client */
echo json_encode($results);
exit;
This is my first time using posting variables from javascript to php so I am sure I have done something wrong, or maybe there is a way to get the information that was passed by the script using javascript, perhaps a way to get the variables I need within my javascript function rather than the mysql query?