我的索引有问题.php。我有一个将在 compute.php 上提交的表单,其中计算在 index.php 中的 iframe 内回显会话变量的值。这是我的代码:
索引.php
<script type="text/javascript">
function reply_click(){
document.getElementById('iframe').src=('compute.php');
}
</script>
<? session_start(); ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
onSubmit="reply_click(); return false">
<table cellspacing=0 cellpadding=10 width=320 border="0">
<tr><td>
<button type='submit' id='1' name='form_submit'>Compute</button>
</td></tr>
</table>
</form>
<?php
if(isset($_POST['form_submit']))
{
session_start();
$_SESSION['val']="8";
}
?>
<iframe id="iframe"></iframe>
计算.php
<?php
session_start();
$val=$_SESSION['val'];
echo($val);
?>
正如您在我的代码中看到的那样,每当提交表单时,会话变量都会初始化为 8。iframe src 将是 compute.php,据说显示 8。但问题是iframe什么都不显示。我的提交事件有问题吗?任何帮助将不胜感激。
首先,您不需要isset($_POST['form_submit'])
之后的第二个session_start()
。
其次,您需要将第一个session_start()
一直移动到顶部,然后再像javascript一样输出到浏览器。
喜欢:
<? session_start(); ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table cellspacing=0 cellpadding=10 width=320 border="0">
<tr><td>
<button type='submit' id='1' name='form_submit'>Compute</button>
</td></tr>
</table>
</form>
<?php
if(isset($_POST['form_submit'])) {
$_SESSION['val']="8";
}
?>
<iframe id="iframe"></iframe>
<?php
if(isset($_POST['form_submit'])) {
// drop out of php and echo the javascript
?>
<script type="text/javascript">
document.getElementById('iframe').src=('compute.php');
</script>
<?php
}
?>
表单
的onSubmit函数中的"return false"意味着您的表单永远不会被提交,因此您的会话值永远不会被设置。
你的问题是:
if(isset($_POST['form_submit']))
永远不会是真的。当您单击该按钮时,您将 iframe 设置为正确的 URL,但您说return false;
以防止页面重新加载。但这也阻止了任何帖子请求的发生。所以 $_POST 永远不会设置。