Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 04-25-2005, 09:14 AM
jg_v7n's Avatar
Senior Member
Latest Blog:
None

 
Join Date: 08-26-04
Location: Rio de Janeiro
Posts: 892
iTrader: 0 / 0%
Javascript post variables...

I have a page with a form, when I post the form I want a javascript on the target page to read POST variables.

How do I refer to POST variables using javascript?

Thanks.

Last edited by jg_v7n; 04-25-2005 at 09:19 AM.
 
Reply With Quote
  #2 (permalink)  
Old 04-26-2005, 04:48 PM
jg_v7n's Avatar
Senior Member
Latest Blog:
None

 
Join Date: 08-26-04
Location: Rio de Janeiro
Posts: 892
iTrader: 0 / 0%
Apparently the answer to this is "you can't"
 
Reply With Quote
  #3 (permalink)  
Old 04-26-2005, 11:07 PM
insitedev's Avatar
Member
Latest Blog:
None

 
Join Date: 12-14-04
Posts: 105
iTrader: 0 / 0%
Not sure how to do it using Javascript. I checked w3schools and didn't see anything.

You can get the POST data using another language like PHP, then assign that to a javascript variable.
 
Reply With Quote
  #4 (permalink)  
Old 05-04-2005, 06:46 AM
Junior Member
 
Join Date: 05-04-05
Posts: 2
iTrader: 0 / 0%
hi,
you may try to do a little trick:
on the main page form submit you open up a temporary window where you can store in javascript the variables that you posted. Then once the main page is loaded with the target page, you grab into javascript the values from the temporary window and close it.
Not so elegant but should work.

Last edited by algis; 05-04-2005 at 06:51 AM.
 
Reply With Quote
  #5 (permalink)  
Old 07-20-2008, 12:13 AM
Junior Member
 
Join Date: 07-20-08
Posts: 1
iTrader: 0 / 0%
Using GET/POST variables in JavaScript

Quote:
Originally Posted by insitedev View Post
Not sure how to do it using Javascript. I checked w3schools and didn't see anything.

You can get the POST data using another language like PHP, then assign that to a javascript variable.
I recently wanted some of my JavaScript to grab the parameters being passed to my webpage, and I used PHP to pass this to JavaScript.

Now, in this example, my webpage is a .php page, but don't let this scare you. You can simply rename your .html page as .php, and put these lines at the start of it:

<?php
session_start();
?>


I want a JavaScript "openPage" variable to be set to that "q6" string:

[url removed]


My .php page contained this Javascript code:

<script language=JavaScript>
<!--
function OnLoad()
{
var openPage = "";
<?php
if ($_GET['id'] != "")
{
echo "var openPage = \"" . $_GET['id'] . "\"";
}
?>

if (openPage != "")
{
// At this point, you can do something with the "openPage"
// variable !!
showAnswerToThisFAQ(openPage);
}
}
-->


Works a treat, but, yes, it could be made a bit simpler, by getting
the PHP to just call my showAnswerToThisFAQ() function.

Hope this helps someone !


Mike

Last edited by Kream; 07-20-2008 at 09:00 PM.
 
Reply With Quote
  #6 (permalink)  
Old 12-21-2008, 10:16 PM
Junior Member
 
Join Date: 12-21-08
Posts: 2
iTrader: 0 / 0%
Assuming, you have access to PHP or a similar server-side language, you can do this using a hidden form and basic HTML DOM reference.

==PHP==

$transfer_form = "
<form name=transfer_form id=transfer_form>
<input type=hidden name=param1 value=$param1>
<input type=hidden name=param2 value=$param2>
</form>";
echo $transfer_form;

==JAVASCRIPT==

var param1 = document.getElementById('transfer_form').param1.va lue;
var param2 = document.getElementById('transfer_form').param2.va lue;


Simplified and out of context, but you get the idea. Set javascript portion to execute onPageLoad and you're laughing.
 
Reply With Quote
  #7 (permalink)  
Old 12-22-2008, 10:16 AM
Contributing Member
Latest Blog:
None

 
Join Date: 11-17-08
Location: NJ
Posts: 55
iTrader: 0 / 0%
What sps113 wrote is the way to do it.

I just want to add that either that form should have style="display:none"
or it should be wrapped in a div with that style.
__________________
Leonid
 
Reply With Quote
  #8 (permalink)  
Old 12-27-2008, 03:25 AM
Junior Member
 
Join Date: 12-27-08
Posts: 2
iTrader: 0 / 0%
JS not seeing the transfer form

