Jump to content

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?

Link to comment
https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/
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

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.