473,406 Members | 2,707 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,406 software developers and data experts.

How to pass javascript variable to php on javascript generated page?

5
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. Onmouseover javascript read area names and send it to javascript generated page. On generated page, area name (from first page) need to be query variable (depending of them show data from database). If i put string instead of variable,everything works fine, result is ok. Only problem is (i think) get variable from html to php on same page.

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. new_window.document.write("<title>".concat(x,"</title>","<?PHP
  3.     $rst = mysql_connect("127.0.0.1","root","12345");
  4.     $a =$_SERVER['PHP_SELF'];
  5.     preg_match_all('/<title>(.*?)<\/title>/',$a, $match);
  6.     $script = $match[0];  
  7.     echo $script;
  8.     echo "<br />";
  9.  
  10.     if (!$rst){
  11.     echo( "<p>Unable to connect to database manager.</p>");
  12.         die('Could not connect: ' . mysql_error());
  13.     }
  14.     mysql_select_db("oprema", $rst);
  15.  
  16.     $result = mysql_query("SELECT * FROM oprema WHERE mreznomesto='$script'");
  17.  
  18.     while($row = mysql_fetch_array($result)) {
  19.         print( $row['mreznomesto'] . " " . $row['serijskibroj']);
  20.         echo "<br />";
  21.     }
  22. ?>"));
  23. </script>
  24.  
Feb 21 '11 #1
7 3182
johny10151981
1,059 1GB
Try print_r instead of echo then you will see the reason
Feb 21 '11 #2
Arsale
5
No, definitely is problem with area definition. When i try print_r instead of echo, got "Unterminated string konstant" error on first page, not open javascript generated page. Same thing is if i replace preg_match_all with preg_match and wrote $script = $match;
Thanks for advice.
Feb 21 '11 #3
dgreenhouse
250 Expert 100+
I'm unclear on why you're running a preg_match on <title> on the name of the currently executing script. The match array will always be empty as far as I can tell.

What are you actually trying to accomplish?
Feb 22 '11 #4
Arsale
5
Is not necessary to run a preg_mach on <title>, i only need to pass javascript variable from one page to php on another. Second page is popup, which is activated on mouseover imagemap area from first page. If you got same other solution any help is appreciated...
Feb 22 '11 #5
nathj
938 Expert 512MB
Hi,

Are you trying to do this with AJAX? If so you can pass a variable on the end of the URL you call with the HTTPRequest method. This will then be available in the $_GET array.

I have developed something similar previously using an image map and AJAX. the image map code looked like:
Expand|Select|Wrap|Line Numbers
  1. <area shape="poly" alt="Burkina Faso" coords="281,158,275,165,275,166,276,167,278,164,285,164" href="javascript:void(0);" title="Burkina Faso" onclick="mapNavigator(1);">
  2.  
The JS functions:
Expand|Select|Wrap|Line Numbers
  1. function GetXmlHttpObject()
  2.  
  3.  
  4.     if (window.XMLHttpRequest)
  5.  
  6.     {
  7.  
  8.         loXMLHTTP=new XMLHttpRequest() ;
  9.  
  10.     }
  11.  
  12.     else if (window.ActiveXObject)
  13.  
  14.     {
  15.  
  16.         loXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP") ;
  17.  
  18.     }     
  19.  
  20.     if (loXMLHTTP==null)
  21.  
  22.     {
  23.  
  24.         alert ("Browser does not support HTTP Request")    ;
  25.  
  26.          return    ;
  27.  
  28.     }
  29.  
  30.     return loXMLHTTP ;
  31.  
  32.  
  33.  
  34.  
  35. function submitXMLHTTP(poXMLHTTP, pcIDToWriteTo, pcOpenMethod, pcURL, plOpen, pcSendData)
  36.  
  37. {                                                                                        
  38.  
  39.     poXMLHTTP.onreadystatechange = function() 
  40.  
  41.     { 
  42.  
  43.         // use of ghost function enables the system to execute each item in the correct order 
  44.  
  45.         writeResponse(poXMLHTTP, pcIDToWriteTo); 
  46.  
  47.     };
  48.  
  49.     poXMLHTTP.open(pcOpenMethod,pcURL,plOpen) ;
  50.  
  51.     poXMLHTTP.send(pcSendData) ; 
  52.  
  53.  
  54.  
  55.  
  56. function writeResponse(poXMLHTTP, pcEmelentID) 
  57.  
  58. {        
  59.  
  60.     if (poXMLHTTP.readyState==4 || poXMLHTTP.readyState=="complete")
  61.  
  62.     { 
  63.  
  64.         if (poXMLHTTP.status == 200)
  65.  
  66.         {
  67.  
  68.             document.getElementById(pcEmelentID).innerHTML=poXMLHTTP.responseText ;  
  69.  
  70.         }
  71.  
  72.     } 
  73.  
  74.  
  75.  
This worked a treat for me.

I hope this helps you out.
nathj
Feb 22 '11 #6
Arsale
5
Thanks for advice, i'll try this evening and notify you what i've done.
Feb 22 '11 #7
Arsale
5
I solved my problem on very light way. In brackets on javascript function "open_new_window(0)" is index number of map area(very important), and first index is always 0.
Image code is something like:
Expand|Select|Wrap|Line Numbers
  1. <map id="imagename" name="imagename">
  2. <area shape="rect" alt="areaname" coords="345,17,391,37" nohref title="areaname" onMouseOver="open_new_window(0)" onMouseOut="close_window()"/>
Javascript is like:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function open_new_window(t) {
  3.     var y = document.getElementById("imagename");
  4.     var x=y.getElementsByTagName("area")[t].title;
  5. var url="http://site/phpfile.php?x="
  6. new_window = open(url+x,"","width=450,height=150,toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=no");
  7. new_window.document.close(t); }
  8. function close_window() {
  9. new_window.close();
  10. }
  11. </script>
PHP file code is simple:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.      $rst = mysql_connect("loaclhost","root","12345");
  3.        if (!$rst){
  4.      echo( "<p>Unable to connect to database manager.</p>");
  5.        die('Could not connect: ' . mysql_error());
  6.        }
  7.     mysql_select_db("databasename", $rst);
  8.    $title=$_GET[x];
  9.      $result = mysql_query("SELECT * FROM table WHERE `field name1`='".$title."'");
  10.     while($row = mysql_fetch_array($result))
  11.     {
  12.     print( $row['field name2'] . " ||  " . $row['field name3'] . " ||   " .$row['field name4']  . " ||  " .$row['field name5'] );
  13.     echo "<br />";
  14.     }
  15. ?>
Thanks for help, i hope to this is useful to someone except me.
Mar 2 '11 #8

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

Similar topics

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...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
4
by: fxs | last post by:
Hello, I've got the following elements on my page: A Body tag that looks like this: <body onload="setTabs()"> A Javascript script in the <head> that has: var foo; function setTabs() {...
2
by: sawtooths | last post by:
I have a page, where onload, I populate various boxes with totals via a javascript function. My page itself is in .asp. My question is, how do I take those javascript values that load initially and...
2
by: prabu507 | last post by:
hi, can anyone tell me how to pass a javascript value to an asp page without submitting the page ..... requirement: i can do it in ajax but i dont want to use that .... i want to try in...
2
by: Zuhaib Hyder | last post by:
please remove double quote error in accessing javascript variable in c# , Page.RegisterStartupScript("MyScript", "<script language=javascript> var thumb = null;thumb =...
3
by: simonakiki | last post by:
hi how can i pass a variable from a page to another in asp.net 10x
2
AnuSumesh
by: AnuSumesh | last post by:
Hi All, I am writing one aspx page in which i am calling abc.htm file using <iframe> tag. I want to pass one variable from aspx page that should be read from html page. I am new to .net. I...
1
by: DeepaMurugan | last post by:
Hi i have vb script function to calculate date and time of diff timezones. i have stored the output of this function to a variable i need to pass this variable to asp page. can anyone give...
2
Frinavale
by: Frinavale | last post by:
Hi! I am dynamically generating external .js JavaScript resources to be used with a Tab control I created (using VB.NET). Originally, the JavaScript was written directly into the <head> of the...
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: 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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.