Hello, I'm into PHP/Javascript and having some problem here, so just want to seek help from you guys.

I have created a page with a textarea. (named Step 1) when the user clicked the "Next" <a></a> link the textarea's value will be stored in a session variable. I've did it by creating a separate php file and link the href value of the <a></a>, here is the php file's src.

<?php
session_start();
$_SESSION['var_gift_message'] = $_POST['gift_message'];

/* Remember header() must be called before any actual output is sent */
/* Redirect browser */
header("Location: http://mywebsite.com/step-3/");

/* For good order don't risk the code below the redirect
to be executed when we redirect. */
exit;
?>

(Don't mind the website address in the code, I've changed it, it's not the real website address...)

After storing the value of the textarea, the php page will redirect the user into a page with other stuffs
(named Step 2)...with a "Next" <a></a> link also. When the user clicked it, a new page will load with a textarea (Step 3)...now, what i need is to get the value of the session variable and insert it in the Step 3's textarea

Here is what i've coded so far: (TextArea_setText.js) -> this script is linked in Step 3's page.

window.onload = function( ) {
    justpass( "<?php echo $_SESSION['var_gift_message'] ?>" );
}

function justpass(str) {
    document.getElementById("my_field_name").value = str;
}

This works fine, but the problem is, the whole "<?php echo $_SESSION['var_gift_message'] ?>" is getting printed on the textarea, not the actual value of $_SESSION['var_gift_message']. I've tried to use single quotes but google chrome's developer tool raises an Error: Uncaught SyntaxError: Unexpected identifier

Then when I removed both quotes it raises an Error: **Uncaught SyntaxError: Unexpected token < **

Is it wrong that i've wrote it in .js and not in .php? or is there any other way?

I don't know if this is the correct way of doing it, because i'm still just starting to delve in PHP and Javascript. So i really have no idea, I'm just searching in google for examples.

By the way, I'm using Wordpress, and the textarea is auto-generated by a wordpress plugin (named WooCommerce). so i'm just depending on the textarea's ID. and i just can't insert the js function call ( justpass( str ) ) in the <textarea> element, because i cannot find it in the plugin's php pages...I've tried to read their documentation, but it didn't helped a lot.

Thank you in advance.

Dani AI

Generated

Good catch sorting this out, and good suggestions from (AJAX) and (localize the data into JS). The core issue was that PHP runs on the server only inside files the server passes to the PHP engine — an external file served as plain .js will not have <?php ... ?> executed. That is why the literal PHP tags showed up in the textarea.

Two practical, safe options that work with WordPress + plugins that generate the textarea:

  1. Fill the textarea on the server when rendering the page. If you can edit the template that outputs the textarea, output the session value as the textarea content and escape it for HTML. Example pattern:

    <!-- inside the Step 3 template -->
    <textarea id="gift_message"><?php echo htmlspecialchars($_SESSION['var_gift_message'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>

    This is simplest and avoids any client-side injection risks.

  2. If you cannot change the plugin HTML (WooCommerce-generated field), pass the session value into JavaScript safely and let a small script set the field after load. Produce a tiny inline script from PHP (or use WP functions to add inline script) and JSON-encode the value so it is safely escaped for JS:

    <script>
    var sessionGift = <?php echo json_encode($_SESSION['var_gift_message'] ?? ''); ?>;
    // then your JS can set the plugin textarea by id or selector
    </script>

Security/resilience tips:

  • Always call session_start() before output when reading session data.
  • Escape for the correct context: use htmlspecialchars/esc_textarea for HTML and json_encode/esc_js for JS.
  • Avoid exposing sensitive data to the client; use AJAX with nonce checks if the data needs protection or validation.

These approaches avoid parsing PHP inside a raw .js file and let you safely populate the WooCommerce textarea created by the plugin.

Recommended Answers

All 3 Replies

in the file where you set $_SESSION['var']
make it do this
<?php echo $_SESSION['var']; ?>
then in the other file,

$.get( "session_page.php", function(data) { $("#my_field_id").html(data); } );

and it will probably do what you're trying to accomplish.
Though, I'm using jQuery.

commented: Thanks! +3

Looks a tad like you are tryin to mix PHP and JavaScript.

In Step 3's page (I assumem that is step3.php?) you need to do the following:

<?php
$handle = "my_script_handle"; // Identify the script to WP
$src = "path/to/javascript.js";
$deps = FALSE; //No dependancies
$ver = "script_version"; // or null to use WP's version
$in_footer = TRUE; // place script inn footer, FALSE places it in header
wp_register_script( $handle, $src, $deps, $ver, $in_footer );
wp_enqueue_script( $handle); // Includes script in step 3
wp_localize_script( $handle, 'my_data', array('message'=>$_SESSION['var_gift_message']);
?> 

In the js file:

document.getElementById("my_field_name").value = my_data.message;

Look up the functions above in the WordPress Codex for more details, or to correct my mistakes!

commented: Thanks! +3

Finally! Been looking for right solutions for this! A big thank you to the both of you.

Problem solved! Thanks again guys! Both of you are a big help! Keep it up! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.