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 19
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    php grabbing data from javascript

    Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?

    create_users.php

    <script type="text/javascript">
    var mac = macs.getMacAddress();
    $.get('/create_users.php?mac=' + mac, function() { alert('yeah done'); })
    </script>


    checkAvailability.php

    $mac = $_GET['mac'];
    $mac = mysql_real_escape_string($mac);
    $sql = "INSERT INTO test(mac) VALUES ('$mac')";
    mysql_query($sql);

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What doesn't work, the insert or the callback?

    Where are you performing the connection to the MySQL database?

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    checkAvailability.php will probably do something, if it is called instead of create_users.php.

  • Users who have thanked Dormilich for this post:

    kennedysee (01-25-2010)

  • #4
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Dormilich View Post
    checkAvailability.php will probably do something, if it is called instead of create_users.php.
    Excellent observation, I totally missed that.

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Manage to get it change.... it only works when i manually called up from the browser...
    http://localhost/checkAvailability.php?mac=555

    create_users.php

    Code:
            <form method="post" action="checkAvailability.php">
                <script type="text/javascript">
                        var macs = {
                            getMacAddress : function()
                            {
                                document.macaddressapplet.setSep( "-" );
                				return (document.macaddressapplet.getMacAddress());
                            }
                        }
                    </script>
            
        
            <script type="text/javascript">
            var mac = macs.getMacAddress(); 
            $.get('/createAvailability.php?mac=' + mac, function() { alert('yeah done'); }) 
            </script>
        </form>
    checkAvailability.php

    Code:
      $dbhost = 'localhost';
        $dbuser = 'root';
        $dbname = 'registration';
        mysql_connect($dbhost, $dbuser);
        mysql_select_db($dbname);
        $mac = $_GET['mac'];
        $mac = mysql_real_escape_string($mac);
        $sql = "INSERT INTO test(mac) VALUES ('$mac')";
        mysql_query($sql);
        echo mysql_error();

  • #6
    New to the CF scene
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    code looks good

    do you get any error ?...

    or you can try this code

    <?php

    mysql_connect('localhost', 'username', 'password') or die("Could not connect database");
    mysql_select_db('databasename');
    $mac = $_GET['mac'];
    $mac = mysql_real_escape_string($mac);
    $sql = "INSERT INTO test(mac) VALUES ('$mac')";
    mysql_query($sql);

    ?>

    i think this code will work fine..


    ?>
    Last edited by rajeevbharti; 01-25-2010 at 08:00 AM.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    sure about the URL?
    Quote Originally Posted by kennedysee View Post
    Code:
            <form method="post" action="checkAvailability.php">
                <script type="text/javascript">
                        var macs = {
                            getMacAddress : function()
                            {
                                document.macaddressapplet.setSep( "-" );
                				return (document.macaddressapplet.getMacAddress());
                            }
                        }
                    </script>
            
        
            <script type="text/javascript">
            var mac = macs.getMacAddress(); 
            $.get('/createAvailability.php?mac=' + mac, function() { alert('yeah done'); }) 
            </script>
        </form>

  • #8
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    checkAvailability.php is a piece of php coding to connect to database and inserting data.

  • #9
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Code:
    php grabbing data from javascript
    Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?

    You could write the data to a cookie using Javascript and then read that same cookie with PHP to insert into the database.


    ------------
    Leonard Whistler

  • #10
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    Code:
    php grabbing data from javascript
    Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?

    You could write the data to a cookie using Javascript and then read that same cookie with PHP to insert into the database.


    ------------
    Do you have an example for me?

  • #11
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by kennedysee View Post
    Do you have an example for me?

    PHP Code:
    <html>
    <body>
    <head>
    <script language="JavaScript">

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
                   
    setCookie('cookie_name','Coding Forums','365');
    </script> 
    </head>
    <body>

    <?php
     
    //Defaults to 1
    $javascript_cookie = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
    echo 
    "$javascript_cookie";

    // db insert query
    ?>
    </body>
    </html>

    ------------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    kennedysee (01-26-2010)

  • #12
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    PHP Code:
    <html>
    <body>
    <head>
    <script language="JavaScript">

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
                   
    setCookie('cookie_name','Coding Forums','365');
    </script> 
    </head>
    <body>

    <?php
     
    //Defaults to 1
    $javascript_cookie = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
    echo 
    "$javascript_cookie";

    // db insert query
    ?>
    </body>
    </html>

    ------------
    i'm alittle weak at this, would you be good enough to help me implement the function in??

  • #13
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Everything is tested except for inserting into the database. I don't know what var mac = macs.getMacAddress(); is and if it will work with the script, be sure to insert it below and delete the test variable.

    index.htm or index.php
    PHP Code:
    <script language="JavaScript">

    function 
    setCookie(c_name,value) {
    document.cookie=c_name"=" +escape(value);
    }

    // replace this line with var mac = macs.getMacAddress();
    var mac="test data 1234 5677";

    setCookie('cookie_name',mac);
    window.location "db_insert.php";

    </script> 
    db_insert.php
    PHP Code:
    <?php
     
    //Defaults to 1
    $javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

    mysql_query("INSERT INTO db_name (table_name) VALUES ('$javascript_cookie_value')");
    ?>

    ---------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    kennedysee (01-26-2010)

  • #14
    New Coder
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    Everything is tested except for inserting into the database. I don't know what var mac = macs.getMacAddress(); is and if it will work with the script, be sure to insert it below and delete the test variable.

    index.htm or index.php
    PHP Code:
    <script language="JavaScript">

    function 
    setCookie(c_name,value) {
    document.cookie=c_name"=" +escape(value);
    }

    // replace this line with var mac = macs.getMacAddress();
    var mac="test data 1234 5677";

    setCookie('cookie_name',mac);
    window.location "db_insert.php";

    </script> 
    db_insert.php
    PHP Code:
    <?php
     
    //Defaults to 1
    $javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

    mysql_query("INSERT INTO db_name (table_name) VALUES ('$javascript_cookie_value')");
    ?>

    ---------
    Thanks.

    Even if i replace the coding for var mac = macs.getMacAddress();
    in my MySQL i still see "test data 1234 5677"... why is that it doesnt appear properly in the database?

  • #15
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by kennedysee View Post
    Thanks.

    Even if i replace the coding for var mac = macs.getMacAddress();
    in my MySQL i still see "test data 1234 5677"... why is that it doesnt appear properly in the database?

    You must replace: var mac="test data 1234 5677";

    With: var mac = macs.getMacAddress();




    --------
    Leonard Whistler


  •  
    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
    •