shadowayex Posted January 4, 2008 Share Posted January 4, 2008 Ok, in PHP you can send info in a text input to a PHP variable using either the $_GET or $_POST scripts. I'm writing a little profit calculator in JavaScript and I got everything written, but I can't test it because I don't know what the JavaScript equivalent of those are. I have a button to call the function that holds the calculator code, so there's no <form> tag. Do I need one? Or how do I do it? Link to comment Share on other sites More sharing options...
Ingolme Posted January 4, 2008 Share Posted January 4, 2008 If you give each <input> tag an ID attribute you can access them like this:<script type="text/javascript">function alertValue() { alert(document.getElementById("something").value);}</script><input type="text" id="something" /><input type="button" onclick="alertValue()" value="click" /> Link to comment Share on other sites More sharing options...
shadowayex Posted January 4, 2008 Author Share Posted January 4, 2008 Yay. Thank you! Link to comment Share on other sites More sharing options...
shadowayex Posted January 5, 2008 Author Share Posted January 5, 2008 Another thing, not required for my operation but just a little twist that would help. Ok I have an idea that I want to try to make my profit calculator easier to use. What the calculator does is you type in your item name (whatever you want) and put in the Buying Price, the Selling Price, and how many you're going to buy and resell. So the HTML looks just like this: Item Name: <input type="text" id="itemtwo" value="" /> Buying Price: <input type="text" id="buytwo" value="" /> Selling Price: <input type="text" id="selltwo" value="" /> Ammount Traded: <input type="text" id="amnttwo" value="" /><br />Item Name: <input type="text" id="itemthree" value="" /> Buying Price: <input type="text" id="buythree" value="" /> Selling Price: <input type="text" id="sellthree" value="" /> Ammount Traded: <input type="text" id="amntthree" value="" /><br />Item Name: <input type="text" id="itemfour" value="" /> Buying Price: <input type="text" id="buyfour" value="" /> Selling Price: <input type="text" id="sellfour" value="" /> Ammount Traded: <input type="text" id="amntfour" value="" /><br />Item Name: <input type="text" id="itemfive" value="" /> Buying Price: <input type="text" id="buyfive" value="" /> Selling Price: <input type="text" id="sellfive" value="" /> Ammount Traded: <input type="text" id="amntfive" value="" /><br />Item Name: <input type="text" id="itemsix" value="" /> Buying Price: <input type="text" id="buysix" value="" /> Selling Price: <input type="text" id="sellsix" value="" /> Ammount Traded: <input type="text" id="amntsix" value="" /><br /> But if that works, there's a new problem. I don't want to write a function to calculate each different ID. Originally I wanted to keep the id's all the same for each one of the items, but when I tried it, it only calculated the first one (I figured it would, but it was worth a shot right?). Any ideas? Link to comment Share on other sites More sharing options...
boen_robot Posted January 5, 2008 Share Posted January 5, 2008 Group each item into it's own "div" and access this div somehow before rushing into the ID of the item. And to access each div, you can again use IDs, or perhaps use "class"es instead. Link to comment Share on other sites More sharing options...
shadowayex Posted January 5, 2008 Author Share Posted January 5, 2008 Ok well I got it to work, it's just when I launch the function it reloads the page with just the things I had it write. If there any way to make the function put the stuff in a certain place? My function looks like this: function additem(){ var numofitem=prompt("How many items would you like to add? \(You may only do this once per calculation.\)","Number of items"); if (numofitem>0 && numofitem<10) { var t=numofitem+1; var r=t-1; var s=r/10; var q=s+1; var i=1; for (i=1;i<=q;i++) { document.write("Item Name: "); document.write("<input type=\"text\" "); document.write("id=\"item" + i + "\" "); document.write("value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;"); document.write("Buying Price: "); document.write("<input type=\"text\" "); document.write("id=\"buy" + i + "\" "); document.write("value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;"); document.write("Selling Price: "); document.write("<input type=\"text\" "); document.write("id=\"sell" + i + "\" "); document.write("value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;"); document.write("Ammount Traded: "); document.write("<input type=\"text\" "); document.write("id=\"amnt" + i + "\" "); document.write("value=\"\" size=\"15\" />"); document.write("<br />"); } document.write("<input type=\"button\" value=\"Add Items\" disabled=\"disabled\" />"); document.write("<input type=\"button\" onclick=\"calculate()\" value=\"Find Profit\" />"); }} Don't mind all the operations and variables. For some reason it was multiplying the inputted number by 10 before adding the one so I had to do a little manipulation and stuff. But I'll probably fix that once I get it to put the rows in the right place. The calculate() function is still incomplete due to the glitch so I won't put that. But the HTML looks like this: <body>Item Name: <input type="text" id="item" value="" size="15" /> Buying Price: <input type="text" id="buy" value="" size="15" /> Selling Price: <input type="text" id="sell" value="" size="15" /> Ammount Traded: <input type="text" id="amnt" value="" size="15" /><br /><input type="button" onclick="additem()" value="Add Items" /><input type="button" onclick="calculate()" value="Find Profit" /></body> Any ideas? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 8, 2008 Share Posted January 8, 2008 Ok well I got it to work, it's just when I launch the function it reloads the page with just the things I had it write. If there any way to make the function put the stuff in a certain place?Don't use document.write, either access an element by ID and use the innerHTML property, or create a new element and use document.appendChild to add it to the page.For some reason it was multiplying the inputted number by 10 before adding the one so I had to do a little manipulation and stuff.That's because the value is a string, you need to convert it to a number before you do the addition. Since it's a string Javascript concatenates a "1" onto the end of it instead of adding the two values. Use parseInt or parseFloat to convert it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.