Jump to content
MakeWebGames

Ajax question


Recommended Posts

Guest Drizzle
Posted

Ok so i have two variables in my javascript/ajax thing. amt and money. I want to know how to check if the amt is greater than a users money. How can i accomplish this?

Guest Drizzle
Posted

Yea. Ok, in the form, i have a hidden field called money, and its value is $ir['money'] which of course is mc's money for each user. i also have the field for amt, which is shown so they can enter an amount. now when i try to do if(amt > money) it doesnt work for some reason. I mean if they put more than they have, it wont deposit becuz of the variables in bank.php, but i need some way to tell them that they dont have enough.

Guest Drizzle
Posted

@zed: Becauses its js, and php variables dont work like they do in php, u need to make a hidden field value or something to check, idk thats why im asking for help.

@danny696: nope tried that.

Posted

First of all, that's not AJAX. JS doesn't mean ajax automatically.

Now what danny proposed it to pass directly the value inside your script. That certainly works (if done correctly). It would help a lot to see your page to see what you are doing wrongly.

Another way (and that's how I do it normally) is to have a section where PHP writes the values of JS variables:

<?PHP
echo "<SCRIPT>\n";
echo "var currentUserGold={ $ir['money']};\n";
echo "</SCRIPT>\n";
?>
<SCRIPT SRC=my.js></SCRIPT>

 

Now why use a static js file? Well first, if you use a good editor, your JS syntax will be checked, and you may even have auto-complete inside JS code. Also, as this part don't change, it make sense to let the browser cache it.

inside the my.js file:

function checkGold(userInput)
{
  var val=parseInt(userInput); // You need to convert to an int if you take the data from a text field.
  if(val > currentUserGold) // Check the local variable against the variable defined by PHP
 	alert('You don't own enough money.');
}
Guest Drizzle
Posted

Source

function showDeposit(){

document.getElementById('cover').style.visibility = 'visible';

document.getElementById('cover').style.width = '100%';

document.getElementById('cover').style.height = '100%';

document.getElementById('deposit').style.visibility = 'visible';

document.getElementById('deposit').style.left = '450px';

document.getElementById('deposit').style.top = '300px';

document.getElementById('deposit').style.color = 'black';

}

JS:

function sendDeposit(){
xmlhttp=new XMLHttpRequest();
amt=document.deposit.amount.value;
money=document.deposit.money.value;
xmlhttp.open('POST','bank.php?action=Deposit');
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send('bank=money&dt='+amt);
}
function hideDeposit(){
document.getElementById('cover').style.visibility = 'hidden';
document.getElementById('deposit').style.visibility = 'hidden';
}
Guest Drizzle
Posted

oh sorry i forgot to put in what was erroring again, i took it out and forgot to put it in. under amt in sendDeposit, there would be a var money=document.deposit.money.value;

then an if(amt > money) { alert("Test"); } and there is a hidden field in the form that is called money, so this should work. but it doesnt :(

Posted

You hidden field cannot be accessed as a variable. You must use some of the DOM commands.

and a form field is access for example by:

document.forms['myform'].myfield.value

or

document.getElementById('myfield_id').value

Guest Drizzle
Posted

ohh. Thanks ill try it. Im new to javascript so i got alot to learn. Thank you though

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...