如何在onClick事件发生时从数据库中填充texbox

时间:2015-12-29 14:12:09

标签: php arrays

我希望我的 php 文件根据选择框标记的选定值用数据库中的信息填充文本框。每次选择更改时,应相应地重新填充文本框。

问题在于,当我调用包含 php 代码的 javascript 方法时,会重新加载页面,并在页面为$_POST[]时收集一些初始值第一次装的都丢了。

我想找到一种方法,即使在页面重新执行 php 之后,$_post[]收集的初始值也会得到保留。我该如何解决这个问题。

<script type="text/javascript">
function displaymessagespatient()
{

<?php
    function docmsg()
   { 
    $title = $_POST["title"];
    $conn=mysqli_connect("localhost","root","","askthedoctor");
    $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);
    return $row1[0];
    } 
     function patmsg()
   { 
    $title = $_POST["title"];
    $conn=mysqli_connect("localhost","root","","askthedoctor");
    $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);
    return $row2[0];
    } 


  ?>

    document.getElementById("answer").innerHTML=docmsg();
    document.getElementById("question").innerHTML=patmsg();         
} 
</script>
</head>


<body>

<?php
if( isset($_POST['username']))
{   
$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="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>
<td><textarea rows="4" col="50" id ="question" readonly> </textarea></td>
<td><textarea rows="4" col="50" id ="answer" readonly> </textarea></td>
</tr>
<tr>
<td></td>
</tr>

<tr>
<td><input type="submit" name="openmessage" value="Display Selected Message" onClick="displaymessagespatient()"></td>
<td><input type="button" name="btnSubmit" value="Ask a new question" ></td>
</tr>

</table>
</fieldset>
</div>  
</div>

</form>
<?php } ?>
</body>

0 个答案:

没有答案