php文件,具有内部和外部引用


php file with both internal and external reference

我有两个php文件。首先说php1调用php2。现在php2包含在php1中form1的action属性中,因此它可以通过$_POST[]通过名称引用form1的所有标记。但是php2包含的代码也应该操作php2标记中包含的数据。每次我试图通过$_POST[]访问php2的标签时,它都会显示"未定义索引"。如何解决这个问题,以便php2也可以通过$_POST访问其内部标记?

php1

<form id="ee" method="POST" onsubmit="return validatelogin();"  action="login.php">
<div id="container" style="text-align: center;">
<div id="header" class="class_header">
<a href="project_index.php" align="right" color="white">Go back to index</a>
</div>
<div id="body" >
<br/><br/>
<font color="white" ><h1 color="white" style="font-size:200%;" align="center">Log In</h1></font>    
</div>
<div id="body" style="height: 30%; width: 60%;" class="div">
</br></br>
<fieldset id="login"><legend><strong>Log In</strong></legend>
<table>
<tr>
<th>Username</th>
<td><input type="text" name="username"></td>
</tr>   
<tr>
<th>Password</th>
<td><input type="password" name="password"></td>
</tr>
<tr>
<th><input type="button" value="Forgot Password" name="forgotpassword" class="link" onClick="forgotpassword1();"/></th>
</tr>
<tr>
<td></td>
<td>
<input type="button" name="Log In" value="Log In" onClick="login1();" >
</td>
</tr>   
</table>

php2

<?php
$username=$_POST["username"];
$password=$_POST["password"]; 
$password=md5($password);
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql="select password from login where username='".$username."';";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
if($row[0]!=$password)
{ 
  echo "The username or password that you entered are incorrect!";
  echo "<br/>";
  echo "<a href='"project_login.php'">Go back to login</a>";
  die();
}
$sql1="select userprivileges from login where username='".$username."';";
$sql2="select image_path from registration1 where id=(select id from login      where username='".$username."');";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2); 
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
$imagepath=$row2[0];
if($row1[0]==2)
{
?>
<form  id="patient" method="POST" enctype="multipart/form-data"     action="login.php" >
<div id="container">
<div id="header" class="class_header">
<a href="project_login.php" align="right" color="white">Sign Out</a>
</div>
<div id="body" >
<br/>
<table class="table">
<tr>
<td><font color="white" ><h1 color="white" style="font-size:200%;"    align="center">Welcome <?php echo $username;?></h1></font></td>   
<td> <div id="box"><image height="65px" width="65px" src="<?php echo   $imagepath; ?>"></div></td>
<tr>    
</table>
</div>

<div id="separator" ></div>
<div id="separator" ></div>
<div id="bodyy" style="height: 60%; width: 60%;"  class="div">
<br/><br/><br/>
<fieldset id="registration">
<table class="table">
<tr>
<th>Messages</th>
<?php
$sql="select title from messages where paitient_id=(select id from login where username='".$username."');";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
?>  
<th>Problem Description</th>
<th>Doctor's Answer</th>
<th></th>
</tr>
<tr>
<td><select name="title"> 
<?php echo "<option value='"mesazhi1'">".$row[0]."</option>";}?>
</select>
</td>
<?php
///////////////////////////////////////////////////////////
$title=$_POST['title'];///////Here happens the problem**********
////////////////////////////////////////////////////////////
$sql1="select patient_text from messages where title='".$title."';";
$sql2="select doctor_text from messages where title='".$title."';";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
?>
<script type="text/javascript">
function displaymessage()
{
    var doctoranswer=<?php echo $row1[0];?>;
    var patientquestion=<?php echo $row2[0];?>;
    document.getElementById("answerswer").innerHTML=doctoranswer;
    document.getElementById("question").innerHTML=patientquestion;
}
</script>
<td><textarea rows="4" col="50" id ="question" readonly> </textarea></td>
<td><textarea rows="4" col="50" id ="answerswer" readonly> </textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="button" name="openmessage" value="Display Selected Message" onClick="displaymessage()"></td>
<td><input type="button" name="btnSubmit" value="Ask a new question" ></td>
</tr>
</table>
</fieldset>
</div>
</div>

</form>
<?php } ?>

问题是,php1中没有名称为"title"的输入字段。为了实现这一点,您可以添加

 <input type="hidden" name="title" value="yourTitle" />

您还可以测试索引是否存在,这样php就不会退出代码。

 if(isset($_POST["title"])) {
    $title = $_POST["title"];
    // ....
 } else {
    // no index is setup
    //...
 }