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'> </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 ?
<body onload="load_data('<?php echo ''.$_SESSION['username']; ?>', ' <?php echo ''.$_SESSION['username']; ?> ');">and then you can access this in js function as params i.e function load_data(name, age){}