Jump to content
phppup

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;  

document.write(A);

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?

Share this post


Link to post
Share on other sites

Javascript is on the client, php is on the server.

To communicate between the two you need to

  • put the value in a form field (input, hidden or textarea) and submit the form, or
  • use AJAX and send the value to the server
Edited by Barand
typo

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

If you want to send something as part of a form submission then it needs to be a form element.

A span is not a form element.

Share this post


Link to post
Share on other sites

I understand that a span is not a form element, but isn't the JS creating a variable that can be 're-shaped'? 

Share this post


Link to post
Share on other sites

Not in any way you'll understand.

You made a <span>. If you want something else then make something else.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.