FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » passing values into javascript
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
passing values into javascript [message #179870] Fri, 14 December 2012 20:03 Go to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
<script type="text/javascript">AudioPlayer.embed("audioplayer_1",
{soundFile: "http://mroldies.net/mp3/[value1]-[value2].mp3"})</script>

Given this line of script which is from the wordpress standalone
audioplayer, how could I pass a value from page one to page two so that the
two values in brackets are replaced with the proper information?


http://mroldies.net/1960/all60.html

What I am looking at doing is, linking each song name to one php page.

So that I only have to have one line of code per artist.
Rather than having a single script per song.

I don't care to read your flames on the page showing an under construction
page for most of the links. I have it that way for a reason. Or any of your
other trite little flames that you do just to harass me with.
Re: passing values into javascript [message #179871 is a reply to message #179870] Fri, 14 December 2012 21:52 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/14/2012 8:03 PM, richard wrote:
> <script type="text/javascript">AudioPlayer.embed("audioplayer_1",
> {soundFile: "http://mroldies.net/mp3/[value1]-[value2].mp3"})</script>
>
> Given this line of script which is from the wordpress standalone
> audioplayer, how could I pass a value from page one to page two so that the
> two values in brackets are replaced with the proper information?
>
>
> http://mroldies.net/1960/all60.html
>
> What I am looking at doing is, linking each song name to one php page.
>
> So that I only have to have one line of code per artist.
> Rather than having a single script per song.
>
> I don't care to read your flames on the page showing an under construction
> page for most of the links. I have it that way for a reason. Or any of your
> other trite little flames that you do just to harass me with.
>

PHP runs on the server, and doesn't know anything about javascript
(which runs on the client). The only way PHP can "pass values" to
javascript is to generate the data in the data which is sent to the client.

But since you're asking about how to do it in wordpress, I would highly
recommend you ask din a wordpress support group. They know their code
better than anyone else, and will know better than anyone else what you
have to do to get it to work in their environment.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: passing values into javascript [message #179872 is a reply to message #179871] Fri, 14 December 2012 22:09 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Fri, 14 Dec 2012 21:52:58 -0500, Jerry Stuckle wrote:

> On 12/14/2012 8:03 PM, richard wrote:
>> <script type="text/javascript">AudioPlayer.embed("audioplayer_1",
>> {soundFile: "http://mroldies.net/mp3/[value1]-[value2].mp3"})</script>
>>
>> Given this line of script which is from the wordpress standalone
>> audioplayer, how could I pass a value from page one to page two so that the
>> two values in brackets are replaced with the proper information?
>>
>>
>> http://mroldies.net/1960/all60.html
>>
>> What I am looking at doing is, linking each song name to one php page.
>>
>> So that I only have to have one line of code per artist.
>> Rather than having a single script per song.
>>
>> I don't care to read your flames on the page showing an under construction
>> page for most of the links. I have it that way for a reason. Or any of your
>> other trite little flames that you do just to harass me with.
>>
>
> PHP runs on the server, and doesn't know anything about javascript
> (which runs on the client). The only way PHP can "pass values" to
> javascript is to generate the data in the data which is sent to the client.
>
> But since you're asking about how to do it in wordpress, I would highly
> recommend you ask din a wordpress support group. They know their code
> better than anyone else, and will know better than anyone else what you
> have to do to get it to work in their environment.

Wordpress created the script to run outside of it's own format.
Had you bothered to look at the page given, you would see the script in
action. I do not have the fullblown Wordpress stuff on my site.

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>"
Re: passing values into javascript [message #179873 is a reply to message #179872] Sat, 15 December 2012 08:10 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Re: passing values into javascript [message #179874 is a reply to message #179873] Sat, 15 December 2012 08:22 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
In article <kahssi$3u7$1(at)dont-email(dot)me>,
Denis McMahon <denismfmcmahon(at)gmail(dot)com> wrote:

> 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";

Personally I try to keep the PHP part as small as possible, so inside
the top section of my JS where I'm initialising some variables, it's
likely to be something like:


var myjsVar = <?php echo $myphpvar; ?> ;


As the man says: simples!

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: passing values into javascript [message #179875 is a reply to message #179871] Sat, 15 December 2012 11:40 Go to previous messageGo to next message
Allodoxaphobia is currently offline  Allodoxaphobia
Messages: 21
Registered: September 2010
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On Fri, 14 Dec 2012 21:52:58 -0500, Jerry Stuckle wrote:
>
> PHP runs on the server, and doesn't know anything about javascript
> (which runs on the client).

(which may or *MAY NOT* be running on the client)
Re: passing values into javascript [message #179876 is a reply to message #179873] Sat, 15 December 2012 16:36 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Sat, 15 Dec 2012 13:10:42 +0000, Denis McMahon wrote:

> $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 }

Typical ballsed up wrapping ...

$myval1 = 50; // default value

// check if a get value was received and that it has the expected
// text format

if ( isset( $_GET['myval1'] ) && preg_match( '/^[2-7][0-9]$/', $_GET
['myval1'] ) == 1 ) {

$myval1 = intval( $_GET[ 'myval1' ] ); // get the int value

if ( $myval1 < 25 ) $myval1 = 25; // lower limit value

if ( $myval1 > 75 ) $myval1 = 75; // upper limit value

}

Rgds

Denis McMahon
Re: passing values into javascript [message #179877 is a reply to message #179875] Sat, 15 December 2012 19:22 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/15/2012 11:40 AM, Allodoxaphobia wrote:
> On Fri, 14 Dec 2012 21:52:58 -0500, Jerry Stuckle wrote:
>>
>> PHP runs on the server, and doesn't know anything about javascript
>> (which runs on the client).
>
> (which may or *MAY NOT* be running on the client)
>

Whether or not it IS running on the client is immaterial. The value is
still passed via HTML.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: passing values into javascript [message #179878 is a reply to message #179871] Sat, 15 December 2012 23:39 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Fri, 14 Dec 2012 21:52:58 -0500, Jerry Stuckle wrote:

> On 12/14/2012 8:03 PM, richard wrote:
>> <script type="text/javascript">AudioPlayer.embed("audioplayer_1",
>> {soundFile: "http://mroldies.net/mp3/[value1]-[value2].mp3"})</script>
>>
>> Given this line of script which is from the wordpress standalone
>> audioplayer, how could I pass a value from page one to page two so that the
>> two values in brackets are replaced with the proper information?
>>
>>
>> http://mroldies.net/1960/all60.html
>>
>> What I am looking at doing is, linking each song name to one php page.
>>
>> So that I only have to have one line of code per artist.
>> Rather than having a single script per song.
>>
>> I don't care to read your flames on the page showing an under construction
>> page for most of the links. I have it that way for a reason. Or any of your
>> other trite little flames that you do just to harass me with.
>>
>
> PHP runs on the server, and doesn't know anything about javascript
> (which runs on the client). The only way PHP can "pass values" to
> javascript is to generate the data in the data which is sent to the client.
>
> But since you're asking about how to do it in wordpress, I would highly
> recommend you ask din a wordpress support group. They know their code
> better than anyone else, and will know better than anyone else what you
> have to do to get it to work in their environment.

FYI, I found the answer at codingforums.
I got it without any BS.
Re: passing values into javascript [message #179879 is a reply to message #179878] Sun, 16 December 2012 01:21 Go to previous messageGo to next message
Beauregard T. Shagnas is currently offline  Beauregard T. Shagnas
Messages: 154
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
richard the sto0pid wrote:

>> On 12/14/2012 8:03 PM, richard the sto0pid wrote:
>>> I don't care to read your flames on the page showing an under
>>> construction page for most of the links. I have it that way for a
>>> reason. Or any of your other trite little flames that you do just to
>>> harass me with.
> [...]
> FYI, I found the answer at codingforums.
> I got it without any BS.

When you asked at that forum, did you include a condescending paragraph
such as you did here? And usually do when you ask questions?

--
-bts
-This space for rent, but the price is high
Re: passing values into javascript [message #179881 is a reply to message #179879] Sun, 16 December 2012 09:12 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 12/16/2012 1:21 AM, Beauregard T. Shagnasty wrote:
> richard the sto0pid wrote:
>
>>> On 12/14/2012 8:03 PM, richard the sto0pid wrote:
>>>> I don't care to read your flames on the page showing an under
>>>> construction page for most of the links. I have it that way for a
>>>> reason. Or any of your other trite little flames that you do just to
>>>> harass me with.
>> [...]
>> FYI, I found the answer at codingforums.
>> I got it without any BS.
>
> When you asked at that forum, did you include a condescending paragraph
> such as you did here? And usually do when you ask questions?
>

Beauregard, you don't understand. Richard NEVER does anything wrong -
the whole of Usenet is against him!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: passing values into javascript [message #179884 is a reply to message #179881] Sun, 16 December 2012 13:50 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Sun, 16 Dec 2012 09:12:31 -0500, Jerry Stuckle wrote:

> On 12/16/2012 1:21 AM, Beauregard T. Shagnasty wrote:
>> richard the sto0pid wrote:
>>
>>>> On 12/14/2012 8:03 PM, richard the sto0pid wrote:
>>>> > I don't care to read your flames on the page showing an under
>>>> > construction page for most of the links. I have it that way for a
>>>> > reason. Or any of your other trite little flames that you do just to
>>>> > harass me with.
>>> [...]
>>> FYI, I found the answer at codingforums.
>>> I got it without any BS.
>>
>> When you asked at that forum, did you include a condescending paragraph
>> such as you did here? And usually do when you ask questions?
>>
>
> Beauregard, you don't understand. Richard NEVER does anything wrong -
> the whole of Usenet is against him!

nah. just 98%.
Re: passing values into javascript [message #179933 is a reply to message #179878] Wed, 19 December 2012 18:30 Go to previous message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
add to buddy list
ignore all messages by this user
"richard" wrote in message
news:2v0zx3v0t5i5(dot)ivrfh58emm9w$(dot)dlg(at)40tude(dot)net...

> FYI, I found the answer at codingforums.
> I got it without any BS.

So, what is the answer you found? I have a similar need, and although I
think other replies covered it well enough, I'd appreciate your sharing the
information, especially if it is any easier.

Thanks,

Paul
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: HP syntax error, unexpected T_variable
Next Topic: Using echo to run separate PHP script from HTML
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Oct 18 09:12:39 EDT 2017

Total time taken to generate the page: 0.00867 seconds