Hi everyone - first post here and I hope someone can assist. I, too, am trying to communicate between PHP/JHTML and Javascript.

I have a PHP script that generates an HTML page for output. Within this page is an <iframe> that causes a further HTML page to be loaded. This is essentially a Google map. The reason for having the <iframe> is that Google mainly uses Javascript and I didn't want to mix technologies that much. Also, IE is a pain in that it wasn't displaying the map correctly without it being in an <iframe> whereas Chrome and Firefox were fine...

I have a lot of data that I need to pass to the map function and the quantity is unknown. Essentially, the user has requested to see information regarding a local governing body and all the clubs that belong to that body. My intention is to display a map of the area (centred on a lat/long read from a MySQL database along with an appropriate Zoom Level for the map - again from the database.

So, in addition to the info needed to create and centre the map, I also need to pass details of all the Clubs within the governing body. This info is Club Id, Club Name, Club level (playing level), Lat, Long (for position on map) and the finally a count of how many Clubs are to be processed.

The JS application then creates the <div> containers for the map and sidebar and displays the map. It also positions on the map a marker for each club (different colours - depending on playing level) and allows for an info window to be displayed when the link or the marker is clicked to show the Club Name. It also displays check boxes so that you can restrict the view to one of the four levels of Clubs (National, Regional, Local, Unknown).

I had it all working beautifully and to do this, I created a url for the <iframe> page with all the parameters needed and the "got" these from the URL. All worked perfectly in Chrome and Firefox and then I discover that IE has a url limit of 2083. Expletives deleted!

So, I searched around for hints and tips and came across this one above of using the transfer_form. The problem seems to be that in my JS the form appears to be undefined (according to Firebug). Now, I'm not a JS expert and was looking for some assistance - all help GREATLY appreciated.

I'm not sure if Im supposed to post code snippets - so apologies if not.

The relevant PHP is:

$transfer_form="
<form style='display: none;' name='transfer_form' id='transfer_form'><div style='display: none;'>";
while ($sqlr_cbc = mysql_fetch_array($sqlq_cbc))
{
$divid = $sqlr_cbc['division_id'];
$sqlq_dv = mysql_query("SELECT level FROM dir_division WHERE id = '$divid'");
$sqlr_dv = mysql_fetch_row($sqlq_dv);
$cl_idx++;
$cbnm=str_replace("&","and",$sqlr_cbc['name']);
$transfer_form .="<input type='hidden' name='lt".$cl_idx."' value='".$sqlr_cbc['google_lat']."'>\n";
$transfer_form .="<input type='hidden' name='ln".$cl_idx."' value='".$sqlr_cbc['google_lng']."'>\n";
$transfer_form .="<input type='hidden' name='nm".$cl_idx."' value='".$cbnm."'>\n";
$transfer_form .="<input type='hidden' name='lv".$cl_idx."' value='".$sqlr_dv[0]."'>\n";
$transfer_form .="<input type='hidden' name='cl".$cl_idx."' value='".$sqlr_cbc['id']."'>\n";
}
$transfer_form .="<input type='hidden' name='cnt' value='".$cl_idx."'></div></form>";
echo "<table align='left'><tr><td bgcolor='$bgdark' class='dark' align='center'><b>Map of ".$sqlr_gb[0]." Area</b></td></tr>
<tr><td>".$transfer_form."<iframe width='875' height='600' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'
src='$cbtext' name='cbmapframe'></iframe></td></tr></table>";

When reviewing the generated HTML - this all looks fine and as expected. Also, the few parameters that I am passing on the URL (related to the governing body are also received by the JS (I use a function to emulate the PHP $_GET facility).

The relevant portion of the JS is below:

var qs = new Querystring();
cblt = qs.get('cblt');
cbln = qs.get('cbln');
cbzm = qs.get('cbzm');
var skin = qs.get('skincol');
var cn = qs.get('cn');
var sport = qs.get('sport');

var cb = qs.get('cb');
var filtr = qs.get('filter');
initialize(cblt, cbln, cbzm);

The above uses the Querystring function to get the parameters from the URL and then calls the initialise function to display the map canvas centred on the lat/long and at the correct zoom - this all works as expected.

The next bit is where it doesn't work. I am attempting to get the form data in the JS for each of the hidden elements posted.

var cnt = document.getElementById('transfer_form').cnt.value ;
alert('Cnt = '+cnt);
for (var x = 1; x <= cnt; x++)
{
xlt = 'lt'+x; xln = 'ln'+x; xnm = 'nm'+x; xlv = 'lv'+x; xcl = 'cl'+x;
var lat =document.getElementById('transfer_form').xlt.valu e;
var lng =document.getElementById('transfer_form').xln.valu e;
var mktxt =document.getElementById('transfer_form').xnm.valu e;
var lvl =document.getElementById('transfer_form').xlv.valu e;
var cl =document.getElementById('transfer_form').xcl.valu e;
alert('lat = '+lat+' lng = '+lng+' mktxt = '+mktxt+' lvl= '+lvl+' cl = '+cl);
}

The above attempts to get the count (cnt) of clubs passed from the transfer form and at this point Firebug asserts that the form is undefined and so it never gets as far as the alert. So, in essence - the form is unknown to hte JS piece.

In addition to the above (sorry to be a complete PITA) is that the style='display: none;' does not appear to be completely unintrusive in that it shifts things along a bit - depending upon where you place the PHP echo statement. I did consider that possibly the placing of the "echo" may be causal in the JS believing the form to be undefined - i.e. not part of the DOM for the <iframe> but I'm not knowledgable enough around that area. I don't believe that I can have the from display actually within the <iframe> or can I? Maybe I'll quickly try that!

So, a long post and my apologies for that - but any help would be much appreciated. May I wish you all season's greetings as well and thank you in anticipation of your help.

Regards
 
Reply With Quote
  #9 (permalink)  
Old 12-30-2008, 07:25 AM
Junior Member
 
Join Date: 12-21-08
Posts: 2
iTrader: 0 / 0%
Quote:
Originally Posted by highlawn View Post
The above attempts to get the count (cnt) of clubs passed from the transfer form and at this point Firebug asserts that the form is undefined and so it never gets as far as the alert. So, in essence - the form is unknown to hte JS piece.
Sounds like your frames are causing problems. Remember that using frames generally affects a page's DOM tree. If the reference is inconsistent with the actual DOM structure, you would expect and want an error. Depending on where you place the transfer form, you'll need to change the DOM path that JS uses to retrieve form values. Do a google search to figure out the proper reference with your frame structure.

Quote:
Originally Posted by highlawn View Post
In addition to the above (sorry to be a complete PITA) is that the style='display: none;' does not appear to be completely unintrusive in that it shifts things along a bit - depending upon where you place the PHP echo statement.
As a side note regarding a previous post, I have trouble imagining a case where using style="display: none" is really necessary. The form elements are already hidden. For most applications, just placing the form, without style alterations, at the bottom of the body is fine.
 
Reply With Quote
  #10 (permalink)  
Old 12-30-2008, 07:56 AM
Contributing Member
 
Join Date: 10-05-08
Posts: 56
iTrader: 0 / 0%
jg, don't know what method you finally decided to use, but in case you are still deciding on it you could use php to get the post variables put them in a cookie, echo the html page with the javascript, this is then open by the end user and javascript is run. Javascript takes all the needed data from the cookie.

Hope this is an answer for your problem
__________________
Get up to 999 one-way links to your site by using the automatic directory submitter. It's lighting fast and all is done 100% automatically for you.
For voice changing software click here.
 
Reply With Quote
  #11 (permalink)  
Old 12-30-2008, 08:01 AM
Junior Member
 
Join Date: 12-27-08
Posts: 2
iTrader: 0 / 0%
Thanks for the pointers. I decided that I needed a fresh look at it all and so I changed the showcbmap.html into a showcbmap.php and used PHP to read the variables from the database and then used PHP to echo the javascript code and to substitute the php variable at the correct time.

All works well now. Happy to show the basic code if anyone interested.

Many thanks to all who replied or spent time thinking about it. It is appreciated.
 
Reply With Quote
  #12 (permalink)  
Old 01-27-2010, 11:58 AM
Junior Member
 
Join Date: 01-27-10
Posts: 1
iTrader: 0 / 0%
Quote:
Originally Posted by jg_v7n View Post
I have a page with a form, when I post the form I want a javascript on the target page to read POST variables.

How do I refer to POST variables using javascript?

Thanks.
location.search gives you the parameters supplied in the url, see:

http://fr.selfhtml.org/javascript/ob...ion.htm#search
Carl
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Variables in PHP for GMap... CheebaHawk Coding Forum 5 01-25-2007 07:52 PM
transfering the variables from one window to another with javascript wulf2001 Coding Forum 0 07-17-2006 05:38 AM
Variables being lost in CGI scripts GeXus Coding Forum 0 12-01-2004 01:47 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 02:45 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2014 Escalate Media




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.