在文本输入表单中键入代码,如何?

时间:2009-08-24 09:07:44

标签: php forms input echo

我想知道将“$ imagepath”写入此输入的最佳方法

这是我的上传脚本

<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "temporary_images/".$imagename;
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "temporary_images/" . $imagepath; //This is the new file you saving
              $file = "temporary_images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 350;                         
              $modheight = 100; 

              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 

              $save = "temporary_images/sml_" . $imagepath; //This is the new file you saving
              $file = "temporary_images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 80; 
              $modheight = 100; 

              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 
            echo "Large image: <img src='temporary_images/".$imagepath."'><br>"; 
            echo "$imagepath"
          }
        }

这是我的表格

<form>
 <input name="animeinput" id="animeinput" size="20" class="textbox">
</form>

2 个答案:

答案 0 :(得分:2)

如果你有可用于标记的var:

<form>
   <input name="animeinput" id="animeinput" size="20" class="textbox" value="<?php echo htmlspecialchars($imagePath); ?>" />
</form>

答案 1 :(得分:0)

请注意,执行此操作时,通常会出现缓存和计时问题(因此,如果您在提交表单后遇到问题时遇到问题,请参阅下文),我想这与图像不存在有关在加载结果页面时到位。我通过使用jquery / ajax更新我的图像解决了这个问题。

发布表单并上传图片后,我会在隐藏标签#large_image中保存对图片的引用。然后使用这个jquery ....

$(document).ready(function(){

    //read in hidden reference to image URL
var large_photo = $("#large_image").val();

   //display relevent image (use placeholder if no image uploaded yet)
if (large_photo != "") {
    $("#photo_holder").html('<img src="' + large_photo + '" />');
} else {
    $("#photo_holder").html('<img src="noimagefound.png" />'); 
}

});

在你的情况下,不是回显图像src,而是回显一个带有图像路径的隐藏标签。

希望这会有所帮助:)