On click run PHP code and paste result in text box?

半腔热情 提交于 2019-12-08 15:07:09

问题


I have some php code that generates a random password. Next to a text box I have some text which says "click to Generate a random password".

What I am looking to achieve is when the text in double quotes above is clicked that the PHP code is run and the generated random password is then pasted into the text box which is beside the text above in double quotes.

How could do this? I am thinking maybe jQuery but not sure how I would do what I want above.


回答1:


Here you are... complete and working example :)
Put files 'index.html' and 'passgenerator.php' into same directory.

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#generate').click(function(){
                    $.get('passgenerator.php', function(data) {
                        $('[name=password]').val(data);
                    });
                })
            });
        </script>
    </head>
    <body>
        <form action="">
            <input type="text" name="password">
            <input type="button" id="generate" value="Generate password">
        </form>
    </body>
</html>

passgenerator.php

<?php
echo sha1(uniqid());
?>



回答2:


either use ajax to call the script page that generates the password or like col. shrapnel says port the generating function to javascript (which is a better idea)

Example (with jquery using ajax)

$.get("/PasswordGenerator.php",function(password)
{
    $("#TextboxID").val( password );
});

where TextboxID is the id of the textbox u want the password to be added to.




回答3:


Don't do it in php. Just do it in javascript. Here is a very neat tutorial that should show you the step by step instructions: http://www.mediacollege.com/internet/javascript/number/random.html




回答4:


Patrick Evans is right. You can setup your form like this :

<input type="button" value="Generate" onclick="generate();">

And in section you have to define a function

<script type="text/javascript">
    function generate(){
$.get("/PasswordGenerator.php",function(password)
{
    $("#TextboxID").val( password );
});
}
</script>



回答5:


Try this ajax call to get the content using ajax and display it on the div.

$("#generate").click(function(){
 $.get('url:to:urPHP',{

                    data:urData
                  },function(data){
                  $("#generatedPassword").html(data);
                  });

});
<input type="" id="generate" value="click to Generate a random password" />
<div id="generatedPassword">

<div>


来源:https://stackoverflow.com/questions/4639563/on-click-run-php-code-and-paste-result-in-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!