-1

我创建了一个名为 classperson.php 的文件,在其中声明了所有变量。

<?php
class person1
{
    var $location;
    var $effective;
    var $training;
    var $treated;
    var $effort;
    var $information;
    var $recognition;
    var $valued;
    var $supervisor;
    var $informed;
    var $job;
    var $treats;
    var $satisfy;
    var $workgroup;
    var $utc;
    var $skills;

    function set_location($new_location)
    {
        $this->location=$new_location;
    }
    function get_location()
    {
        return $this->location;
    }


    function set_effective($new_effective)
    {
        $this->effective=$new_effective;
    }
    function get_effective()
    {
        return $this->effective;
    }



    function set_training($new_training)
    {
            $this->training=$new_training;
    }
    function get_training()
    {
            return $this->training;
    }



    function set_treated($new_treated)
    {
        $this->treated=$new_treated;
    }
    function get_treated()
    {
        return $this->treated;
    }




    function set_effort($new_effort)
    {
        $this->effort=$new_effort;
    }
    function get_effort()
    {
        return $this->effort;
    }



    function set_information($new_information)
    {
        $this->information=$new_information;
    }
    function get_information()
    {
            return $this->information;
    }



    function set_recognition($new_recognition)
    {
        $this->recognition=$new_recognition;
    }
    function get_recognition()
    {
        return $this->recognition;
    }



    function set_valued($new_valued)
    {
        $this->valued=$new_valued;
    }
    function get_valued()
    {
            return $this->valued;
    }



    function set_supervisor($new_supervisor)
    {
        $this->supervisor=$new_supervisor;
    }
    function get_supervisor()
    {
        return $this->supervisor;
    }



    function set_informed($new_informed)
    {
        $this->informed=$new_informed;
    }
    function get_informed()
    {
        return $this->informed;
    }



    function set_job($new_job)
    {
        $this->job=$new_job;
    }
    function get_job()
    {
            return $this->job;
    }



    function set_treats($new_treats)
    {
        $this->treats=$new_treats;
    }
    function get_treats()
    {
        return $this->treats;
    }



    function set_satisfy($new_satisfy)
    {
        $this->satisfy=$new_satisfy;
    }
    function get_satisfy()
    {
            return $this->satisfy;
    }



    function set_workgroup($new_workgroup)
    {
        $this->workgroup=$new_workgroup;
    }
    function get_workgroup()
    {
            return $this->workgroup;
    }



    function set_utc($new_utc)
    {
        $this->utc=$new_utc;
    }
    function get_utc()
    {
        return $this->utc;
    }



    function set_skills($new_skills)
    {
        $this->skills=$new_skills;
    }
    function get_skills()
    {
        return $this->skills;
    }
}
?>

后来我在 oopossurvey.php 中包含了这个文件。这是一个有 5 页的表格

看看代码

<?php
session_start();
$session=session_id();
$_SESSION['session']=$session;
?>
<html>
<body>
<form action="oopssurvey.php" method="post">
Where do you work?<br/>
<Input type ="radio" name="location" value="USA" <?php if($_SESSION['location'] == 'USA') echo "checked='checked'" ?>/>In the United states<br/>
<Input type ="radio" name="location" value="India" <?php if($_SESSION['location'] == 'India') echo "checked='checked'" ?>/>Outside of the United states<br/>

<input name="Operation1" type="submit" value="saveandresume" />
<input name="Operation2" type="submit" value="next" />

</form>
</body>
</html>

<?php
session_start();

if($_SESSION['session']==$session)
{
$location=$_SESSION['location'];
}
else
{
$_SESSION['location'] = $_POST['location'];
}
?>

<html>

<body>

<form action="oopssurvey.php" method="post">

In my work group, we are actively involved in making our work processes more effective and efficient (simpler, faster, etc.) using ACE.<br/>
<Input type ="radio" name="effective" value="Agree"/>Agree<br/>
<Input type ="radio" name="effective" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="effective" value="disagree"/>Disagree<br/>

I have received the training I need to understand and implement ACE in my work group.<br/>
<Input type ="radio" name="training" value="Agree"/>Agree<br/>
<Input type ="radio" name="training" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="training" value="disagree"/>Disagree<br/>

