简体   繁体   English

随机生成器词生成器-PHP

[英]Random Generator Word Generator - PHP

I'm trying to create a few random word generators on the same page which generates verbs, nouns etc. (creating eventually a plot generator but the user can select suggestions that randomly generate) 我试图在同一页面上创建一些随机的单词生成器,以生成动词,名词等。(最终创建了一个图生成器,但用户可以选择随机生成的建议)

I've managed to create it, however, I want the random word generated to stay in the box when I click another generator on the same page (for example if I click suggest a noun and a word is generated, then I go to click suggest a Adjective and an adjective generates, but the noun disappears from the previous box) 我已经设法创建了它,但是,当我单击同一页面上的另一个生成器时,我希望生成的随机词保留在框中(例如,如果我单击建议一个名词并且生成了一个词,那么我要单击建议一个形容词和一个形容词生成,但名词从上一个框消失)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>

<?php 

{
{

$Noun=array("just", "assume", "there", "are", "lots", "of", "nouns", "in", "here");

//echo rand(0, 2);
$myRandom = rand(0, 99);
//echo $Cars[$myRandom];
//if first name is blank/null then put 
}
?> 

<form action="quote.php">
    Noun<br>
    <input type="text" name="firstname" 
           value="<?php if (isset($_GET['Noun'])){echo $Noun[$myRandom];} }?>"><br>
    <input type="submit" name ="Noun" value="Suggest">
</form>

<?php 

{
{

$Adj=array("just", "assume", "there", "are", "lots", "of", "adjectives", "in", "here");

//echo rand(0, 2);
$Random = rand(0, 99);
//echo $Cars[$myRandom];
//if first name is blank/null then put 
}
?> 

<form action="quote.php">
    Noun<br>
    <input type="text" name="firstname" 
           value="<?php if (isset($_GET['Adjective'])){echo $Adj[$Random];} }?>"><br>
    <input type="submit" name ="Adjective" value="Suggest">
</form>

</body>
</html>

You can try to use ajax instead of writing all the php and html code in one page which makes it unwieldy. 您可以尝试使用Ajax而不是在一页中编写所有php和html代码,这会使它变得笨拙。

Set up an ajax call to a php script which will return a random name and then assign this name to the input box. 设置对php脚本的ajax调用,该脚本将返回一个随机名称,然后将此名称分配给输入框。 For example; 例如;

Assign an id to your input and button; 为您的输入和按钮分配一个ID;

<input id='name' name='name' />
<button id="submit">Generate random name</button>

In your ajax script, call the php script 在您的ajax脚本中,调用php脚本

$( "#submit" ).bind( "click", function() {
$.ajax({
         type: 'POST',
           url: "quote.php",
           success: function(result){
               //assign result to input
               $('#name').val(result);
           }
     })
  })

In your php script, generate the name in what ever way and send back the result by echo. 在您的php脚本中,以任何方式生成名称并通过echo发送回结果。

<?php

//generate random names and other stuff
echo $random;

You can modify it in any way you want. 您可以根据需要进行任何修改。

Let me know if it helps or if you find a problem. 让我知道是否有帮助,或者您发现问题了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM