0

I have a member profile page which displays an username in regular text,(h1). I can capture the username using Javascript using this code

<script type="text/javascript">
var userID = document.getElementsByClassName("display-name")[0].innerHTML;
</script>

Q: What I want to know is, is there a way to get that variable and sent it back to my database? And add the username if it's an unique username.

P.S.
- I'm running a Wordpress site, database is (phpMyAdmin).
- I've read the $_POST methods for javascript/php transfers. But I don't see how I can apply it here, because there is no form to submit anything in.
- The username gets generated by itself (using a plugin) that's why I'm doing it this way.

Francisco
  • 10,918
  • 6
  • 34
  • 45
AndyW
  • 1
  • 1
  • You can send a post request without a form, look into Ajax – JimL Jan 02 '16 at 21:01
  • Where did the username come from in the first place, if not the database? – rnevius Jan 02 '16 at 21:33
  • @mevius Those usernames can be found in the database, but It is generated by a plugin, then it gets put in the original userbase file. However I want to integrate Mixpanel with wordpress, especially mixpanel.alias/identify. And the plugin doesn't allow me to catch the variable I need so I have to do it this way. – AndyW Jan 02 '16 at 22:03

1 Answers1

0

You probably want to use an AJAX request and send it back to your server, without having to have a usual form/submit. Check this jQuery Ajax POST example with PHP

Community
  • 1
  • 1
Darko Maksimovic
  • 1,155
  • 12
  • 14
  • If you don't want to use Jquery you can find some helpful code samples here on how to achieve it http://youmightnotneedjquery.com/ – JimL Jan 02 '16 at 21:02
  • @Darko Maksimovic, If I'm not mistaken, And somehow I think I'm surely am, it also uses forms/submit to get the data? The first answer of your link uses a function as soon the submit button is clicked. The second one comes closer I think, but I'm not sure how to set the "type" of the printed username to post.. – AndyW Jan 02 '16 at 23:04
  • No it doesn't use form. It's simply ajax - connects to a server script and sends it whatever crap it wants. You may call it form-submit conceptually if you want, but it's not the usual html form. – Darko Maksimovic Jan 03 '16 at 00:11