I'm trying to se up unique user names without registration, this could end up with having same user names and I can't say "see all posts by this user" as users could have the same name.
Basically I am using this wordpress plugin called User Submitted Posts which is great as user can post an article without registration, but there is the duplicate name issue, so I thought a solution could be that we have an input field:
<input name="user-submitted-name" type="text" value="" data-required="true" required placeholder="Your name" class="form-control input-lg usp-input">
And we add a random unique string to the output in order to have:
User 1: simon_xyz
User 2: simon_abc
I guess it's quite simple to generate a random string in php, founds lots of stacks that help me on that such as this question: PHP random string generator
But ideally i want to tell the user something like "This is you username, copy it and always use this". There won't be an email requirement as there isn't a registration, so how to add a random unique string to an input field value and show the result live?
I am using this jQuery to add the value of a checkbox to the input field for something else on the site:
$('.checkTag').click(function() {
$("#user-submitted-tags").val(
$('.checkTag:checked').map(function() {
return $(this).val();
}).get().join(', ')
);
});
Perhaps something like that could be used but i'm struggling as the random string isn't something you would select.
The live result would be something like:
<h2>This is your username</h2>
<h3>Use this user name the next time you write your post</h3>
<p>Your usernanme is: simon_xyz</p>
As far as I understand it now, based on your comments, I'd propose something like this:
Page with the username input box (omitting <head>
, <html>
and <body>
)(based on the JS-snippet in the question I assume you can use jQuery):
$uniqueRandomString = ...;
?>
<form id="username-form"> (or get, that doesn't really matter, just remember to adjust it on the processing page.)
<input type="text" name="username" id="username-input" /> <? echo "+ " . $uniqueRandomString; ?>
<input type="hidden" name="unique-random-string" value="<? echo $uniqueRandomString; ?>"/>
<input type="submit" />
</form>
<script>
$("#username-form").on('submit', function() { // Of course you could change the ID of the form, but you'll have to change this as well in that case.
$.get('url/of/processing/page.php?' + $(this).serialize(), function(data) {
if (data.indexOf("SUCCESS") == 0) {
username = data.substring(8);
$('body').append("<h2>This is your username</h2>
<h3>Use this user name the next time you write your post</h3>
<p>Your usernanme is: " + username + "</p>");
} else {
// Display an error message.
}
});
});
Page where you process the username:
<?
// Check if the unique random string - now contained is $_POST['unique-random-string'] - is valid!
// If not, make the expression in this if-statement evaluate to true.
if ($error) echo "ERROR";
else echo "SUCCESS:" . $_GET['username'];
?>