John Davidson

javascript - Numbers after whitespace doesn't send in jquery

0 comments
Message:


I'm trying to make a bank system for a game I'm developing. The issue is with whitespace and I have tried out $.trim(); without any success.


This is my Jquery code:


$(document).ready(function(){
$("#money_in").click(function(){
$("#error").empty(); // empty error div
$("#success").empty(); // empty success div

var money = $('#number_bank').val(); // get value from input field

$.post("app/banken/banken_sett_inn.php",
{
money: money
},
function(output, res, status){
if (~output.indexOf("ikke")){ // if output contains 'ikke'
$('<span>' + output + '</span>').appendTo('#error');// add text to error feedback
$("#error").show(); // show error feedback
} else {
var money_in_bank = parseInt(<?php echo AS_session_row($_SESSION['ID'], 'AS_bankmoney', $pdo) ?>) + parseInt(money);
var money_on_hand = parseInt(<?php echo AS_session_row($_SESSION['ID'], 'AS_money', $pdo) ?>) - parseInt(money);

$('<span>' + output + '</span>').appendTo('#success'); // add text to success feedback
$("#success").show(); // show success feedback
$('#saldo').text(numberWithSpaces(money_in_bank) + ' kr'); // Edit numbers for bank
$('#saldo_top').text(numberWithSpaces(money_in_bank) + ' kr'); // Edit numbers for top container
$('#saldo_left').text(numberWithSpaces(money_on_hand) + ' kr'); // Edit numbers for left container

$("#number_bank").val(''); // Empty input field
}
}
);
});
});

Here is my PHP code


<?php

ob_start();
include_once '../../includes/dbh.php';
include_once '../../auth.php';
include_once '../../functions/functions.php';

$money = remove_space($_POST['money']);

if(isset($money)){
$number = $money;
if(number_valid($number) && $number <= AS_session_row($_SESSION['ID'], 'AS_money', $pdo)){
echo "Du satt inn ".number($number)." kr i banken";
} else {
echo "Du kan ikke sette mer penger i banken enn du har på hånden";
}
}

?>

My issue is when I input 10 000 into my numbers-field, it only adds 10 on example #saldo even though it should add 10 000


I have experimented with removing whitespace on var money with $.trim(); but it still won't work. It does edit my database with the correct amount. If I have 10 000 in my bank and I deposit 10 000 it says 20 000 in my database, but the feedback on the site only says 10 010


Thanks in advance!



My Answer:
It looks like the issue might be with how you are handling the input value in your PHP code.

In your PHP code, you are using the `remove_space` function on the `$_POST['money']` value. This function might be removing the whitespace from the number, causing it to be treated as a smaller number than it actually is.

Instead of using the `remove_space` function, you can try using `str_replace` to remove any whitespace from the input value before processing it. Here's how you can modify your PHP code:

php
$money = str_replace(' ', '', $_POST['money']);


By using `str_replace` to remove whitespace from the input value, you should be able to correctly process the full number inputted by the user.

Give this a try and see if it resolves the issue with numbers not being sent correctly in jQuery.

Rate this post

3 of 5 based on 9890 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us