我是一个完整的新手(在上周开始)
问题是这样的:
基本上,我试图确保一旦填写了子表单,就不会改变它。所以,我使用了!isset来显示子表单(即如果!isset为true),如果!isset为false,则隐藏该子表单并显示下一个子表单(个人表单仅设计)
<?php include($_SERVER['DOCUMENT_ROOT'].'/officespace/includes/functions.php');
echo'<html>
<head>
<title> Create </title>
</head>
<body>';
if(!isset($_POST["Category"])){
/* if no category is selected, then this code will display the form to select the category*/
Echo "Pls Select Category before clicking on Submit Category";
/* Breaking out of PHP here, to make the form sticky by using a php code inside form action*/
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Select the Category of Person: </legend><br />
<input type="radio" name="Category" value="Individual" /> Individual<br /><br />
<input type="radio" name="Category" value="Company, Pvt Ltd" /> Company, Pvt Ltd<br /><br />
<input type="radio" name="Category" value="Company, Ltd" /> Company, Ltd<br /><br />
<input type="radio" name="Category" value="Partnership Firm" /> Partnership Firm<br /><br />
<input type="radio" name="Category" value="LLP Firm" /> LLP Firm<br /><br />
<input type="radio" name="Category" value="HUF" /> HUF<br /><br />
<input type="submit" name='Submit Category' value="Submit Category" /><br />
</fieldset>
</form>
<?php
} Else {
$Category = $_POST["Category"];
Echo "$Category";
Echo "<br />";
/* Using swich statement to test the value of Category, and accordingly echo appropriate forms*/
switch ($Category) {
case "Individual":
if(!isset($_POST['Submit_Data_for_Individual'])){
//if no data for individual is submitted,
//then this code will display the form to enter and submit data for Individual
Echo /*displays message*/
"Pls Enter the Data for the Individual";
?>
<form action="<?php Echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<br />
First Namee: <input type="text" name="Individual_First_Name" />
<br />
Middle Name: <input type="text" name="Individual_Middle_Name" />
<br />
Last Name: <input type="text" name="Individual_Last_Name" />
<br />
Date of Birth: <input type="text" name="date_of_birth/incorporation" />
<br />
Gender:
<br />
<input type="radio" name="Gender" value="male" /> Male
<br />
<input type="radio" name="Gender" value="female" /> Female
<br />
Email 1: <input type="text" name="email_1" />
<br />
<input type="submit" name="Submit_Data_for_Individual" value="Submit Data for Individual" />
</fieldset>
</form>
<?php
}Else
{
$email_1 = $_POST["email_1"];
$Gender = $_POST["Gender"];
validate_email($email_1); // this is a custom function which i made
Echo $Gender; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
break;
case "Company, Pvt Ltd":
echo "Company, Pvt Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Partnership Firm":
echo "Partnership Firm";
break;
case "LLP Firm":
echo "LLP Firm";
break;
case "HUF":
echo "HUF";
break;
case NULL:
echo "Error: nothing selected";
break;
}
}
echo '</body>
</html>';
?>
答案 0 :(得分:3)
立即看到一个问题。
您正在检查名为Submit Data for Individual
的表单输入,但这是一个没有value
属性的提交按钮的name
。在提交按钮上设置name='submit-data'
属性并更改条件以检查名称而不是其值:
// This will never match.
if(!isset($_POST["Submit Data for Individual"])){
// Change it to
if(!isset($_POST["submit-data"])){
// Then change this
<input type="submit" value="Submit Data for Individual" />
// To this:
<input type="submit" name='submit-data' value="Submit Data for Individual" />
此外,switch
语句中的默认大小写使用default
关键字:
// You may safely change this:
case NULL:
echo "Error: nothing selected";
break;
// To this:
default:
echo "Error: nothing selected";
break;
<强>附录强>:
以下代码永远不可访问,因为表单会发布到另一个脚本create.php
。如果您将<form>
操作属性更改为回发到<?php $_SERVER['PHP_SELF'];?>
而不是create.php
,则应该看到else
案例。现在,它不起作用,因为您设置了if
的{{1}}测试。只有在提交表单时才能设置它,但表单提交到外部脚本。
$_POST["submit-data"]
要解决此问题并看到您的 // This else case can never be reached...
}Else
{
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"]; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
已回显,请暂时更改
Gender
附录2
您正在检查是否已设置类别,但在发布用户表单后,它不会是:
<form action="create.php" method="post">
// change to
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
然后,您需要测试用户表单是否已提交。在// Change
if(!isset($_POST["Category"])){
// To check that the user form was not submitted
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
部分之前,添加Else { $Category = $_POST['Category'];
以处理用户表单。
else if
最后,从if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
// Show the Category form...
}
// Process the user form...
else if (isset($_POST['submit-data'])) {
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"];
}
// Now process the categories or show the user form...
else {
$Category = $_POST['Category'];
// etc...
}
案例中移除整个Else
块,因为它不能在那里使用。