phppup Posted November 16, 2018 Share Posted November 16, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/ Share on other sites More sharing options...
Barand Posted November 16, 2018 Share Posted November 16, 2018 (edited) 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 November 16, 2018 by Barand typo Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562183 Share on other sites More sharing options...
phppup Posted November 16, 2018 Author Share Posted November 16, 2018 (edited) 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 November 16, 2018 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562184 Share on other sites More sharing options...
requinix Posted November 16, 2018 Share Posted November 16, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562185 Share on other sites More sharing options...
phppup Posted November 16, 2018 Author Share Posted November 16, 2018 I understand that a span is not a form element, but isn't the JS creating a variable that can be 're-shaped'? Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562189 Share on other sites More sharing options...
requinix Posted November 16, 2018 Share Posted November 16, 2018 Not in any way you'll understand. You made a <span>. If you want something else then make something else. Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562193 Share on other sites More sharing options...
maxxd Posted November 17, 2018 Share Posted November 17, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307905-transfer-js-data-to-php/#findComment-1562211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.