/    Sign up×
Community /Pin to ProfileBookmark

Send server side string to client side

I want to use a string created on the server (using classic Javascript) inside Javascript located on the client side.

I have learned that the following line of code works for Integers:
var Integer = <%=serverInt%>;

But this same approach fails for anything else (i.e., strings, array of integers).

Also, it would be nice to know how to modify the contents of an HTML “textarea” via server code.

Thanks, ROK

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumAug 24.2015 — 
  • 1. Regarding string: When you echo a string in PHP, the result has no quotes. You have to add them in Javascript.

  • 2. Regarding array: The best way to transfer a PHP array to javascript is to use JSON.

    Take a look at this demo:

    test1.php:
    [CODE]<!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/jquery.fancybox.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="js/jquery.fancybox.js"></script>
    <?php include 'test2.php'; ?>
    <script>
    var testint = <?php echo $testint; ?>;
    var teststr = "<?php echo $teststr; ?>";
    var testarr = <?php echo json_encode($testarr); ?>;
    console.log(testint);
    console.log(teststr);
    console.log(testarr);
    </script>
    </head>
    <body>
    </body>
    </html>[/CODE]

    test2.php:
    [code=php]<?php
    $testint = 124;
    $teststr = "My Teststring";
    $testarr = array(1,2,4,8);
    ?>[/code]
  • Copy linkTweet thisAlerts:
    @ROKauthorAug 26.2015 — Sempervivum,

    I had to get a bit inventive to get this message to you.


    Apparently, my WindowsXP OS combined with my IE-8 browser just don't hack

    it on this web site. I sent a message to the web administrator commenting

    that my web session gets stuck in an endless loop which just blinks "Done"

    over and over again in the status bar.

    So, I am forced to use an Apple laptop to access the site

    and send this message you.


    ------------------------------------------

    (original intended reply to you)

    You are indeed a scholar and a gentleman.


    It is difficult to find someone who is willing to donate

    time to assist a hapless individual like myself.


    Your instructions on how to retrieve strings worked like a charm.


    I had to revert to the <% … %> notation, but the concept was right on.


    The code for retrieving integer arrays did not fare so well.

    I attribute this to either/both of two causes:

    1) The first line of my HTML page is:

    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>

    2) My computer is old, much like myself, > 10 years with WindowsXP as an OS,

    and therefore, I cannot upgrade to a more modern version of IE-8.

    The server side PHP code probably worked fine because the server itself

    must certainly be more modern and versatile. However, none of the PHP lines

    within the client side <script> was recognizable. Since all of the code to

    handle integer arrays is masked by the "virtual" includes of "src=…",

    I have no idea how I might ease it back into my Javascript world.


    But this ability is not essential to the functioning of my site.


    I can work around this issue.

    Regarding "textarea" modifications on the client page via server code

    strikes me a rather basic desire. I am still interested in how to do that.

    Thanks again, ROK
    Copy linkTweet thisAlerts:
    @SempervivumAug 26.2015 — Hm, I'm a bit confused about this:<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>[/QUOTE]and this:I had to revert to the <% … %> notation[/QUOTE]Never seen such notation before. I googled for <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%> and found this:

    https://forums.adobe.com/thread/42104

    Do you use Dreamweaver? Unfortunately I don't have any knowledge about this software. Maybe it will help you to follow the instructions given in the link.
    Copy linkTweet thisAlerts:
    @SempervivumAug 26.2015 — Regarding "textarea" modifications on the client page via server code

    strikes me a rather basic desire. I am still interested in how to do that.[/QUOTE]
    This should be possible simply by entering the string into the textarea tag:[code=html]<textarea name="textarea" rows="10" cols="50"><?php echo $teststr; ?></textarea>[/code]
    Copy linkTweet thisAlerts:
    @ROKauthorAug 26.2015 — Yes, I use Dreamweaver, and it chooses the language definition.

    regarding the "<%" notation, it is just a matter of replacing the PHP "?" with "%", and things work exactly as you specified.

    I chose Javascript language because of its remarkable similarity to C++, with which I am rather familiar. If I told you how I manipulate JS to pass it thru the C++ compiler looking for coding errors, you'd die laughing.

    I'll check out the link.

    I've tried something akin to your syntax for "text area", but I'll look back to see if it was EXACTLY as you suggest.

    Thanks again --- ROK
    ×

    Success!

    Help @ROK spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...