【问题标题】:Type code in to a text input form, how?在文本输入表单中输入代码,如何?
【发布时间】:2010-11-22 05:13:33
【问题描述】:

我想知道将“$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>

【问题讨论】:

  • 表单和代码之间的关系是什么?

标签: php forms input echo


【解决方案1】:

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

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

【讨论】:

  • 我的“脚本”是一个上传脚本。而且由于您在加载页面时没有上传图片,所以您无法写出 $imagepath.. 我希望它写出 $imagePath在上传图像时的输入中,因此它显示图片的名称。我该怎么做?
  • @Per,如果您的表单与上面的上传代码位于相同的脚本文件中,那么在您构建标记时 $imagePath 将可用。您可以测试它是否为 null 以查看是否应将其添加到标记中
【解决方案2】:

请注意,执行此操作时通常会出现缓存和计时问题(因此,如果您在提交表单后实际查看图像时遇到问题,请参见下文),我想这与图像不存在有关在加载结果页面时就位。我通过使用 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,而是回显带有图像路径的隐藏标签。

希望这会有所帮助:)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多