Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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">
    <input name="timeRan"/>
    <input name="expGained"/>
    <input name="totalUrns" />
    <input type="submit"/>
    </form>
    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.

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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=3
    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.

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Rabbe; 08-03-2012 at 10:19 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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.

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    		}
    gui:

    hope this helps you with some stuff

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #9
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by Rabbe; 08-04-2012 at 01:25 AM.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Ummm...what PHP code??? What JavaScript code?

    This code:
    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.
    		}
    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.

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    As Old Pedant posted:

    Code:
    <form action="saveStatistics.php" method="post">
    <input name="timeRan"/>
    <input name="expGained"/>
    <input name="totalUrns" />
    <input type="submit"/>
    </form>
    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!
    Last edited by AndrewGSW; 08-04-2012 at 01:48 AM.
    "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

  • #12
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,696
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...what PHP code??? What JavaScript code?

    […]

    It appears to be *JAVA* code.
    That’s what I was gonna say, too. That code looked suspicious to me. I’m gonna move this thread to the Java forum.

  • #13
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    kk sorry for posting in the wrong section.

  • #14
    New Coder
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    private void siginfoActionPerformed(ActionEvent e) {
    			progressBar1.setValue(100);
    			
    			String playername = username.toString();
    			char[] password = passwordField1.getPassword();
    			int totalurns = urns;
                            int exp = expgained;
    			Timer timeran = runTime;
    		}
    this is what I have, is that correct if you look at the table? now how do I submit it to a php page?

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •