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

Php 如何在文本输入表单中键入代码?,php,forms,input,echo,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'];

我想知道将“$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" value="<?php echo htmlspecialchars($imagePath); ?>" />
</form>


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

表单发布并上传图像后,我将对图像的引用保存在一个隐藏标记“大图像”中。然后使用这个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" />'); 
}

});
$(文档).ready(函数(){
//读入对图像URL的隐藏引用
var large_photo=$(“#large_image”).val();
//显示相关图像(如果尚未上载图像,请使用占位符)
如果(大照片!=“”){
$(“#照片持有人”).html(“”);
}否则{
$(“#照片持有人”).html(“”);
}
});
那么,在您的情况下,您将不回显图像src,而是回显带有图像路径的隐藏标记


希望这有帮助:)

表单和代码之间有什么关系?我的“脚本”是一个上传脚本。。由于加载页面时没有上载图像,因此无法写出$imagepath。。我希望它在上传图像后在输入中写出$imagePath,以便显示图片的名称。如何执行此操作?@Per,如果您的表单与上面的上载代码位于同一脚本文件中,则在构建标记时,$imagePath将可用。你可以测试它是否为空,看看你是否应该将它添加到标记中。我不认为你明白我的意思。上传图像后,将显示该图片。但是我需要脚本在表单中写入文件名
$(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" />'); 
}

});