www.webdeveloper.com
Results 1 to 5 of 5

Thread: accessing database values in javascript

Hybrid View

  1. #1
    Join Date
    Feb 2010
    Posts
    3

    accessing database values in javascript

    Well

    The situation is we are using mysql.We have written the queries and extracted the result from the assoc array.Now how do we access the result from javascript.

    $query = "SELECT firstname FROM employees;
    $result = mysql_query( $query , $resource );
    $value= mysql_fetch_assoc( $result);
    extract( $value );

    Now does the javascript code be like
    <script type="text/javascript">
    function somefunction(){
    var databasefield = document.getElementsByName("firstname").value;
    }
    </script>

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    It all depends on what you want to do. If you are just trying to use the info in javascript then you could always do something like
    <script type="text/javascript">
    var databasefield = '<?php echo $firstname; ?>';
    </script>


    However, if the field can change because it is in a textbox then you can access the info from the form by document.yourformname.formelementname.value.

    I think what is best if you try to give us more detail about what exactly you are trying to accomplish because just saying you want to access information from database with javascript could be done in many different ways.
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  3. #3
    Join Date
    Feb 2010
    Posts
    3
    Okay the thing is i have to compare the value the user entered in a textfield to the value entered in the database.

    i am storing the textfield value in a php variable and when ever the user moves out of the field i am using the onBlur event and triggering a javascript function.In this javascript function i have to do a comparison and based on the result there should be an alert.

    Quote Originally Posted by skywalker2208 View Post
    It all depends on what you want to do. If you are just trying to use the info in javascript then you could always do something like
    <script type="text/javascript">
    var databasefield = '<?php echo $firstname; ?>';
    </script>


    However, if the field can change because it is in a textbox then you can access the info from the form by document.yourformname.formelementname.value.

    I think what is best if you try to give us more detail about what exactly you are trying to accomplish because just saying you want to access information from database with javascript could be done in many different ways.

  4. #4
    Join Date
    Feb 2010
    Posts
    3
    I am sorry,the textfield the user entered is compared to the value retrieved from the db.
    Quote Originally Posted by pswathi View Post
    Okay the thing is i have to compare the value the user entered in a textfield to the value entered in the database.

    i am storing the textfield value in a php variable and when ever the user moves out of the field i am using the onBlur event and triggering a javascript function.In this javascript function i have to do a comparison and based on the result there should be an alert.

  5. #5
    Join Date
    Mar 2007
    Posts
    946
    Then I would suggest doing one of two things. Either use ajax and have it return the value you need or do something like this
    PHP Code:
    <?php
    $firstname 
    query database for first name
    ?>
    <script type="text/javascript">
    function compare() {
    var databasefield = '<?php echo $firstname?>';
    }
    </script>
    If you are checking more then one value like the first name and last name, etc then you use the json_encode php function.

    Then you could do something like
    PHP Code:
    <?php
    $user_data 
    = array('firstname' => 'Joe''lastname' => 'Smith');
    ?>
    <script type="text/javascript">
    var user_data = <?php echo json_encode($user_data); ?>;
    alert(user_data.firstname); //This creates a pop with the name Joe in it.
    </script>
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles