Guest Drizzle Posted March 10, 2010 Posted March 10, 2010 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? Quote
seanybob Posted March 10, 2010 Posted March 10, 2010 If (amt > money) ... Am I missing something in the question? :P Quote
Guest Drizzle Posted March 10, 2010 Posted March 10, 2010 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. Quote
iSOS Posted March 10, 2010 Posted March 10, 2010 Maybe I'm wrong but wouldn't if($var['amt'] > $ir['money']) { Be fine? Why do you need to put $ir['money'] into a hidden field? Quote
Danny696 Posted March 10, 2010 Posted March 10, 2010 Im quessing you havent looked at js, if(myvr > <?php echo $ir['money'] ?>) { document.write("Hello"); } me thinks Quote
Guest Drizzle Posted March 10, 2010 Posted March 10, 2010 @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. Quote
a_bertrand Posted March 10, 2010 Posted March 10, 2010 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.'); } Quote
Guest Drizzle Posted March 10, 2010 Posted March 10, 2010 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'; } Quote
Guest Drizzle Posted March 11, 2010 Posted March 11, 2010 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 :( Quote
a_bertrand Posted March 11, 2010 Posted March 11, 2010 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 Quote
Guest Drizzle Posted March 11, 2010 Posted March 11, 2010 ohh. Thanks ill try it. Im new to javascript so i got alot to learn. Thank you though Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.