Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7805

Re: passing values into javascript

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: passing values into javascript
Date 2012-12-15 13:10 +0000
Organization A noiseless patient Spider
Message-ID <kahssi$3u7$1@dont-email.me> (permalink)
References <14k13t3ghfii5.rtu07anokn7j$.dlg@40tude.net> <kagomk$o0$1@dont-email.me> <1c509cnlwc8ky.138l6xvqihkle$.dlg@40tude.net>

Show all headers | View raw


On Fri, 14 Dec 2012 22:09:12 -0500, richard wrote:

> As PHP creates the html page, it would be, or should be, rather simple
> to produce a line of code for that line of JS.
> Such as Echo "<script>code</script>"

Yep.

Now all you have to do is work out how to get the values you want where 
you have "code".

Simples. Google "javascript variable assignment"

It's probably something like:

echo "<script type='text/javascript'>var jsvarname = {$phpvarname};</
script>\n";

If $phpvarname is a string, you'll need single or escaped double quotes 
round it, either of:

echo "<script type='text/javascript'>var jsvarname = '{$phpvarname}';</
script>\n";

echo "<script type='text/javascript'>var jsvarname = \"{$phpvarname}\";</
script>\n";

If You want to pass the value of a javascript variable from one page to a 
script on another page, then on the first page you need to put the 
variable value into a suitable post or get request (which is a javascript 
issue, not a php issue, so don't ask how to do it here), and then in the 
php you need to extract the value from the request, validate it, and then 
use it for $phpvariable.

How you get it into the post / get request using javascript is a 
javascript topic, not a php one. You might want to google something like 
"dynamically generating get request urls with javascript".

On the php side, you can check that a string in the $_POST or $_GET array 
matches the expected format using a regex:

http://php.net/manual/en/function.preg-match.php

You can convert a string in the $_POST or $_GET array to an int or float 
value using appropriate functions:

http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.floatval.php

So, for example, as an academic exercise, to read an integer limited to a 
range of 25 .. 75 and with a default value 50 from a get request and send 
it back as a javascript value, you could code something like this in the 
php:

$myval1 = 50; // default value
if ( isset( $_GET['myval1'] ) && preg_match( '/^[2-7][0-9]$/', $_GET
['myval1'] ) == 1 ) {
$myval1 = intval( $_GET['myval1'] );
if ( $myval1 < 25 ) $myval1 = 25; // lower limit value
if ( $myval1 > 75 ) $myval1 = 75; // upper limit value
}

echo "<script type='text/javascript'>\nvar myval1 = {$myval1};\n</script>
\n";

Obviously you also need to make sure that the echo appears at an 
appropriate point in generating the output, I normally try and put such 
things in the document head before I declare any javascript functions.

Rgds

Denis McMahon

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

passing values into javascript richard <noreply@example.com> - 2012-12-14 20:03 -0500
  Re: passing values into javascript Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-14 21:52 -0500
    Re: passing values into javascript richard <noreply@example.com> - 2012-12-14 22:09 -0500
      Re: passing values into javascript Denis McMahon <denismfmcmahon@gmail.com> - 2012-12-15 13:10 +0000
        Re: passing values into javascript Tim Streater <timstreater@greenbee.net> - 2012-12-15 13:22 +0000
        Re: passing values into javascript Denis McMahon <denismfmcmahon@gmail.com> - 2012-12-15 21:36 +0000
    Re: passing values into javascript Allodoxaphobia <knock_yourself_out@example.net> - 2012-12-15 16:40 +0000
      Re: passing values into javascript Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-15 19:22 -0500
    Re: passing values into javascript richard <noreply@example.com> - 2012-12-15 23:39 -0500
      Re: passing values into javascript "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2012-12-16 06:21 +0000
        Re: passing values into javascript Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-16 09:12 -0500
          Re: passing values into javascript richard <noreply@example.com> - 2012-12-16 13:50 -0500
      Re: passing values into javascript "P E Schoen" <paul@peschoen.com> - 2012-12-19 18:30 -0500

csiph-web