用php写了一个简单的发布信息页面,上传完图片后页面会自动刷新,之前表单中填写的内容就全没了,怎么让上传完成后还能保留表单中的内容
希望能帮忙解决
<?php
session_start();
 include("config.php");
 $myselect1 = -1;
 $username3=$HTTP_SESSION_VARS["id"];
$title1=$HTTP_POST_VARS["title"];
$content1=$HTTP_POST_VARS["content"];
 $myselect1=$HTTP_POST_VARS["myselect"];
  $buy_or_sell=$HTTP_POST_VARS["radio"];
$withimg1=$HTTP_POST_VARS["withimg"];
if($withimg1 == "withimg")
{
$withimg1 = "1";
}
else
{
$withimg1 = "0";
}
$_SESSION['withimg']=$withimg1;
date_default_timezone_set(PRC);
 $savetime =date("d-m-y G:i");
//上传部分
 if($_POST['upload']){ if (is_uploaded_file($_FILES['upfile']['tmp_name'])){$upfile=$_FILES["upfile"];$name = $upfile["name"];
$type = $upfile["type"];
$size = $upfile["size"];
$tmp_name = $upfile["tmp_name"];
$error = $upfile["error"];
//echo $name;
$_SESSION['imgsrc']=$name;
//echo $_SESSION['imgsrc'];
switch ($type) {
case 'image/pjpeg' : $ok=1;
break;
case 'image/jpeg' : $ok=1;
break;
case 'image/gif' : $ok=1;
break;
case 'image/png' : $ok=1;
break;
}if($ok && $error=='0'){
 move_uploaded_file($tmp_name,'up/'.$name);
 echo "<script language=\"javascript\">alert('上传成功,请选中有图片后填写信息内容!');window.location.href='sendmessage.php';</script>";
         }
      }
  }
if($_POST['submit']){ if($buy_or_sell=="sell"){
$query1 = "insert into  mes_".$username3." (username,messageAttribute,messageClassify,messageTitle,messageContent,time,receivefrom,ischecked)" .
  " values ('$username3','sendsell','$myselect1','$title1','$content1','$savetime' ,'checkman','checking')";
$result=@mysql_query($query1)or die("未插入个人信息表成功!");
 if($result) {echo "<script language=\"javascript\">alert('发布成功');window.location.href='showsendmessagetitle.php';</script>";}
$result1 = mysql_query("select max(messageid) maxid from mes_".$username3) or die("选择messageid错误");
$row = mysql_fetch_array($result1);
$query2 = "insert into `all_message` (username,messageAttribute,messageClassify,messageTitle,messageContent,time,receivefrom,ischecked,user_tableid)" .
  " values ('$username3','sendsell','$myselect1','$title1','$content1','$savetime' ,'checkman','checking','$row[maxid]')";
  $result=@mysql_query($query2)or die("未插入总信息表成功!");
}
      if($buy_or_sell=="buy"){
       $query1 = "insert into `mes_".$username3."` (username,messageAttribute,messageClassify,messageTitle,messageContent,time,receivefrom,ischecked)" .
  " values ('$username3','sendbuy','$myselect1','$title1','$content1','$savetime' ,'checkman','checking')";
$result=@mysql_query($query1)or die("未插入个人信息表成功!");
 if($result) {echo "<script language=\"javascript\">alert('发布成功');window.location.href='showsendmessagetitle.php';</script>";}
$result1 = mysql_query("select max(messageid) maxid from mes_".$username3) or die("选择messageid错误");
$row = mysql_fetch_array($result1);
$query2 = "insert into `all_message` (username,messageAttribute,messageClassify,messageTitle,messageContent,time,receivefrom,ischecked,user_tableid)" .
  " values ('$username3','sendbuy','$myselect1','$title1','$content1','$savetime' ,'checkman','checking','$row[maxid]')";
  $result=@mysql_query($query2)or die("未插入总信息表成功!");
      }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>发布信息</title>
<SCRIPT language=javascript>
function CheckPost()
{
if (form1.title.value=="")
{
alert("标题不能为空");
form1.title.focus();
return false;
}
if (form1.content.value=="")
{
alert("内容不能为空");
form1.content.focus();
return false;
}
if (form1.myselect.value=="-1")
{
alert("请选择分类");
form1.myselect.focus();
return false;
}
}
</SCRIPT>
<style>
#header{
width:900px;
height:40px;
}
#showmenu {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
float:left;
}
#showmenu a,#showmenu a:visited {
display:block;
width:10em;
padding:0.25em;
margin:0.5em auto;
color:#fff;
text-decoration:none;
text-align:center;
background-color:#8ab;
border:2px solid #fff;
border-color:#def #678 #345 #cde;
position:relative;
}
#showmenu a:hover {
top:2px;
left:2px;
border-color:#345 #cde #def #678;
}
}
#showbody {
width:980px;
height:200px;
}
#showleft {
width:200px;
height:200px;
float:left;
padding:20px;
}
#showright {
width:700px;
height:200px;
float:right;
padding-top:35px;
}
</style>
</head>
<body>
<div id="header">
<table align="left" width="900px" height="40px">
  <tr>
      <td width="20%" align="left" style="margin-left=20px">
      &nbsp;&nbsp;用户名:<a href="showperinformation.php"><?php  $username3=$_SESSION['id']; echo $username3;?>
      </a></td>
      <td width="5%" align="center"><a href="logoff.php"><font size="-1">注销</font></td>
      <td width="15%" align="center"><a href="showperinformation.php">个人信息</a></td><td width="15%" align="center"><a href="sendmessage.php">信息管理</a></td>
      <td width="15%" align="center"><a href="sendmessage.php">公告管理</a></td>
      <td width="15%" align="center"><a href="waitcheckmessage.php">信息审核</a></td><td width="15%" align="center"><a href="#">用户管理</a></td>
  </tr>