All employees at the company are treated fairly regardless of differences.<br/>
<Input type ="radio" name="treated" value="Agree"/>Agree<br/>
<Input type ="radio" name="treated" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="treated" value="disagree"/>Disagree<br/>

Sufficient effort is made to get the opinions and thoughts of the people who work here.<br/>
<Input type ="radio" name="effort" value="Agree"/>Agree<br/>
<Input type ="radio" name="effort" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="effort" value="disagree"/>Disagree<br/>

<input name="Operation3" type="submit" value="saveandresume" />
<input name="Operation4" type="submit" value="next" />
<input name="Operation5" type="submit" value="back" />

</form>

</body>

</html>


 <?php
session_start();

if($_SESSION['session']==$session)
{

    $effective=$_SESSION['effective'];

    $training=$_SESSION['training'];

    $treated=$_SESSION['treated'];

    $effort=$_SESSION['effort'];
}
else
{
    $_SESSION['effective']=$_POST['effective'];
    $_SESSION['training']=$_POST['training'];
    $_SESSION['treated']=$_POST['treated'];
    $_SESSION['effort']=$_POST['effort'];
}

?>

<html>
<body>
<form action="oopssurvey.php" method="post">

How satisfied are you with the information you receive from management on what's going on in the company?<br/>
<Input type ="radio" name="information" value="agree"/>Agree<br/>
<Input type ="radio" name="information" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="information" value="disagree"/>Disagree<br/>

How satisfied are you with the recognition you receive for doing a good job?<br/>
<Input type ="radio" name="recognition" value="agree"/>Agree<br/>
<Input type ="radio" name="recognition" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="recognition" value="disagree"/>Disagree<br/>

I feel valued as an employee of the company.<br/>
<Input type ="radio" name="valued" value="agree"/>Agree<br/>
<Input type ="radio" name="valued" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="valued" value="disagree"/>Disagree<br/>

My immediate supervisor does a good job of coaching/guiding me.<br/>
<Input type ="radio" name="supervisor" value="agree"/>Agree<br/>
<Input type ="radio" name="supervisor" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="supervisor" value="disagree"/>Disagree<br/>

<input name="Operation6" type="submit" value="saveandresume" />
<input name="Operation7" type="submit" value="next" />
<input name="Operation8" type="submit" value="back" />

</form>
</body>
</html>

<?php
session_start();


if($_SESSION['session']==$session)
{

$information=$_SESSION['information'];

$recognition=$_SESSION['recognition'];

$valued=$_SESSION['valued'];

$supervisor=$_SESSION['supervisor'];
}
else
{
    $_SESSION['information']=$_POST['information'];
    $_SESSION['recognition']=$_POST['recognition'];
    $_SESSION['valued']=$_POST['valued'];
    $_SESSION['supervisor']=$_POST['supervisor'];
}
?>
<html>

<body>

<form action="oopssurvey.php" method="post">

I feel well informed about what is expected in my job.<br/>
<Input type ="radio" name="informed" value="agree"/>Agree<br/>
<Input type ="radio" name="informed" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="informed" value="disagree"/>Disagree<br/>

Overall, how good a job do you feel is being done by your immediate supervisor<br/>
<Input type ="radio" name="job" value="agree"/>Agree<br/>
<Input type ="radio" name="job" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="job" value="disagree"/>Disagree<br/>

My immediate supervisor treats me with respect and dignity.<br/>
<Input type ="radio" name="treats" value="agree"/>Agree<br/>
<Input type ="radio" name="treats" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="treats" value="disagree"/>Disagree<br/>

How satisfied are you with your involvement in the decisions that affect your work?<br/>
<Input type ="radio" name="satisfy" value="agree"/>Agree<br/>
<Input type ="radio" name="satisfy" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="satisfy" value="disagree"/>Disagree<br/>

<input name="Operation9" type="submit" value="saveandresume" />
<input name="Operation10" type="submit" value="next" />
<input name="Operation11" type="submit" value="back" />

</form>

</body>

