使用PHP将值从一个html页面传递到另一个html页面

时间:2012-09-01 11:16:21

标签: php javascript html

  

可能重复:
  Get variable from PHP to JavaScript

我知道它非常简单,但由于某些原因我的代码不起作用,我不知道错误在哪里。

在第一页中,我有一个表单字段。

第1页:

 <form action="fare.html" method="post">
    <fieldset id="main" style="background-color:#CCC">    
    <table align="center">    
    <tr>
   <td colspan="5">
  <fieldset>
  <legend style="color:#F00"><b>Departure Address</b></legend> 
  <input name="address1" id="address1" type="text" size="15" value="City" /> 
  <input name="" type="submit" />
  </fieldset>
  </td>
  </tr>  
    </table>
   </fieldset>
   </form>

第2页:

<head>

<?PHP
$address1 = $_POST['address1'];
?>

<script type="text/javascript">
$(window).load(function(){
document.getElementById('from').value = $address1;
})  
</script>

</head>

<body>

  <div class="content">
    <h1>Instructions</h1>
    <table width="200">
  <tr>
    <td colspan="2">
      <input type="text" name="from" id="from" /></td>
  </tr> 
</table>
    <!-- end .content --></div>  
</body>

当我点击提交链接时,页面被重定向,但我看不到新页面的测试字段中的值。

谢谢!

编辑:我仍然无法成功。

我将代码更改为

第1页:

<form action="fare1.php" method="post">
    <fieldset id="main" style="background-color:#CCC">    
    <table align="center">    
    <tr>
  <td colspan="5">
  <fieldset>
  <legend style="color:#F00"><b>Departure Address</b></legend> 
  <input name="address1" id="address1" type="text" size="15" value="City" /> 
  <input name="" type="submit" />
  </fieldset>
  </td>
  </tr>  
    </table>
    </fieldset>
    </form>

第2页:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?PHP
$address1 = $_POST['address1'];
?>

<script type="text/javascript">
$(window).load(function(){
document.getElementById('from').value = "<?php echo $address?>";
})  
</script>
</head>

<body>
<div class="content">
    <h1>Instructions</h1>
    <table width="200">
  <tr>
    <td colspan="2">
      <input type="text" name="from" id="from" /></td>
  </tr> 
</table>
    <!-- end .content --></div>  
</body>

我需要将值从html页面传递到html页面,但我甚至尝试为第2页创建.php页面,但它仍然无法正常工作。

仍在寻找解决方案:(

谢谢!

4 个答案:

答案 0 :(得分:1)

document.getElementById('from').value = "<?php echo $address1; ?>";

此外,您必须将表单操作从fare.html更改为fare.php。因为你不能在html页面中使用php功能。

答案 1 :(得分:1)

我建议你写一下fare.html

<?php
   if(isset($_POST["address1"])
     {
         //echo $_POST["address1"];
            $address=$_POST["address1"];
      }
?>



document.getElementById('from').value = "<?php echo $address?>";

答案 2 :(得分:0)

<script type="text/javascript">
$(window).load(function(){
document.getElementById('from').value = '<?php echo $address1; ?>';
})  
</script>

你应该在javascript中回显php变量,因为这是两种不同的语言。

答案 3 :(得分:0)

你应该如何在html页面中获取php变量。您需要将表单操作更改为php页面

<form action="fare.php" method="post">
        <fieldset id="main" style="background-color:#CCC">    
        <table align="center">    
        <tr>
       <td colspan="5">
      <fieldset>
      <legend style="color:#F00"><b>Departure Address</b></legend> 
      <input name="address1" id="address1" type="text" size="15" value="City" /> 
      <input name="" type="submit" />
      </fieldset>
      </td>
      </tr>  
        </table>
       </fieldset>
       </form>

fare.php

<head>


<input type="hidden" id='addressval' value='<?=$_POST['address1']?>'>

<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
$(window).load(function(){
document.getElementById('from').value = document.getElementById('addressval').value;
})  
</script>

</head>

<body>

  <div class="content">
    <h1>Instructions</h1>
    <table width="200">
  <tr>
    <td colspan="2">
      <input type="text" name="from" id="from" /></td>
  </tr> 
</table>
    <!-- end .content --></div>  
</body>