如何在没有提交的情况下将值从php发送到另一个php页面?

时间:2014-01-22 20:04:24

标签: php

我已经进行了深入搜索,并尝试了很多将值自动发送到其他php页面。我不知道是不是可能。但如果有人知道那么请告诉我。

以下是我正在尝试的代码:

t.html //用于浏览

<html>
<head>
 <style>
 #boox{
    overflow:auto;
    width:600px;
    height:400px;
    }
 </style>

<script>
alert("hello");
</script>
<script>

    function pict(str)
    {    alert("hel");
         if (str=="")
            {
            document.getElementById("txtHint").innerHTML="";
            return;
            } 
        xmlhttp = new XMLHttpRequest();
        var picInput = document.getElementById('userfile').value;
        var uploadpic = document.getElementById('upload').value;
        document.getElementById('usf').innerHTML = picInput;
        document.getElementById('upic').innerHTML = uploadpic;  
        xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      //   var resp = xmlhttp.responseText;
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      //   alert(resp);
      } alert("a");
        xmlhttp.open( "POST", "show.php.php?q"="+str, true); //POST Because you use $_POST in php
xmlhttp.send();// not happening
   }

    }    
    </script> 
    </head>
    <body>
    <form method="post" enctype="multipart/form-data" action="take.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<input name="upload" type="submit" onChange ="pict(this.value)" class="box" id="upload" value=" Upload ">
</tr>
</table>
</form>
<div  id="txtHint">here div </div>
    </body>
     </html> 

take.php //这是我试图发送而不提交给其他php的页面

<?php
    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
        {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        $fp      = fopen($tmpName, 'r');
        $content = fread($fp, filesize($tmpName));
        $content = addslashes($content);
        fclose($fp);
        if(!get_magic_quotes_gpc())
        {
            $fileName = addslashes($fileName);
        }
         $con=mysql_connect("localhost","root",'');
         mysql_select_db("project",$con) or die("error db");

        $query = "INSERT INTO upload (name, size, type, content ) ".
        "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
        mysql_query($query) or die('Error, query failed');
        mysql_close($con);
        echo "<br>File $fileName uploaded<br>";
// i am trying to send parameter that is "fileName" through header to show.php
header("location:show.php");
        }
      ?>

show.php

<HTML>

<head>
<script>
#showpic
{
overflow:auto;
width:600px;
height:400px;
border:#000000 2px solid;

}
</script>
<script>

     function changeThis(){

     xmlhttp = new XMLHttpRequest();
     var formInput = document.getElementById('theInput').value; 
     var title = document.getElementById('title').value; 
     /* formInput will be undefined */
     document.getElementById('newText').innerHTML = formInput;
     document.getElementById('ntitle').innerHTML = title;
     /* also undefined */
//    var xmlHttp = null;

    xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         var resp = xmlhttp.responseText;
         document.getElementById("txtHint").innerHTML=resp;
         alert(resp);
      }
   }
//alert("yes");
   xmlhttp.open( "POST", "file2.php?q=+" , true); //POST Because you use $_POST in php
xmlhttp.send('theInput='+ encodeURIComponent(formInput) + 
         '&newText='+ encodeURIComponent(formInput) +
         '&ntitle='+ encodeURIComponent(title));
    } 
     </script>

</head>
<div id="showpic">
<?php
//header("location:event.php");

echo"heloo00";
 $con=mysql_connect("localhost","root",'');
 mysql_select_db("project",$con) or die("error db");

$sql="select * from upload";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row ['name'];
echo '<img src="data:image/png;base64,' . base64_encode( $row['content'] ) . '" />';
}
?>
</div>

<body>
<p> <span name id='ntitle'></span> </p> 
     <p>You wrote: <span id='newText'></span> </p>
     <body>
    <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >  
    Title :<textarea name="des"   rows="1" cols="25" required></textarea>
                  <br>
    Write :<textarea name="cht"   rows="25" cols="25" required  >write</textarea>     
     <input type=submit name="submit"  onclick='changeThis() value ='Post event'/>
    </form>
    <?PHP

if(isset($_GET['submit']))
{
$u=$_GET['des'];
$k=$_GET['cht'];

$q="insert into event (title,detail) value('$u','$k')";
 $con=mysql_connect("localhost","root","") or die ("connection Error");;
mysql_select_db("project",$con) or die ("db Error");
$r=mysql_query($q) or die ("Query Error");;

}


?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

要在php文档之间共享数据而不显式传递该数据,可以将其存储在$ _SESSION变量中。当用户在同一会话期间访问php脚本时,这使得数据可用。会话在这里有很多解释,但您可以在session handling的PHP手册部分阅读它们。如果您有更具体的问题,请在此处发布,我们可以帮助您。