</html>

<?php

session_start();

if($_SESSION['session']==$session)
{

$informed=$_SESSION['informed'];

$job=$_SESSION['job'];

$treats=$_SESSION['treats'];

$satisfy=$_SESSION['satisfy'];
}
else
{
    $_SESSION['informed']=$_POST['informed'];
    $_SESSION['job']=$_POST['job'];
    $_SESSION['treats']=$_POST['treats'];
    $_SESSION['satisfy']=$_POST['satisfy'];
}
?>

<html>

<body>

<form action="oopssurvey.php" method="post">

Day-to-day decisions and activities in my work group demonstrate that quality is a top priority.
<Input type ="radio" name="workgroup" value="agree"/>Agree<br/>
<Input type ="radio" name="workgroup" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="workgroup" value="disagree"/>Disagree<br/>

I have the opportunity to grow and develop at my company and/or elsewhere in UTC.
<Input type ="radio" name="utc" value="agree"/>Agree<br/>
<Input type ="radio" name="utc" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="utc" value="disagree"/>Disagree<br/>

My job makes good use of my skills and abilities.
<Input type ="radio" name="skills" value="agree"/>Agree<br/>
<Input type ="radio" name="skills" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="skills" value="disagree"/>Disagree<br/>

<input name="Operation12" type="submit" value="submit" />
<input name="Operation13" type="submit" value="back" />



</form>

</body>

</html>

<?php
session_start();
include 'classperson.php';

