Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript for setting PHP variable as HTML text field's value

I am a learning Javascript and PHP, I am trying to do this , I have stored some variables in a PHP session, i am trying to set the values present in the session as the values in the form's input field, i have used javascript for that purpose,

PHP :

<html>
<head>
    <link rel='stylesheet' type='text/css' href='http://localhost/Matrix/frameCSS.css' />
    <script lang='text/javascript' src='http://localhost/Matrix/gd.js'></script>
</head>
<body>
    <?php
        // ------- --------- -------- -------- ------
        $debug=true;
        dBug("Started Code Execution ... ");
        function dBug($text)
        {
            global $debug;
            if($debug) { echo "<p class='status'>debug : $text</p>"; }
            else { return; }
        }
        // ------ ----------- ------------ -----------
        $db = mysqli_connect("127.0.0.1","user","xyz","sample");
        if(mysqli_connect_errno())
        {
            die(mysqli_connect_error());
        }
        session_start();
        if($_SESSION['is_open']==true)
        {
            $username = $_SESSION['username'];
            dBug('retreived username > '.$username);
        }
    ?>

    <table class="content">
        <form method="POST" action="http://localhost/gen_det.php" onload="load_data()">
           <tr>
               <td>
                    Full Name : 
               </td>
               <td>
                    <input type="text" class="txtbox" id='name'/>
               </td>
           </tr>
           <tr>
               <td>
                    Age : 
               </td>
               <td>
                    <input type="text" class="txtbox" id='age' />
               </td>
           </tr>
           <tr>
                <td>
                    <p class='status' id='status'>&nbsp;</p>
                </td>
           </tr>
           <tr>
               <td colspan="2" align='center'> 
                    <input type="submit" class="btn" onclick="return validateData();" value="save" /> 
                    <input type="button" class="btn" onclick="redirect()" value="cancel" /> 
               </td>
           </tr>
        </form>
    </table>
</body>

Javascript :

function load_data()
{
 var name = document.getElementById('name');
 var age = document.getElementById('age'); 
 name.value = " <?php echo ''.$_SESSION['username']; ?> ";
 age.value = " <?php echo ''.$_SESSION['username']; ?> ";
}

This page is actually inside a iframe in another page (if that is important). When I open this page, it opens , but the values aren't filled in the text fields. What have I done wrong ?

like image 744
cyberpirate92 Avatar asked Jun 04 '26 14:06

cyberpirate92


1 Answers

here is the method

<body onload="javascript: load_data('<?php echo $_SESSION['name']; ?>', '<?php echo $_SESSION['age']; ?>') >

and in the JS file you can define the function

function load_data(param_name, param_age)
{
            var name = document.getElementById('name');
            var age = document.getElementById('age'); 
            name.value = param_name;
            age.value = param_age;
}
like image 90
MaNKuR Avatar answered Jun 07 '26 02:06

MaNKuR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!