在两个HTML和PHP之间交换变量

时间:2014-09-24 14:38:00

标签: javascript php html mysql session-variables

我正在尝试为mikrotik热点做单独表格,这个设备不允许在其中使用PHP,我需要将变量从设备传送到外部php页面,这将节省客户数据和在Hotspot中启用试用...

在此设备中,它托管了一个包含以下代码的HTML页面

<html>
<head><title>...</title></head>
<body>
$(if chap-id)
<noscript>
<center><b>JavaScript required. Enable JavaScript to continue.</b></center>
</noscript>
$(endif)
<center>If you are not redirected in a few seconds, click 'continue' below<br>
<form name="redirect" action="http://10.2.3.100/cadastro.html?mac=$(mac)" method="post">
<input type="hidden" name="mac" value="$(mac)">
<input type="hidden" name="ip" value="$(ip)">
<input type="hidden" name="username" value="$(username)">
<input type="hidden" name="link-login" value="$(link-login)">
<input type="hidden" name="link-orig" value="$(link-orig)">
<input type="hidden" name="error" value="$(error)">
<input type="hidden" name="chap-id" value="$(chap-id)">
<input type="hidden" name="chap-challenge" value="$(chap-challenge)">
<input type="hidden" name="link-login-only" value="$(link-login-only)">
<input type="hidden" name="link-orig-esc" value="$(link-orig-esc)">
<input type="hidden" name="mac-esc" value="$(mac-esc)">
<input type="submit" value="continue">
</form>
<script language="JavaScript">
<!--
    document.redirect.submit();
//-->
</script></center>
</body>
</html>

我已经学会了在google中搜索我可以将变量发送到另一个页面,方法是将其插入到URL的末尾,这样就可以了,我将发送开始试用所需的变量$ MAC。

接收它的页面是一个用HTML写的表单页面,我无法在这个页面中收到变量,我需要接收这个变量发送到另一个页面,在PHP中最后一个将在MySQL中保存表单然后发送给Mikrotik与客户MAC的链接,以允许它在互联网上浏览。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CADASTRO DE CLIENTES COM BANCO DE DADOS E PHP</title>
<style type="text/css">


</head>

<body>
<p id="demo"></p>
<script type"text/javascript">
var qs = new QueryString();
var v1 = qs.get("mac");
document.getElementById("demo").innerHTML = v1;
</script>


<form id="cadastro" name="cadastro" method="post" action="cadastro.php" onsubmit="return validaCampo(); return false;">
  <table width="625" border="0">
    <tr>
      <td width="69">Nome:</td>
      <td width="546"><input name="nome" type="text" id="nome" value="$(mac)" size="70" maxlength="60" />
        <span class="style1">*</span></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><input name="email" type="text" id="email" size="70" maxlength="60" />
      <span class="style1">*</span></td>
    </tr>

    <tr>
      <td colspan="2"><p>
        <input name="cadastrar" type="submit" id="cadastrar" value="Concluir meu Cadastro!" /> 
        <br />
          <input name="limpar" type="reset" id="limpar" value="Limpar Campos preenchidos!" />
          <br />
          <span class="style1">* Campos com * s&atilde;o obrigat&oacute;rios!          </span></p>
      <p>&nbsp; </p></td>
    </tr>
  </table>
</form>
</body>
</html>

现在是PHP页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php 
// RECEBENDO OS DADOS DO PC DO USUARIO
   $mac=$_POST['mac'];

// RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO !
$nome   = $_POST ["nome"];  //atribuição do campo "nome" vindo do formulário para variavel  
$email  = $_POST ["email"]; //atribuição do campo "email" vindo do formulário para variavel

//Gravando no banco de dados !

//conectando com o localhost - mysql
$conexao = mysql_connect("localhost","root","senha");
if (!$conexao)
    die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
//conectando com a tabela do banco de dados
$banco = mysql_select_db("cadhotspot",$conexao);
if (!$banco)
    die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());



$query = "INSERT INTO `clientes` ( `nome` , `email`  ) 
VALUES ('$nome', '$email', '$sexo' '')";

mysql_query($query,$conexao);

echo "Seu cadastro foi realizado com sucesso!<br>Agradecemos a atenção.";
?>
Mac: <?php echo $mac ?><br>

<meta http-equiv="refresh" content="60; url=<?php echo $linkloginonly; ?>?dst=<?php echo $linkorigesc; ?>&username=T-<?php echo $mac; ?>">


</body>
</html>

这个想法是,从客户设备接收MAC从MikroTik到其中托管的HTML页面,然后加载带有表单的HTML页面到客户单独,当客户点击发送它将写入mysql并发回下面链接到Mikrotik

 <meta http-equiv="refresh" content="60; url=http://10.5.50.1?&username=T-<?php echo $mac; ?>">

我的整个问题是在HTML和HTML之间以及PHP页面之后交换变量。

1 个答案:

答案 0 :(得分:0)

要将mac的值转发到php页面,请尝试在表单中添加以下JS代码。这将创建一个隐藏字段,当提交时将mac值发布到php页面。

<script type="text/javascript">
var qs = new QueryString();
var v1 = qs.get("mac");
document.write('<input type="hidden" name="mac" value="' + v1  + '">')
</script>

html页面中缺少一些结尾标记。