if($_POST['Operation1'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage2.php";
}

else if($_POST['Operation2'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
include "oopspage2.php";
}


elseif($_POST['Operation3'])

{
$_SESSION['effective'] = $effective;
$_SESSION['effective'] = $_POST['effective'];
$_SESSION['training'] = $training;
$_SESSION['training'] = $_POST['training'];
$_SESSION['treated'] = $treated;
$_SESSION['treated'] = $_POST['treated'];
$_SESSION['effort'] = $effort;
$_SESSION['effort'] = $_POST['effort'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage3.php";

}

elseif($_POST['Operation4'])
{
$_SESSION['effective'] = $effective;
$_SESSION['effective'] = $_POST['effective'];
$_SESSION['training'] = $training;
$_SESSION['training'] = $_POST['training'];
$_SESSION['treated'] = $treated;
$_SESSION['treated'] = $_POST['treated'];
$_SESSION['effort'] = $effort;
$_SESSION['effort'] = $_POST['effort'];
include "oopspage3.php";
}

elseif($_POST['Operation5'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
include "oopspage1.php";
}

elseif($_POST['Operation6'])
{
$_SESSION['information'] = $information;
$_SESSION['information'] = $_POST['information'];
$_SESSION['recognition'] = $recognition;
$_SESSION['recognition'] = $_POST['recognition'];
$_SESSION['valued'] = $valued;
$_SESSION['valued'] = $_POST['valued'];
$_SESSION['supervisor'] = $supervisor;
$_SESSION['supervisor'] = $_POST['supervisor'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage4.php";

}

elseif($_POST['Operation7'] )
{
$_SESSION['information'] = $information;
$_SESSION['information'] = $_POST['information'];
$_SESSION['recognition'] = $recognition;
$_SESSION['recognition'] = $_POST['recognition'];
$_SESSION['valued'] = $valued;
$_SESSION['valued'] = $_POST['valued'];
$_SESSION['supervisor'] = $supervisor;
$_SESSION['supervisor'] = $_POST['supervisor'];
include "oopspage4.php";
}

elseif($_POST['Operation8'])
{
include "oopspage2.php";
}

elseif($_POST['Operation9'])
{
$_SESSION['informed'] = $informed;
$_SESSION['informed'] = $_POST['informed'];
$_SESSION['job'] = $job;
$_SESSION['job'] = $_POST['job'];
$_SESSION['treats'] = $treats;
$_SESSION['treats'] = $_POST['treats'];
$_SESSION['satisfy'] = $satisfy;
$_SESSION['satisfy'] = $_POST['satisfy'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage5.php";

}

elseif($_POST['Operation10'])
{
$_SESSION['informed'] = $informed;
$_SESSION['informed'] = $_POST['informed'];
$_SESSION['job'] = $job;
$_SESSION['job'] = $_POST['job'];
$_SESSION['treats'] = $treats;
$_SESSION['treats'] = $_POST['treats'];
$_SESSION['satisfy'] = $satisfy;
$_SESSION['satisfy'] = $_POST['satisfy'];
include "oopspage5.php";
}
elseif($_POST['Operation11'])
{
include "oopspage3.php";
}

elseif($_POST['Operation12'])
{
$con = mysql_connect("localhost","cgiadmin","cgi");
if (!$con)
 {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("j2", $con);

$person = new person1;
$person->set_location($_POST['location']);
$person->set_effective($_POST['effective']);
$person->set_training($_POST['training']);
$person->set_treated($_POST['treated']);
$person->set_effort($_POST['effort']);
$person->set_information($_POST['information']);
$person->set_recognition($_POST['recognition']);
$person->set_valued($_POST['valued']);
$person->set_supervisor($_POST['supervisor']);
$person->set_informed($_POST['informed']);
$person->set_job($_POST['job']);
$person->set_treats($_POST['treats']);
$person->set_satisfy($_POST['satisfy']);
$person->set_workgroup($_POST['workgroup']);
$person->set_utc($_POST['utc']);
$person->set_skills($_POST['skills']);


$sql="INSERT INTO bigsurvey(location, effective, training , treated , effort, information , recognition,valued,supervisor,informed,job,treats,satisfy,workgroup,utc,skills)
VALUES
('$_SESSION[location]','$_SESSION[effective]','$_SESSION[training]','$_SESSION[treated]','$_SESSION[effort]','$_SESSION[information]','$_SESSION[recognition]','$_SESSION[valued]','$_SESSION[supervisor]','$_SESSION[informed]','$_SESSION[job]','$_SESSION[treats]','$_SESSION[satisfy]','".$person->get_workgroup()."','".$person->get_utc()."','".$person->get_skills()."')";
if (!mysql_query($sql,$con))
  {
    die('Error: ' . mysql_error());
  }
echo "Thanks for taking the survey .";
mysql_close($con);

}
elseif($_POST['Operation13'])
{
include "oopspage4.php";
}
?>

我的问题是,这是在 php 中使用面向对象概念创建或创建表单的正确方法吗?或者我应该创建更多对象或进行任何更改?

我试图在所有页面上包含文件 classperson.php,但是当我使用下一个按钮时出现错误。

提前致谢

4

1 回答 1

3

首先,您所说的“页面”仅在客户端使用,但对于 PHP,它是单个脚本。因此,例如多次执行 session_start() 是没有意义的,因为每次向服务器发送数据时,脚本都会再次从头开始运行。当它到达当前页面的末尾时,它也不会停止运行。

其次,这种通过会话变量确定调查步骤的想法本身有点生疏,并且没有正确实施。我很确定它不起作用(尽管我没有运行代码)。在会话中存储变量并没有什么不好,但我的建议是你应该以某种方式重新考虑逻辑。例如,您可以在每个表单中使用一些隐藏的输入来检查用户是哪个步骤的调查,并且在 PHP 文件中有一些switch{}()用于每个页面的案例。

第三,不要这么混淆,你应该把逻辑和表现分开。我的意思是 - 对所有表单使用单独的文件,并使用单独的文件接受来自用户的数据,将其写下来(如您的示例 - 在会话中),然后选择接下来要显示的页面。这样,您将更容易定位错误,并且它会更多地在您的脑海中安定下来。

至于你关于 PHP 中使用的类的问题:通常你应该只包含一次类,不需要重新包含它,例如在这个文件中你将处理所有逻辑。然后,您可以创建该类的实例并使用它。无论如何,我有一种感觉,你有些着急,而且在我看来,你现在开始使用 OOP 技术还为时过早——最好是一步一步来。尝试让这个脚本在没有类的情况下工作。当它可以工作时,将逻辑提取到一个类中。这样你会更好地理解一切:)

于 2012-06-08T13:44:43.033 回答