I wish to have the form to be able to automatically select the radio button based on the logic of HTML/php variable. example show below:
javascript : - function autoForm(value)
-
{
-
var myForm = document.forms["search"];
-
-
if (value == '1')
-
myForm.elements["category"][0].checked = true;
-
if (value == '0')
-
myForm.elements["category"][1].checked = true;
-
}
- <body onload = "autoForm('1')">
html:
[HTML]<form name = "search">
<input type = "radio" name = "category"/>category 1
<input type = "radio" name = "category"/>category 2
<?php
$value = '1'; /* with either 1 or 0 */
?>
</form>[/HTML]
Question:
if I pass value as constant, it will work as shown in next line code,
<body onload = "autoForm('1')"> or <body onload = "autoForm('0')">
if I pass value as variable from php, it doesn't work at all,
<body onload = "autoForm("$value")">
How I can pass value of variable from html/php into Java Script ?
Thanks for ur insight..
10 16470 gits 5,390
Expert Mod 4TB
hi ...
welcome to TSDN ...
you have to use single quotes inside the onload, or no quotes when you are sure that $value is always a number and $value must be printed to the html page (that has to be processed before sent to the client): - <body onload = "autoForm($value);">
-
kind regards
Thanks for ur response.
I tried it either use single quotes inside the onload, or no quotes at all. Both doesn't make Javascript see the value the onLoad try to pass in.
<body onload = "autoForm($RRI);">
<body onload = "autoForm('$RRI');">
However, in HTML/Php portion of code, if I did
echo "<hr><b>the RRI value is: ".$RRI." </b><br />";
On HTML page, it will show
the RRI value is: 1
Still stretch my hair :-(((
Thanks
- <body onload = "autoForm('<?php echo $value; ?>')">
regards
jx2
I tried the following three variation, none of them able to pass the value to javascript : -
<body onload = "autoForm('<?php echo $value; ?>')">
-
<body onload = "autoForm(<?php echo $value; ?>)">
-
<body onload = "autoForm(<?php echo "$value"; ?>)">
-
However, in HTML, this code -
<?php
-
echo "the value is $value</b><br />";
-
?>
will display
the value is 1
Any suggestion is welcomed..
Thanks
gits 5,390
Expert Mod 4TB
hi ...
as i said ... it has to be printed to the html ;) ... (or even echoed :) )
ok ... is $value initialized with a value before you print it to the onload-function? ... try it in firefox and have a look at the js-console ... are here errors in it?
kind regards
Here's my testing script/html:
[HTML]<html>
<head>
<title>Configuration Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function autoForm(value){
var myForm = document.forms["thisform"];
if (value == '1')
myForm.category[0].checked=true;
if (value == '0')
myForm.category[1].checked=true;
}
</script>
</head>
<body onload="autoForm('<?php echo $value; ?>')">
<form name="thisform" method="post" action="next-test.php">
<?php
$value='1';
echo "$value</b><br />";
?>
The selection choice:<br />
Enable:<input type="radio" value="Enable" name="category" >:<br />
Disable:<input type="radio" value="Disable" name="category" >:<br />
<input type="submit" value="submit" name="submit"><br />
</form><br />
[/HTML]
Here's the page display, after the form is executed:
1
The selection choice:
Enable:: <=== both radio button is blank
Disable::
Here's the chunk of form source code display from Firebug, after the form is executed:
[HTML]</script>
</head>
<body onload="autoForm('')">
[/HTML]
In other words, the '1' of $value didn't substitute here, why ?? I don't get it.
Without php assitance in form's source code, for example
<body onload="autoForm('1')">
This will work as it will display exactly the same in firebug.
However, for source code as:
<body onload="autoForm('$value')">
, it won't work as it will display the same in firebug. It treat $value as string char.
Did I miss something terrible ? Still learn how to debug from firebug !
I'm using firefox, no error from firebug console as far as I can tell.
Thanks much for ur help,
Please use CODE tags when posting code. Thanks!
I think you're setting $value afterwards. Move it up to before the body tag.
Yes, u are hitting right on the nail... after I move $value section before the <body> tag, everything works great.
May be I was naive about this, I thought <body onload="> always has to be at top of HTML, and will be executed as last section of html code after form loaded?
<body onload="function()">, I was wrong obviously.
Any pointer on how "onload" is working?
Thanks for everyone's great help and wisdom.
Yes, onload will run on page load, but that's Javascript. PHP will run whilst the page is loading. In actual fact, what's happening is that you're just generating what parameters will be passed to Javascript for the function which will run on page load.
This is what caught me logically. If "Onload" is part of Javascript, and PHP generating the parameters for it, then they are two separate piece and the actual sequence of code line shall not be matter, right ?
Hence, it seems this "onLoad" tend to execute at the beginning of HTML form, not at the end of page reload.
Let's say, if I have HTML "radio button" preset with "checked" after the <input type="radio", checked> , then "onLoad" invoke the JavaScript with "unchecked" to reset it by whatever the logic, then what the form will display ?
If "onLoad" execute first, then the botton will be "checked",
if "onLoad" execute last, then the botton will be "unchecked".
Thanks again for ur thoughtful insight..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: olga |
last post by:
Hi,
On my site, i want to pass a javascript variable to php. I know
that this needs to done in a link or in a post. I want to know if
there is a way i can do it with an html link. I should...
|
by: brett |
last post by:
I need to pass a hard coded value to a JS file:
<script language="JavaScript" src="Tracking.js"
type="text/javascript"></script>
Neither of these methods work:
<script language="JavaScript"...
|
by: umashd |
last post by:
Hi,
I am doing a web based project for my graduation. I studied bit of java
for backend processesing and javascript for the client. Here is the
scenario.
In the FORM, I use INPUT TYPE=text...
|
by: john woo |
last post by:
Hi
in JSP, it's easy to pass value from java-variable to
javascript-variable, like
js_function(a)
{ a=<%java-class.A%>
}
I'm wondering how is the other way around? I tried
|
by: Sean Dockery |
last post by:
I have the following HTML file that I've been using for testing...
<html>
<head>
<script type="text/javascript">
<!--
function handleWindowLoad() {
var items = ;
for (var i = 0; i < 11; i++)...
|
by: weiwei |
last post by:
Hi;
I am having trouble to get variable from grid view. here is my
scenario. I want to delete a row in database from web page, in additon,
I also want to delete that user's directory in c:drive. ...
|
by: Ellie |
last post by:
In my program the user clicks on a link and using the window.open
function, opens a new window.
I pass a url to the new window like this:
<a href="#"...
|
by: pleaseexplaintome |
last post by:
Hi I have the following perl/cgi script snippet. The goal of this
script is to pass a javascript variable to perl where it can be re-used
later. Any help is appreciated, Thanks
...
|
by: IRC |
last post by:
hey, i am pretty new on javascript as well as PHP,
Hey, anyone can you help me, how to pass the javascript array value to
php page.........
i want to retrieve the values which are arrayed on...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |