1 回答
TA贡献2016条经验 获得超9个赞
您有一些需要解决的问题,但我认为主要问题是,当您重定向时,您的帖子不再设置,因此您只想直接发布到目标页面:
<form role="form" action="loadpage.php" method="POST" name="theForm" id="theForm">
然后在该页面上执行会话操作。另外,我会创建一个函数来获取您的请求,以便您可以进行一些过滤:
<?php
# You could include this from a separate page, makes your script cleaner
function getRequest($key = false, $type = 'post')
{
switch($type) {
case('get'):
$arr = $_GET;
break;
case('post'):
$arr = $_POST;
break;
default:
$arr = $_REQUEST;
}
if(!empty($key))
return (isset($arr[$key]))? trim($arr[$key]) : null;
return $arr;
}
# Don't put this after any "if" statements, just make it first thing on top of
# every top-level page
session_start();
# Just stop if invalid
if(empty(getRequest('selectedPage')))
die("Invalid request.");
# Assign the session variables
$_SESSION['sayi'] = htmlentities(getRequest('sayi'));
$_SESSION['madde'] = htmlentities(getRequest('madde'));
$_SESSION['tarih'] = htmlentities(getRequest('tarih'));
$_SESSION['selectedPage'] = htmlentities(getRequest('selectedPage'));
# Modify the switch a bit. What you have is not wrong though.
# Make sure to use exit after he redirect so the script doesn't continue to run
switch(getRequest('selectedPage')) {
case "page_0":
# Since everything else redirects, just die here
die("Konu Seçmediniz.");
case "page_1":
$page = "page2_form";
break;
case "page_2":
$page = "page_2";
break;
default:
$page = "page1_form";
break;
}
# Just do one redirect here
header("Location: {$page}.php");
# This may be the end of the script anyway, but best to get in the habit of
# exiting after redirect
exit;
如果您确实想先发布到同一页面然后重定向,则在您的 loadpage.php 上您需要使用会话值而不是 $_POST 进行重定向值:
<?php
switch($_SESSION['selectedPage']) {
case "page_0":
# Since everything else redirects, just die here
die("Konu Seçmediniz.");
case "page_1":
$pg = "page2_form";
break;
case "page_2":
$pg = "page_2";
break;
default:
$pg = "page1_form";
break;
}
# Just do one redirect here
header("Location: {$pg}.php");
exit;
# If this is the end of the script, don't close it with a "?>" it is proper syntax to leave it off
# and then you don't have to worry about any hidden spaces that may mess you
# up down the road.
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报