How do I pass data from a javascript to php and mysql
Hello, I'm absolutely new to php and mysql and I'm making a dynamic signature. But I need to get values from my javascript to php or in my mysql database. What code do I implement in my javascript and what do I code in php. (it's for game stats) how do I secure the users username with a password so that only that user can increment their stats.
I have a table in mysql with 5 columns 'username', 'time ran', 'expgained', 'total urns').
As I said I absolutely don't know how to get the data from my script to the php and in my mysql database.
after that, how do I get the signature to display all the data?
Last edited by Rabbe; 08-03-2012 at 07:34 PM..
Reason: mistyped
Notice that I did *NOT* include a "username" field. I would *ASSUME* that the user has already logged in and that you have saved the user's id as a SESSION value in PHP.
Now...you *can* send data to PHP from JavaScript code using AJAX, but before you get to that stage you need to learn how to use PHP and MySQL. There is no point in sending a letter to somebody who doesn't know how to read. Same thing with AJAX: There is no point in JavaScript sending data to PHP when you don't know how to write PHP code to read and process that data.
So... Go learn PHP and MySQL *first*. And how to handle simple <form> posting. *THEN* come back and maybe you will want to use AJAX (but maybe not...maybe you don't need it).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
that's why I came here, to learn those things. I have no idea where to start otherwise. The gui of my script asks for a username and a password (Don't know how to create an account so that you can only update the stats if you have the correct username and password). then the javascript does it's thing on a game and shows stats in a paint. now I want these stats to be stored and every time the user runs the script the stats get added. but the problem is, as I said, how do I get those stats in my database?
You need to learn that part of it, first, before you start worrying about the browser side of things.
You say "the gui of my script...". Well, where does it *TEST* that username and password? Where is the code for that? At the point where the user passes that test, presumably in PHP code, you should set a SESSION value to indicate WHO the session belongs to. You might set the username in the SESSION, but a userid would be even better.
And then every time information is sent from the browser to the server, you use that SESSION information as the key under which to store the data.
You need to work out your database design *FIRST*, before anything else. Then you need to develop a PHP page that will accept data and store it in the appropriate table under the appropiate key (that is, under username or userid). Fortunately, you can test all this without getting JavaScript involved at all.
For example, say you had a PHP page that accepted a query string like this:
and then just used the info in that querystring, along with the SESSION user key, to save the data.
So...step by step. But the very first step is to design the database table or tables you need.
If the game already has some tables defined (it surely has, to support user login if nothing else) then learn to use some database tool (such as phpMysqlAdmin, but there are others) to investigate the DB, find what tables are there, and design and setup your additional table(s).
One step at a time. There's no magic wand you can wave that will do it all at once.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Well, first of all, none of this now has anything to do with JavaScript, so it's in the wrong forum.
And what you are asking for is several pages of coding. And I have no idea what your database looks like. For example, is it even a MySQL database?
If this is some game you are trying to *modify*, you probably really should contact the author(s) for how to get started.
But in any case, nobody is going to be able to help you until you can provide them with:
(1) A schema of your existing database, showing all tables and fields (or at least relevant fields).
(2) The existing PHP code that supports the user login process.
Once you have found all that, post in the PHP forum for more help.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
well my original question was how do I get the data from my java script to the database
2) yes it is a mysql database:
this is the code in my javascript (I'm not modifying a game, the script does things in the game for you)
Code:
username = new JTextField();
passwordField1 = new JPasswordField();
siginfo = new JButton();
private void siginfoActionPerformed(ActionEvent e) {
progressBar1.setValue(100); //this is the "save" button I don't know what to code here so that it sends the username and password to the php, let alone how to "hash" the password.
}
The first step on your journey is to NOT use an integer as a password
id should be the primary key and, preferably, use AUTO_INCREMENT. username should be UNIQUE as well.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
I've stepped down from the password idea, so now it back to javascript. what code do I enter so that the username that is entered in the JTextfield is sent to my php file? and how do I send my other data directly in the database
username = new JTextField();
passwordField1 = new JPasswordField();
siginfo = new JButton();
private void siginfoActionPerformed(ActionEvent e) {
progressBar1.setValue(100); //this is the "save" button I don't know what to code here so that it sends the username and password to the php, let alone how to "hash" the password.
}
Is neither PHP nor JavaScript.
It appears to be *JAVA* code.
And it LOOKS to be Java APPLET code.
If I am right, then you are in the wrong forum. You need to be in a JAVA forum. And one specifically for Java Applets. (There isn't any such forum here at coding forums. There's a Java forum, but it's mostly for JSP--Java Server Pages--not for clientside Java applets.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
When the submit (or Save Info) button is pressed it posts the data to the script 'saveStatistics.php'. This php script would read the $_POST[] data, clean it, validate it and, eventually, store it in your database.
@Old Pedant: good spot on the Java - I missed that!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 08-04-2012 at 01:48 AM..
Intercommunication between multiple langauges is one of the more difficult tasks overall. There have been leaps and bounds in what can be done though.
You have to choose what you want to do with this. You can go simple.
If you want to POST to a PHP form, you can access the form script by using the HttpUrlConnection class. Here's a quick example of that: http://www.exampledepot.com/egs/java.net/post.html
SOAP is also an option for communication. Instead of providing a get or post, PHP provides a wsdl and Java can consume this to make calls to PHP's soap servers. Soap is far more difficult to set up properly, but when it does it works awesome as it is almost like making native calls to the application. This looks like a good read on that: http://oreilly.com/catalog/javasoap/chapter/ch05.html
In any case, you need to learn how to either send and accept the http post/get data in both java and PHP, or learn how to use something different such as soap in both languages. The key here is that you are going to need to learn both languages to do this.