![]() |
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? |
Ummm...by learning PHP and MySQL?
Nothing to do with JavaScript. You just have a <form> that you post to a PHP page. e.g., Code:
<form action="saveStatistics.php" method="post">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). |
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?
|
As I said, using PHP and MySQL.
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: Code:
saveStatistics.php?timeRan=37&expGained=5&totalUrns=3So...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. |
okay could you help me with the session? I've read up on mysql and php on w3schools and I have an Idea of what to do.
so only questions that remain is how to have that session. How do I make "accounts" for each user so that it updates only that users data. and how do I update the data? (so add the data from the past session to the data that already exists in the table)? thx for explaining it a bit so far EDIT: it doesn't test the username and password, how do I do that? |
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. |
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: http://i45.tinypic.com/2rwoylf.png 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();hope this helps you with some stuff |
The first step on your journey is to NOT use an integer as a password :eek:
id should be the primary key and, preferably, use AUTO_INCREMENT. username should be UNIQUE as well. |
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
|
Ummm...what PHP code??? What JavaScript code?
This code: Code:
username = new JTextField();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.) |
As Old Pedant posted:
Code:
<form action="saveStatistics.php" method="post">@Old Pedant: good spot on the Java - I missed that! |
Quote:
|
kk sorry for posting in the wrong section.
|
Code:
private void siginfoActionPerformed(ActionEvent e) { |
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. |
| All times are GMT +1. The time now is 04:44 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.