473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to pass HTML/php variable into JavaScript

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:

Expand|Select|Wrap|Line Numbers
  1. function autoForm(value)
  2. {
  3. var myForm = document.forms["search"];
  4.  
  5. if (value == '1')
  6.               myForm.elements["category"][0].checked = true;
  7. if (value == '0')
  8.               myForm.elements["category"][1].checked = true;
  9. }
Expand|Select|Wrap|Line Numbers
  1. <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..
Aug 21 '07 #1
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):

Expand|Select|Wrap|Line Numbers
  1. <body onload = "autoForm($value);">
  2.  
kind regards
Aug 21 '07 #2
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
Aug 21 '07 #3
jx2
228 100+
Expand|Select|Wrap|Line Numbers
  1. <body onload = "autoForm('<?php echo $value; ?>')">

regards
jx2
Aug 21 '07 #4
I tried the following three variation, none of them able to pass the value to javascript:
Expand|Select|Wrap|Line Numbers
  1. <body onload = "autoForm('<?php echo $value; ?>')">
  2. <body onload = "autoForm(<?php echo $value; ?>)">
  3. <body onload = "autoForm(<?php echo "$value"; ?>)">
  4.  
However, in HTML, this code
Expand|Select|Wrap|Line Numbers
  1.                  <?php
  2.                  echo "the value is $value</b><br />";
  3.                  ?>
will display
the value is 1

Any suggestion is welcomed..

Thanks
Aug 22 '07 #5
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
Aug 22 '07 #6
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,
Aug 22 '07 #7
acoder
16,027 Expert Mod 8TB
Please use CODE tags when posting code. Thanks!

I think you're setting $value afterwards. Move it up to before the body tag.
Aug 22 '07 #8
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.
Aug 22 '07 #9
acoder
16,027 Expert Mod 8TB
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.
Aug 23 '07 #10
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..
Aug 23 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
3
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"...
2
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...
3
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
10
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++)...
0
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. ...
6
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="#"...
2
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 ...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.