dtc99987 2013-09-15 15:37
浏览 31
已采纳

将随机唯一字符串添加到输入值,并实时显示结果?

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>
  • 写回答

1条回答 默认 最新

  • dongqia3502 2013-09-15 16:18
    关注

    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'];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献