Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 发生onClick事件时如何从数据库填充文本框_Php_Arrays - Fatal编程技术网

Php 发生onClick事件时如何从数据库填充文本框

Php 发生onClick事件时如何从数据库填充文本框,php,arrays,Php,Arrays,我希望我的php文件根据select box标记的选定值用数据库中的信息填充文本框。每次选择更改时,文本框都应相应地重新填充 问题是,当我调用包含php代码的javascript方法时,页面被重新加载,并且当页面第一次加载时,$\u POST[]收集的一些初始值丢失 我想找到一种方法,即使在页面重新执行php之后,$\u post[]收集的初始值也能得到保存。我怎样才能解决这个问题 <script type="text/javascript"> function displaymes

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

问题是,当我调用包含php代码的javascript方法时,页面被重新加载,并且当页面第一次加载时,
$\u POST[]
收集的一些初始值丢失

我想找到一种方法,即使在页面重新执行php之后,
$\u 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" >

函数displayMessageSpatialent()
{

我建议你重新考虑你的方法。对于这种类型的事情,我使用ajax,让javascript在需要时联系单独的php页面获取数据库信息。然后javascript可以整理数据并将其排列到html中。这意味着该页面不需要重新加载,而且速度更快,外观更好,而且你拥有的内容也没有问题有问题。这不起作用,因为你正在混合javascript和PHP。你需要一个PHP文件来通过ajax将内容返回到javascript。PHP只向客户端输出服务器计算。然后它完成执行。ajax将再次运行PHP,获得结果并完成工作在internet上查找一些ajax教程,有成千上万的em:)
<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>