admin管理员组文章数量:1278819
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 ?
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 ?
Share Improve this question asked Dec 23, 2013 at 16:40 cyberpirate92cyberpirate92 3,1864 gold badges29 silver badges47 bronze badges 5- 1 Is all the code (HTML/PHP/JavaScript) inside the same page? Why are you not just using the same PHP to put it in the input fields? Seems pointless to use JavaScript here. – putvande Commented Dec 23, 2013 at 16:43
- 1 Where is the javascript located? If it's in it's own file, the PHP won't run. – Dave Zych Commented Dec 23, 2013 at 16:44
- 2 where you have defined the javascript function. is it in the same html file or in a separate js file. – MaNKuR Commented Dec 23, 2013 at 16:44
- @MaNKuR Seperate file " <script lang='text/javascript' src='localhost/Matrix/gd.js'></script>" – cyberpirate92 Commented Dec 23, 2013 at 16:48
-
1
than in this case you have to pass the php variable to that function. Becasue there is no way (as per my knowledge) to send php variable directly to javascript file until its function will be called ... so in this case you have to call that method in this way
<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){} – MaNKuR Commented Dec 23, 2013 at 16:51
5 Answers
Reset to default 3here 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;
}
Your <form>
tag cannot have an onload attribute. This is simply not possible.
Try assigning the onload event to your <body>
element:
<body onload="load_data();">
A better solution would be to directly register the event in the JavaScript code:
window.addEventListener("load", load_data);
For one, you're inserting your PHP data incorrectly. You can very easily produce JS syntax errors by directly dumping arbitrary PHP values into a JS context. You should ALWAYS use json_encode() to produce valid JS text:
name.value = <?php echo json_encode($_SESSION'username']); ?>;
json encoding will take care of any quoting/escaping necessary.
You don't mention HOW this JS is getting included in your site. If it's in (say) an external .js file, then by default this file will NOT be passed through the PHP interpreter, and you'll be sending raw un-executed PHP code to the browser, where it will be seen by the JS parser and rejected as a outright syntax error. That will kill the entire script block and your JS code dies right then and there.
If it is somehow being parsed/executed by the server, then you should directly hit that JS script file with your browser and see WHAT is being output. There could be other structural problems causing JS to ignore the entire script.
If you already have the value of the data within the PHP variable you can just build the required input element with the value already included
<input type="hidden" id="foo" name="foo" value="<?php echo $_SESSION['foo']; ?>"/>
When your event (click/submit) fires the target element will then contain the value
foo = document.getElementById('foo');
console.log(foo.value);
well is very simple, when a web page is loaded the load order they are as follows.
HTML->CSS->JavaScript->PHP.
So if you want to get that variable in your js file, you have to do it this way...
<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>
<script>
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']; ?>";
}
</script>
</head>
<body>
...
</body>
</html>
本文标签: Using Javascript for setting PHP variable as HTML text field39s valueStack Overflow
版权声明:本文标题:Using Javascript for setting PHP variable as HTML text field's value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741242808a2364309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论