Jump to content

Transfer JS data to PHP


Recommended Posts

I have used the following JavaScript to create and HTML span of text and then populate the area:

document.write("<span name='msg' id='msg'></span>"); 

document.getElementById("msg").textContent = "This is a test";

And when I add this code in JavaScript, I can see validation of the element's functionality.

var A = document.getElementById("msg").textContent;  


Resulting in the phrase "This is a test" being displayed.  However, I cannot get the phrase to connect into PHP.

I've tried incorporating  $msg=$_POST['msg'];  and    $A=$_POST['A'];

echo "This is the message from ".$A;


But there doesn't seem to be a connection occurring.  How can I make the jump so that the PHP acknowledges the phrase inside the HTML span tag?

Link to comment
Share on other sites

That was my initial intention (i like knowing I am on the right path, as I'm sure YOU do too, my friend) but after discovering that I can gain 'access' to the phrase in a <span> through JS, i became compelled with finding the method of 'transforming' and using IT as the value; thereby saving a few lines of code [at the expense of several hours of wrestling and research].

Aside from abandoning my quest, is there a way to convert this element so it can be interpreted as a value on the PHP side?

PS: This is all occurring inside of a form.

Edited by phppup
Link to comment
Share on other sites

I'm not sure what you mean by "re-shaped", but yes technically you could grab the contents of any structural element via JavaScript and then send that content as data to PHP via AJAX. However, you're not saving yourself any "lines of code". In fact, you're adding complexity to what should be a fairly simple system, given what you've described. Perhaps you could explain what exactly you're trying to do and why you're determined to do it this way?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.