简体   繁体   English

如何在HTML中使用JavaScript显示PHP回声?

[英]How to display PHP echo with JavaScript in HTML?

I want to transfer PHP echo text into HTML through JavaScript.I've seen that it can be done with the function document.getelementbyid() and innerHTML but I tried and it did not work. 我想通过JavaScript将PHP回声文本转换为HTML,我已经知道可以通过函数document.getelementbyid()和innerHTML来实现,但是我尝试了一下并且没有用。

This PHP Code: 此PHP代码:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}
?>

I want to echo the text displays in HTML Without switching to upload.php!! 我想在不切换到upload.php的情况下回显HTML中的文本显示!

On id statusbar I want to show echo text! 在ID状态栏上,我想显示回显文本!

This HTML code: 此HTML代码:

<form action="../assets/php/upload.php" method="post" enctype="multipart/form-data">
<label>Izaberi fotografiju:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit" onClick="info()">
<p id="statusbar"></p> 
</form>

You have to write code inside <?php ?> tag.Like this. 您必须在<?php ?>标签内编写代码。

<script type="text/javascript">
 //js code..

     <?php echo "Sory, only PNG, JPG, GIF"; ?>

</scrip>

You can directly echo things to html. 您可以直接将内容回显到html。 You can also echo things to a js variable directly like this, then set the html content with js. 您也可以像这样直接将内容回显到js变量,然后使用js设置html内容。

<script>
  var string = <?php echo "Sory, only PNG, JPG, GIF"; ?>
  document.getElementById("notify-container").innerText = string;
</script>

Say your HTML element was the following: <div id="error"></div> , you'd assign the error message to a JavaScript variable like so: <script> var errorMsg = <?php echo "Sory, only PNG, JPG, GIF"; ?> </script> 假设您的HTML元素如下: <div id="error"></div> ,您将错误消息分配给JavaScript变量,如下所示: <script> var errorMsg = <?php echo "Sory, only PNG, JPG, GIF"; ?> </script> <script> var errorMsg = <?php echo "Sory, only PNG, JPG, GIF"; ?> </script> Then you can show the message like so: document.getElementById('error').innerHTML = errorMsg; <script> var errorMsg = <?php echo "Sory, only PNG, JPG, GIF"; ?> </script>然后,您可以显示如下消息: document.getElementById('error').innerHTML = errorMsg;

let's say you have to print this in your uploading html page 假设您必须在上传的html页面中打印此内容

if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}

change this to below 将此更改为以下

if ($uploadOk == 0) {
$message = "Sorry, your file was not uploaded.";
header("Location: /index.php?message="$message);
// if everything is ok, try to upload file
}

then get the message and print in your html page 然后获取消息并在您的html页面中打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM