Set a variable using parseInt is not passing the value

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 4360 Views - Last Post: 18 February 2015 - 07:59 PM

#1 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 12:04 PM

Im really confused. I am trying to pass the value of money to var amount. My console log shows the correct value for money but will not show the value for amount. What am I doing wrong here?
function updateCredit() {

    var amount = parseInt(document.getElementById("money").value);

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
    var xmlhttp = new XMLHttpRequest(); 
      } else if (window.ActiveXObject) { // IE 8 and older  
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
      }

	var data = "credits=" + amount;

    xmlhttp.open("POST","credits.php",true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

console.log(money); // displays 1975
console.log(amount); // displays NaN
console.log(data); // displays "credits=NaN"

	xmlhttp.send(data);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Set a variable using parseInt is not passing the value

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 12:24 PM

What type of element is "money"? (Does it have a value?)
Was This Post Helpful? 1
  • +
  • -

#3 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 02:09 PM

Money starts out at the beginning of the program like this...
money = 2000;


It's used through the program in ways like this ...
var payout=toteAce+(toteAce*acerandomnumber);
 money += payout;
document.getElementById("money").innerHTML = money;         // updateMoney();


And like this...
function updateMoney() {
       if ( pot <= 0 ){ 
       if ( money <= 0 ){ 
       document.getElementById("aaa").innerHTML = "Lost? Here's A Loan !!!";
          money = 1000 ;}
      }
       document.getElementById("money").innerHTML = money;
      }


and this ...
function enterWager(){  // doMath function inside here
var x=parseInt(document.getElementById('textbox').value,10);
       if (money <= 0) {
          x = 0 ; }
document.getElementById("bet").innerHTML = parseInt(x,10);
if(isNaN(x)||x < 1 || x > 250)
	{
    document.getElementById("aaa").innerHTML = "You're Out Of Money!!!";
	}
        document.getElementById("textbox").style.display = 'none';
        document.getElementById("button").style.display = 'none';
function doMath() {
       if (money <= 0) {
          money = 0 ; }
       if (x > money) {
          x = money ; }
      money = money - x;
      pot = pot + x;
    if (wA == 0){  // display wager on tote board for A
    toteAce = toteAce + x;
    toteA.innerHTML = toteAce;
    wA = 1;
                      }


In my first post above I showed that there is a value for money by way of using the console. Here again is the result of the console for money what I ran a test wager of 25. Basically when I wager 25 it subtracts that from 2000 and returns 1975. So as you can see, money is a number, but why wont it pass? I am essentially trying to use AJAX and pass to my php. But why is var amount NOT becoming the same value as money so I can pass it later to php?
console.log(money); // displays 1975
  var amount = parseInt(document.getElementById("money").value);


Should I just pass money? Hey there's an idea, am I being crazy here?

This post has been edited by EdNolan: 28 November 2014 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 02:27 PM

From the information you have provided money is a variable, it isn't an element on the page/ it isn't the id of an element on the page.

This post has been edited by andrewsw: 28 November 2014 - 02:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 02:31 PM

So, I changed my code to this ...
function updateCredit() {
   if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
    var xmlhttp = new XMLHttpRequest(); 
      } else if (window.ActiveXObject) { // IE 8 and older  
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
      }
	var data = money;

    xmlhttp.open("POST","credits.php",true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

console.log(money); // shows 1975
// console.log(amount);
console.log(data); // shows 1975
	xmlhttp.send(data);
}


So now I have data equals money via my console log output. So are you saying I need to make it an element now?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 02:34 PM

Quote

So are you saying I need to make it an element now?

No. All I am saying is that, from the information you have provided, "money" is not the id of an element on your page.
Was This Post Helpful? 0
  • +
  • -

#7 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 02:41 PM

Okay thank you. Let me ask you this now. In order for me to pass var data to php, do I have to do anything like encode/parse, etc.? Just a hint please! I should be able to just use POST now right?

I don't want to pass it in the url and use Get, I would like to use $_POST and it will go over as a $_POST array

I thought money was the id of the element ...
document.getElementById("money").innerHTML = money;   


Should I rename it?

This post has been edited by EdNolan: 28 November 2014 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 03:47 PM

Quote

I thought money was the id of the element ...

Do you, or do you not, have an element with the id of "money" on your page?

Quote

Should I rename it?

Rename what? Why? I cannot answer this question. If you have an element with id of "money", and a variable with the name "money" that refers to the value (assuming it has one) of this element, then I don't know why you would benefit from renaming either. If you do not have an element with the id of "money" then you, obviously, cannot use getElementById to refer to this (non-existent) element.

This post has been edited by andrewsw: 28 November 2014 - 03:49 PM

Was This Post Helpful? 1
  • +
  • -

#9 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 04:13 PM

OMG your confusing the day lights out of me. Is this the element you are speaking of ?
document.getElementById("money").innerHTML = money;   


If so, then yes I do have this element on my page. SO now that I am totally confused can you tell me if the code I posted in post 1, or post 5, is the bases for which I should proceed?

html
<p>Money: $<span id="money"></span><br></p>


I understand ...

JSON.stringify turns an object in to a JSON text and stores that JSON text in a string.
JSON.parse turns a string of JSON text into an object.

I believe I need to pass my ifo as an object but I am not sure if I am correct even after researching. But I have been trying. Another question I have is knowing if I can pass a value somehow into an object I am creating, I tried this but it doesn't work.
var obj ={ data:document.getElementById("money").value };
JSON.parse(obj); // make object


I am not asking for you to give me code, I was just hoping to get a clear understanding of using a POST method and if I needed to pass an object to my php.

I still don't even know why my orig parseInt attempt didn't work and am simply asking for an explanation about how my first post/attempt was a fail.

This post has been edited by EdNolan: 28 November 2014 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#10 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 07:33 PM

I have an object now on client side that I would like to pass to php via POST my question now is, first of all is this the correct way to do this? Secondly, if it is the correct way, when I pass the obj to php do I have to encode or do something on the php side then?

javascript
function updateCredit() {
   if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
    var xmlhttp = new XMLHttpRequest(); 
      } else if (window.ActiveXObject) { // IE 8 and older  
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
      }
	var data = money;  // variable is amount of credits
	var obj = {money:data}; // money is the field in the table

    xmlhttp.open("POST","credits.php",true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

console.log(obj);  // Object { money: 1975 }
console.log(data); // 1975

  xmlhttp.send(obj);
}


php
<?php
include 'members/include/db.php';
$credits = $_POST['credits'];
session_start();
try {
if(isset($_SESSION['username']) && !empty($_POST['credits']))
    $pdo->beginTransaction();{

$stmt = $pdo->prepare("UPDATE usersystem SET money=:money WHERE username =$username");
$stmt->bindValue(':money', $_POST['money'], PDO::PARAM_STR);
$stmt->execute();
}
}
catch(PDOException $e)
{
	$pdo = null;  /*** close the database connection ***/
	echo $e->getMessage();	
}
?>

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 11:41 PM

A SPAN does not have a value (by default), form elements have values.
<span id="money">Show me the money?</span>
<script>
console.log(document.getElementById('money').value);
// undefined
console.log(parseInt(document.getElementById('money').value));
// NaN
document.getElementById('money').value = 50;  // create a new property
console.log(document.getElementById('money').value);
// 50, now it has a value
</script>

Although, it is not a great idea to give an arbitrary element a value-property as it is now easily confused with a form-element, which it isn't.



I am only responding to your original question, someone else could assist you with your additional questions.

This post has been edited by andrewsw: 28 November 2014 - 11:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Set a variable using parseInt is not passing the value

Posted 28 November 2014 - 11:56 PM

Things could have been less confusing if you had answered my first questions:

View Postandrewsw, on 28 November 2014 - 07:24 PM, said:

What type of element is "money"? (Does it have a value?)

Was This Post Helpful? 0
  • +
  • -

#13 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 29 November 2014 - 06:23 AM

Thank you Andrew, So are you saying there is not a value being passed when I simply write it like this? My fault for not looking in to .value more. I just pulled up http://www.w3schools.com and they show a nice example of how I should have written it
Example

Return the value of the selected option in a drop-down list:
var x = document.getElementById("mySelect").selectedIndex;
alert(document.getElementsByTagName("option")[x].value);


For some reason even though I was seeing it not passing a value I assumed .value did something magical and would return the value based on this; property: optionObject.value.
(document.getElementById('money').value)


From all the reading I did yesterday it never dawned on me to try and pass it this way, but I tried this just now and console log shows the correct value.
var data = (document.getElementById("money").innerHTML = money);  // amount of credits
var obj = {money:data}; // money is the field in the table
console.log(document.getElementById("money").innerHTML = money); // 1975


Andrew I am sorry I didn't give a more specific shorter reply to your question. One thing I know is, I didn't know how to answer the question in a shorter response then the initial response I gave. Knowing what I know now; based on your latest example, I understand why you asked, Thank you Andrew.

BTW great name, that's my sons name too!
Was This Post Helpful? 0
  • +
  • -

#14 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 29 November 2014 - 08:21 AM

When I add encodeURIComponent to my var data line the console log displays the data within quotes. My console.log(obj) also reflects the quotes.
var data = encodeURIComponent(document.getElementById("money").innerHTML = money);

var obj = {money:data}; // money is the field in the table

xmlhttp.open("POST","credits.php",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

console.log(obj);  // Object { money: "1975" }
console.log(data); // "1975"
console.log(document.getElementById("money").innerHTML = money); // 1975

xmlhttp.send(obj); // send(string) -- This is used for POST only

This post has been edited by EdNolan: 29 November 2014 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#15 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Set a variable using parseInt is not passing the value

Posted 29 November 2014 - 09:07 AM

I get this error when I run the above in firefox...

NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED: Javascript component does not have a method named: "available"'Javascript component does not have a method named: "available"' when calling method: [nsIInputStream::available]

credits.php
<?php
include 'members/include/db.php';
$credits = $_POST['obj'];
session_start();
try {
if(isset($_SESSION['username']) && !empty($_POST['credits']))
    $pdo->beginTransaction();{

$stmt = $pdo->prepare("UPDATE usersystem SET money=:money WHERE username =$username");
$stmt->bindValue(':money', $_POST['money'], PDO::PARAM_STR);
$stmt->execute();
}
}
catch(PDOException $e)
{
	$pdo = null;  /*** close the database connection ***/
	echo $e->getMessage();	
}
?>


I don't get any errors in my console when run in chrome. Also, when I run in chrome my console outputs ... No quote for data, yet quotes for obj.

console.log(obj); // Object {money: "1975"}
console.log(data); // 1975
console.log(document.getElementById("money").innerHTML = money); // 1975

I don't have a very good understanding here. I know obj has a value. I guess what I am not real clear on is if I need to encode when POST, I also am unclear if JSON.stringify() is required.

Nothing posts to db yet.

Can anyone help please?

This post has been edited by EdNolan: 29 November 2014 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3