473,385 Members | 1,445 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.

Converting php variable into javascript variable

Hi all,
How to convert a php variable into the javascript variable. I need this because i want to display the content of php variable in the textbox of html field.

With regards
Aug 29 '07 #1
10 16791
pbmods
5,821 Expert 4TB
Heya, Kumuda.

You can display a PHP variable in a textbox like this:
Expand|Select|Wrap|Line Numbers
  1. <textbox><?php echo $variable; ?></textbox>
  2.  
If you absolutely need to transport the PHP variable to Javascript, simply create JavaScript code to generate the variable:
Expand|Select|Wrap|Line Numbers
  1. echo '
  2. <script type="text/javascript">
  3. // <![CDATA[
  4.     var myVar = "' . $variable . '";
  5. // ]]>
  6. </script>';
  7.  
Aug 29 '07 #2
Hi all,
How to convert a php variable into the javascript variable. I need this because i want to display the content of php variable in the textbox of html field.

With regards

No need to go with java script....try this
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $i='value';
  3. echo '<input type="text" value ='.$i.'>' 
  4. ?>
Aug 29 '07 #3
pbmods
5,821 Expert 4TB
AMT, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 29 '07 #4
Hi pbmods,

I tried the code
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.     / <![CDATA[
  3.  
  4.           var myVar = "' . $Name . '";
  5.           alert(myVar);
  6.  
  7.       // ]]>
  8. </script>
  9.  
But this is not working, it is just displaying the '.$Name.' but the contents of variable. Please help me.

With regards
Aug 30 '07 #5
Atli
5,058 Expert 4TB
Hi.

PHP and JavaScript do not work together. PHP is executed Server-side before your JavaScript code is executed on the client's browser.
You need to use PHP to generate the JavaScript code.

Like this:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.     / <![CDATA[
  3.  
  4.           var myVar = "<?php echo $Name; ?>";
  5.           alert(myVar);
  6.  
  7.       // ]]>
  8. </script>
  9.  
Aug 30 '07 #6
Thank you Atli

It is working.
Aug 30 '07 #7
Hi all,

I have one more issue.

I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
I have used the following code where 'SelectSP' is the name of the dropdown box
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3.           var Name = "<?php echo $name; ?>";
  4.           document.getElementById('SelectSP').value=Name;
  5.  
  6. </script>
Aug 30 '07 #8
Hi all,

I have one more issue.

I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
I have used the following code where 'SelectSP' is the name of the dropdown box

Expand|Select|Wrap|Line Numbers
  1.  <script language="javascript">
  2.  
  3.           var Name = "<?php echo $name; ?>";
  4.           document.getElementById('SelectSP').value=Name;
  5.  
  6. </script> 

An dhere rhe html code

Expand|Select|Wrap|Line Numbers
  1.    <tr>
  2.      <td align="right" > 
  3.         <label id="lblSelctSP">Please select the service provider :
  4.      </td>
  5.     <td align="left">
  6.         <select name="SelectSP">
  7.         <option value="" selected>[Select]
  8.         <option value="SP1">Sp1
  9.         <option value="SP2">SP2
  10.         <option value="SP3">Sp3
  11.         </select> 
  12.      </td>
  13. </tr>
Aug 30 '07 #9
ak1dnar
1,584 Expert 1GB
kumuda v,
You have posted more than 10 posts to the Forum so far, But you have missed the CODE tags while posting source codes. Please use them in the future.Thanks.
Aug 30 '07 #10
Atli
5,058 Expert 4TB
Hi all,

I have one more issue.

I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
...
You can use PHP to dynamically build your dropdown box.

Consider this code:
Expand|Select|Wrap|Line Numbers
  1. <select name="mySelect">
  2. <?php
  3.   for($i = 0; $i < 3; $i++) {
  4.     echo "\n\t<option value=\"$i\">Item #$i</option>";
  5.   }
  6. ?>
  7. </select>
  8.  
Which would output something like this
Expand|Select|Wrap|Line Numbers
  1. <select name="mySelect">
  2.   <option value="0">Item #0</option>
  3.   <option value="1">Item #1</option>
  4.   <option value="2">Item #2</option>
  5. </select>
  6.  
Aug 30 '07 #11

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

Similar topics

1
by: Stefano | last post by:
Posted: Fri Jan 23, 2004 3:59 pm Post subject: Variable Javascript -------------------------------------------------------------------------------- Hi all, i have a problem with...
2
by: Matt | last post by:
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas??
4
by: tnhoe | last post by:
Hi, if I have both javascript and aspscript, how to get/retrieve the variable value in javascript into asp variable ? regards hoe
3
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Keep in mind that i am not a javascript programmer but php. Here is...
3
by: stahl.karl | last post by:
I have a CGI/Perl program that returns a string output. Is it possible to get this into a Javascript variable, where the name of the variable is defined in the Javascript and not in the Perl code?...
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: sumanbangladesh | last post by:
hi I am new both in php and javascript. I have a javascript varialbe. But I need a php variable with the same contents.How can I do it. thanks in advance
7
by: Arsale | last post by:
I am trying to pass a javascript variable to php on javascript generated page, but on the new page I got only "Array". Where did I go wrong? On first page, i've got image map with areas....
1
by: shinu bhaskar | last post by:
how to assign a JavaScript variable to a jsp variable in a single page Ex : <script type="text/javascript"> var MM=Calculate_Form('<%=DATE_INSTALL%>','<%=TILLDATE%>'); alert(MM); </script>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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 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.