我试着在同一页上创建一些随机的单词生成器,生成动词,名词等(最终创建一个情节生成器,但用户可以选择随机生成的建议)
我已经设法创建了它,但是,当我单击同一页上的另一个生成器时,我希望生成的随机单词留在框中(例如,如果我单击建议一个名词,然后生成一个单词,然后我单击建议一个形容词,一个形容词生成,但该名词从上一个框中消失)
<!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>发布于 2017-04-08 00:41:42
您可以尝试使用ajax,而不是在一个页面中编写所有的php和html代码,这会使它变得笨拙。
设置一个对php脚本的ajax调用,该脚本将返回一个随机名称,然后将该名称分配给输入框。例如:
给你的输入和按钮分配一个id;
<input id='name' name='name' />
<button id="submit">Generate random name</button>在ajax脚本中,调用php脚本
$( "#submit" ).bind( "click", function() {
$.ajax({
type: 'POST',
url: "quote.php",
success: function(result){
//assign result to input
$('#name').val(result);
}
})
})在php脚本中,以任何方式生成名称并通过echo返回结果。
<?php
//generate random names and other stuff
echo $random;你可以以任何你想要的方式修改它。
如果有帮助或发现问题,请告诉我。
https://stackoverflow.com/questions/43282857
复制相似问题