473,382 Members | 1,390 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,382 software developers and data experts.

calling a variable in javascript from php or vice versa

1
i need help with referencing variables from php to a javascript or vice versa.

my pages are written in php primarily. i needed a user response - therefore i used javascript and used confirm() to get the appropriate response.
the user response is needed inside the php for further calculations.

does anybody have any idea?

any kind of help would be much appreciated.
thanks,
:confused:
Mar 23 '06 #1
6 8094
Banfa
9,065 Expert Mod 8TB
php runs server side and javascript runs client side

There is no point using a confirm as this just makes it harder, use a form and submit the form to a php script. Depending on the submit method (GET or POST) the data in the form will appear in the php superglobal $_GET or $_POST which are arrays the indexes of which will be the names you give your form element.
Mar 23 '06 #2
Hi

You can directly embed php in java script

like
<script language="javascript">
function x()
{
<?php

ur php code


?>


}

then execute where ever u want

onclick or onsubmit or any where if u need more help just inform me

</script>
Jun 28 '06 #3
Hi

You can directly embed php in java script

like
<script language="javascript">
function x()
{
<?php

ur php code


?>


}

then execute where ever u want

onclick or onsubmit or any where if u need more help just inform me

</script>

hey i have:

<SCRIPT LANGUAGE="JavaScript">
function test(){
<?php

echo "hi";
?>
}
</SCRIPT>

<a href="javascript:test();">test</a>

but it doesn't work. :(
Jul 5 '06 #4
Banfa
9,065 Expert Mod 8TB
hey i have:

<SCRIPT LANGUAGE="JavaScript">
function test(){
<?php

echo "hi";
?>
}
</SCRIPT>

<a href="javascript:test();">test</a>

but it doesn't work. :(
You are using 2 different scripting languages, PHP and Javascript.

PHP runs server side and Javascript (normally) runs client side so by the time the browser has loaded a page the php has already run, the Javascript runs after the browser has loaded the page.

The effect of your code above is that the browser sees a page with the following in it

[html]
<SCRIPT LANGUAGE="JavaScript">
function test(){
hi }
</SCRIPT>

<a href="javascript:test();">test</a>
[/html]

"hi" is not valid Javascript so it doesn't work. You could have

[php]
<SCRIPT LANGUAGE="JavaScript">
function test(){
document.write(<?php echo '"hi"'; ?>);
}
</SCRIPT>

<a href="javascript:test();">test</a>
[/php]

or

[php]
<SCRIPT LANGUAGE="JavaScript">
function test(){
var string = <?php echo '"hi"'; ?>;
document.write(string);
}
</SCRIPT>

<a href="javascript:test();">test</a>
[/php]
Jul 10 '06 #5
iam_clint
1,208 Expert 1GB
<? $test = "test"; ?>
Expand|Select|Wrap|Line Numbers
  1.  <SCRIPT LANGUAGE="JavaScript">
  2. function test(){
  3.     var string = "<?=$test?>";
  4.     alert(string);
  5. }
  6. </SCRIPT>
  7.  
  8. <a href="javascript:test();">test</a> 
  9.  
  10.  
this would be kind of passing a variable..
Jul 17 '06 #6
Hey,

Produce more incredible result if you write your script like this:

print
"<script language=javascript>
function test()
{
var string='$test';
alert(string);
}
</script>
";



<? $test = "test"; ?>
Expand|Select|Wrap|Line Numbers
  1.  <SCRIPT LANGUAGE="JavaScript">
  2. function test(){
  3.     var string = "<?=$test?>";
  4.     alert(string);
  5. }
  6. </SCRIPT>
  7.  
  8. <a href="javascript:test();">test</a> 
  9.  
  10.  
this would be kind of passing a variable..
Nov 7 '06 #7

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

Similar topics

1
by: Dharmendra Singh | last post by:
Hi I'm using .Net(C#) and working on the form(Screen) which have text boxes for both arabic and english data to store. So i want to change the language at run time from arabic to english and...
4
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have...
12
by: Mal Ice | last post by:
I am creating an initial index.htm page on which I show some disclaimers and introduction information. In the head section I have Javascript which determines the screen resolution of the client....
5
by: Protoman | last post by:
How do I call assembly code from C++ code and vice versa? Thanks for the help!!!
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
0
by: Aziz Paracha | last post by:
Hi: I am very new to .NET web services. I have a situation here; I want a web service that will communicate with plain ASP page. It will call ASP page with some query string variables, the ASP...
9
by: misdst23 | last post by:
Hi, I know I can call a static java method within javascript by using the <% ... %tags. But how can pass a javascript variable ? function thefunction() { var = javascriptvariable ;
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried the code and it seems that it works that way. The...
1
by: Maric Michaud | last post by:
Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit : This is not valid xml, there is no commas in attribute list in xml. You could try with minidom if your xml stream isn't too...
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
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: 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:
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.