2013-04-05 58 views
0

所以这是一个基本的表单提交和处理,骨子里。有三个脚本,表单脚本,处理器脚本和视图页面。下面是摘录从每个:使用PHP处理表单数据

形式:

 <form action="processor.php" method="post"> 
     <h4>Question # 1</h4> 
     <p>What grade are you in?</p> 
     <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label> 
     <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label> 
     <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label> 
     <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label> 
     <div class="button"> 
     <button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button> 
     </div> 
     </form> 

的处理器脚本:

<?php 

header("Location: viewpage.html"); 

$grade = $_POST['grade']; 

function Grades() { 
if ($grade =="1") { 
    echo "You're a freshmen"; 
} elseif ($grade == "2") { 
    echo "You're a sophomore"; 
} elseif ($grade == "3") { 
    echo "You're a junior."; 
} elseif ($grade == "4") { 
    echo "You're a senior."; 
} else { 
    echo "Something is wrong."; 
} 

} 

?> 

查看页面:

 <h4>Question # 1</h4> 
     <p><?php Grades(); 
     ?> 
     </p> 

本质上讲,我试图让用户填写表格,处理它并吐出输入。例如,他们表示他们是新生,他们点击提交并被带到查看页面,在那里它吐出“你是一名新生。”这是一段时间,因为我编码,这是一个非常简单的问题,我似乎无法找到解决方案。任何帮助将不胜感激。谢谢。

因为,当我到达视图页面时,它什么也没有显示。

+1

问题是...?除了你的处理器基本上通过重定向到另一个页面来中止处理?还有一些可变的范围问题...还有一些不清楚的“这些脚本如何与对方相关”的问题...... – 2013-04-05 15:02:09

+0

我在原帖之后做了一个编辑。我创建了处理器页面,以便可以在后端进行处理,并且可以将PHP和HTML分开。有没有更高效的方法?我遇到的问题是,当它到达视图页面脚本时,函数什么都没有吐出。 – buttonitup 2013-04-05 15:07:09

+0

你能否澄清一下这些脚本如何相互关联?你的意思是我如何将每一个连接到另一个? – buttonitup 2013-04-05 15:08:20

回答

1

它应该是这个样子的是:

<?php 

function Grades() { 
$grade = $_POST['grade']; 
if ($grade =="1") { 
    echo "You're a freshmen"; 
} elseif ($grade == "2") { 
    echo "You're a sophomore"; 
} elseif ($grade == "3") { 
    echo "You're a junior."; 
} elseif ($grade == "4") { 
    echo "You're a senior."; 
} else { 
    echo "Something is wrong."; 
} 

} 

include('viewpage.php'); 

?> 

请注意,您应该重命名viewpage.html到viewpage.php

+0

谢谢你,做到了。只是有点生疏。谢谢您的帮助。 – buttonitup 2013-04-05 15:23:49

0

你可以使用一些函数来重定向不同的输入,如新鲜...为此,您必须创建单独的页面并使用重定向(“Pagename”); 我认为这可能是您的问题的解决方案

1

它看起来像我收到帖子,然后立即重定向到另一个页面,而不发送任何刚刚收到的变量。因此,您需要将grade字段作为GET参数发送。你需要在视图文件中定义你的方法/变量。

处理器:

<?php 

    // If you're going to redirect, you MUST send along the grade 
    header("Location: viewpage.html?grade=" urlencode($_POST['grade')); 

?> 

查看:

<?php 

    // In your view you only have access to variables in your immediate request. 
    $grade = $_GET['grade']; 

    function Grades ($grade) { 
    if ($grade =="1") { 
     echo "You're a freshmen"; 
    } elseif ($grade == "2") { 
     echo "You're a sophomore"; 
    } elseif ($grade == "3") { 
     echo "You're a junior."; 
    } elseif ($grade == "4") { 
     echo "You're a senior."; 
    } else { 
     echo "Something is wrong."; 
    } 
    } 
?> 

<h4>Question # 1</h4> 
<p><?php Grades($grade); ?></p> 

,除非你真的需要的处理器将数据与保存:

<form action="viewpage.html" method="get"> 
    <h4>Question # 1</h4> 
    <p>What grade are you in?</p> 
    <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label> 
    <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label> 
    <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label> 
    <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label> 
    <div class="button"> 
    <button class="btn btn-primary" type="submit" name="submit">Submit</button> 
    </div> 
    </form> 
+0

我试过这种方式,但在视图页面上仍然没有输出。你提到假设我包含了处理脚本中的视图。我认为这是问题所在?你能否详细说明一下? – buttonitup 2013-04-05 15:13:47

+0

@buttonitup哇,我甚至没有看到重定向。那是你的问题! – 2013-04-05 15:17:48

0

您需要将POST变量传递为参数给你的功能。

2

第一个问题是您在处理器脚本做的第一件事是发出标题重定向。之后脚本可能会生成输出,但客户端永远不会看到它。第二个是你的程序结构全部关闭。理想情况下,您希望使用会话来存储处理器和查看脚本之间的'等级'值,或者将它们合并为一个。

<?php 
$grade = $_POST['grade']; 

function Grades ($input) { 
if ($input =="1") { 
    echo "You're a freshmen"; 
} elseif ($input == "2") { 
    echo "You're a sophomore"; 
} elseif ($input == "3") { 
    echo "You're a junior."; 
} elseif ($input == "4") { 
    echo "You're a senior."; 
} else { 
    echo "Something is wrong."; 
} 

?><html> 
<head></head> 
<body> 
<h4>Question # 1</h4> 
<p><?php Grades($grade);?></p> 
</body> 
</html> 

,或者:

<?php 
session_start(); 
$_SESSION['grade'] = $_POST['grade']; 
header('Location: viewpage.php'); 

<?php 
session_start(); 
$grade = $_SESSION['grade']; 

function Grades ($input) { 
if ($input =="1") { 
    echo "You're a freshmen"; 
} elseif ($input == "2") { 
    echo "You're a sophomore"; 
} elseif ($input == "3") { 
    echo "You're a junior."; 
} elseif ($input == "4") { 
    echo "You're a senior."; 
} else { 
    echo "Something is wrong."; 
} 

?><html> 
<head></head> 
<body> 
<h4>Question # 1</h4> 
<p><?php Grades($grade);?></p> 
</body> 
</html> 

你还需要记住的是,除非你include()一个脚本到另一个每个脚本是完全无视其他人。他们不知道定义了什么变量或函数,唯一的例外是会话变量。

+0

OR:代码去哪里? – buttonitup 2013-04-05 15:19:13

+0

你说过这个代码,或者那个代码。 – buttonitup 2013-04-05 15:27:00

+0

@buttonitup第一个块将用一个脚本代替'processor'和'view'脚本。 'Or:'下方的另外两个块分别替换'processor'和'view'脚本。 – Sammitch 2013-04-05 15:28:34