如何将html文本框中的值分配给变量并显示在同一页面中

时间:2018-02-04 16:16:54

标签: javascript php html

我尝试从文本框中获取值到变量并在同一页面中显示该值?

目标是用户将BTC钱包插入文本框,我需要将文本框中的值分配到同一页面中的变量,然后回显该值。

我试过这段代码,但是我收到了一个错误 这是我的错误

  

Oficheironãofoencontrado   Pode ter sido movido ou eliminado。   ERR_FILE_NOT_FOUND

这是我的代码

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<meta name="Author" content="Helder Ventura">
<title>Roll the dice</title>
<body bgcolor="orange">
    <h1 align="center">Welcome To Dice Game</h1>
    <center>
        <form action= $Wallet method="post">
        <input type="text" size="15" name="var1" />
        <input type="submit" value="assign" />
</form>
         </center>
<?php
$var =$_POST["var1"];
// now you can do wtf you want with the variable.
echo $var;
?>

<style type="text/css">

body{
background-color:white;
font-size:10pt;
font-family:sans-serif;
color:#00436e;
}
</style>

<script language="javascript">

var d1, d2;
var ctr=0;

var images=new Array();
for(i=0;i<=6;i++){
images[i]=new Image(); images[i].src='dice'+i+'.gif';
}

function rolldice(){
document.f.t.value='';
document.f.b.value='     Rolling.....    ';
if(ctr<6){
showdice();
ctr++;
setTimeout('rolldice()',150);
}else{
ctr=0;
showdice();
document.f.t.value=d1+d2;
document.f.b.value='Roll the dice...';
}}

function showdice(){
d1=Math.floor(Math.random()*6+1);
d2=Math.floor(Math.random()*6+1);
document.images['d1'].src='dice'+d1+'.gif';
document.images['d2'].src='dice'+d2+'.gif';
}

</script>

</head>
<body>
<center>

<form name="f">
<table>
<tr height="60">
<td align="center">
<img src="dice1.gif" name="d1">
</td>
<td width="50">&nbsp;</td>
<td align="center">
<img src="dice1.gif" name="d2">
</td>
</tr>
</table>
<br><br>
<b>Roll Total</b> <input type="text" size="2" value="" name="t" readonly 
onClick="document.f.b.focus()" onSelect="document.f.b.focus()">
<br><br>
<input type="button" name="b" value="Roll the dice..." onClick="rolldice()">
</form>

</center>

<br><br><hr><br><br>

<h2>Instructions:</h2>

<form name="SAf">

<br><br><br>
 1: Copy and paste the following script in the HEAD section of your page:
<br>
<a href="javascript:document.forms['SAf'].scripttxt.focus();document.forms['SAf'].scripttxt.select();">Select all...</a>
 <br><br>
 <textarea cols="80" rows="8" wrap="off" name="scripttxt">
 &lt;script language=&quot;javascript&quot;&gt;

 var d1, d2;
 var ctr=0;

var images=new Array();
for(i=0;i<=6;i++){
images[i]=new Image(); images[i].src='dice'+i+'.gif';
}

function rolldice(){
document.f.t.value='';
document.f.b.value='     Rolling.....    ';
if(ctr<6){
showdice();
ctr++;
setTimeout('rolldice()',150);
}else{
ctr=0;
showdice();
document.f.t.value=d1+d2;
document.f.b.value='Roll the dice...';
}}

function showdice(){
d1=Math.floor(Math.random()*6+1);
d2=Math.floor(Math.random()*6+1);
document.images['d1'].src='dice'+d1+'.gif';
document.images['d2'].src='dice'+d2+'.gif';
}
&lt;/script&gt;
</textarea>

<br><br><br>
2: Copy and paste the following HTML in the BODY section of your page:
<br>
<a href="javascript:document.forms['SAf'].bodytxt.focus();document.forms['SAf'].bodytxt.select();">Select all...</a>
<br><br>
<textarea cols="80" rows="8"  wrap="off" name="bodytxt">
<form name="f">
<table>
<tr height="60">
<td align="center">
<img src="dice1.gif" name="d1">
</td>
<td width="50">&nbsp;</td>
<td align="center">
<img src="dice1.gif" name="d2">
</td>
</tr>
</table>
<br><br>
<b>Roll Total</b> <input type="text" size="2" value="" name="t" readonly 
onClick="document.f.b.focus()" onSelect="document.f.b.focus()">
<br><br>
<input type="button" name="b" value="Roll the dice..." onClick="rolldice()">
</form>
</textarea>


</form>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

在阅读了有关所有类型表单的更多内容后,我认为它有效。 实际上它很容易,但我只是以错误的方式开始。 现在它的工作完美 这是工作代码

<h1 align="center">Welcome To Dice Game</h1>
<center><input type="text" id="wallet" />
<input type="button" onclick="getwords()" value="Enter" />
<div id="output">
</div></center>
<script language="javascript">
function getwords(){
myOutput=document.getElementById('output');
textbox = document.getElementById('wallet');
if (textbox.value != "")

myOutput.innerHTML="You entered: " + textbox.value;
else
alert("No wallet has been entered!")
}
</script>