</table>
</div>
<div id = "showbody">
<div id = "showleft">
<div id="showmenu">
    <a href="showperinformation.php"> 个人信息 </a>
    <a href="sendmessage.php"> 发布信息 </a>
    <a href="showsendmessagetitle.php"> 发件箱管理 </a>
    <a href="showreceivedmessagetitle.php"> 收件箱管理 </a>
</div>
</div>
<div id = "showright">
<form name="form1" method="post"   action="sendmessage.php" onsubmit="return CheckPost();"><h1>发布信息</h1><br />
<p>信息标题:&nbsp;&nbsp;<input name="title" type="text" id="nickname">
<input name="withimg" type="checkbox" value="withimg">有图片
<input type="radio" name="radio"  value="buy" >我想买
<input type="radio" name="radio"  value="sell" checked>我想卖
<br /><br />
信息内容:<br />
<textarea name="content" cols="70" rows="20" style="margin-left:83px; margin-top:-15px;"></textarea><br /><br />
信息分类:<select name="myselect" id="myselect" style="float:left; margin-left:80px; margin-top:-20px;">
  <option value="-1">----请选择----</option>
  <option value ="1">财经类图书信息</option>
  <option value ="2">文学类图书信息</option>
  <option value="3">计算机类图书信息</option>
  <option value="4">工具类图书信息</option>
  <option value="5">英语类图书信息</option>
  <option value="6">其他类图书信息</option>
  <option value="51">二手货信息信息</option>
  <option value="61">房屋租赁信息</option>
  <option value="71">招领类信息信息</option>
  <option value="91">招聘类信息</option>
</select><br>
<input type="submit" name="submit" value="发布信息" style="float:left; margin-left:350px; margin-top:10px;"><br/><br></p></form>
<form name="form2" method="post" action="sendmessage.php" enctype="multipart/form-data" >
上传图片:
  <input name="upfile" type="file">
  <input type="submit" name="upload"  value="上传"><br></form></div>
</div>
</body>
</html>

解决方案 »

  1.   

    将表单元素的value设置为提交的数据例如<input type="text" name="sex" value="<?php isset($_POST['sex'])?$_POST['sex']:''; ?>"
      

  2.   

     echo "<script language=\"javascript\">alert('上传成功,请选中有图片后填写信息内容!');window.history.go(-1);</script>";
      

  3.   

    我有两个form,form1提交时将数据写入数据库,form2完全是上传图片的代码    点击form2的上传按钮会把页面舒心一边,然后之前写在form1 的内容就没了       
      

  4.   

    就算我把2楼所说的那个地方不用alert提示,就算什么都不提示也会刷新,愁死了   我写的代码可读性很差   所以也不好解决    唉 新手没经验  真可怕
      

  5.   

    你前面不是用$withimg1=$HTTP_POST_VARS["withimg"];取值了吗
    把这个值写到input 中的value
      

  6.   

    可是只有当点击发布信息的那个按钮后   才会把$HTTP_POST_VARS["withimg"]的值取出来啊  点击上传图片肯定是在点击发布信息之前的,所以$withimg1=$HTTP_POST_VARS["withimg"]是不会取到值的啊    
      

  7.   

    我写了啊 window.history.go(-1);是回到上一页了啊  但是没有啊  是不是我用了两个form的原因啊  能不能写在一起啊
      

  8.   


    所以加个判断是否提交了,如果提交了,就是post的值,否则就是空值啊。
      

  9.   

    是把表单的value都改成像这样吗  
    <p>信息标题:&nbsp;&nbsp;<input name="title" type="text" id="nickname" value="<?php   echo $title1; ?>">
      

  10.   

    嗯  俩表单是分开的     不过点击上传图片的按钮如果之前在标题和内容里边填写数据的话    $title1=$HTTP_POST_VARS["title"];
    $content1=$HTTP_POST_VARS["content"]; $myselect1=$HTTP_POST_VARS["myselect"];    这三步能把title和content取出来吗
      

  11.   

    可是上传的那个也是个表单啊,点击 上传  之后不应该也能把信息取出来吗         这两个表单我觉得无论点击哪个按钮都能取出来值         信息标题:&nbsp;&nbsp;<input name="title" type="text" id="nickname" value="<?php echo $title1; ?>">但是value改完后还是没有值       我该怎么办