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.

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.