Javascript DOM操作问题
我已成功将我的图像上载到数据库,我的代码:Javascript DOM操作问题,javascript,php,html,dom,Javascript,Php,Html,Dom,我已成功将我的图像上载到数据库,我的代码: <?php if(isset($_POST['submit'])){ $name = $_POST["inputEntryName"]; $picture_tmp = $_FILES['inputFile']['tmp_name']; $picture_name = $_FILES['inputFile']['name']; $picture_type = $_FILES['inputFile']['type'];
<?php
if(isset($_POST['submit'])){
$name = $_POST["inputEntryName"];
$picture_tmp = $_FILES['inputFile']['tmp_name'];
$picture_name = $_FILES['inputFile']['name'];
$picture_type = $_FILES['inputFile']['type'];
$desc = $_POST["desc"];
$path = "upload/". $picture_name;
if (file_exists("upload/" . $picture_name)){
echo $picture_name . " already exists. ";
}
else{
if(move_uploaded_file($picture_tmp,$path)){
require_once "database.php";
$db = new Database();
$insQuery = "insert into image(name,image,description) values('".$picture_tmp."','".$path."','".$desc."')";
$result = $db->query($insQuery);
echo "File successfully uploaded!";
}
else{
echo "File is not uploaded.";
}
}
}
?>
关于:
// ... assuming that this is the array of the images
$arr = ...
for ( $i = 0; $i < count( $arr ); $i++ ) {
echo "<h1>Name</h1>";
echo "<img src=\"" . $arr[ $i ] . "\" />";
echo "<p>Description</p>";
}
/。。。假设这是图像的数组
$arr=。。。
对于($i=0;$i”;
}
在这里,您可以通过与获取图像相同的方式获取名称和描述 根据要求,提供一个快速示例,指导您在提交表单和更新数据库后显示图像
<?php
session_start();
require_once 'database.php';
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* Perform any checks to ensure there is file to proceess */
/* Handle file upload */
/* Insert record into db */
}
?>
<html>
<head>
<title>File upload and image display</title>
</head>
<body>
<!--
by omitting the action, we automatically POST the form to the same page
and is processed at the top of the script
-->
<form name='fileupload' method='post' enctype='multipart/form-data'>
<!--
Include all the fields & buttons required to upload file(s)
-->
</form>
<!-- :: Display images :: -->
<div id='images'>
<?php
/*
This is slightly pseudo code because it is impossible to know the
exact manner in which your `database` class works - mysql, mysqli
or possibly pdo.
*/
$sql='select `name`,`image`,`description` from `image`;';
if( !$db ) $db = new Database;
$res=$db->query( $sql );
if( $res ){
while( $rs=$res->fetch_object() ){
echo "
<div>
{$rs->name}
<img src='/path/to/images/{$rs->image}' alt='{$rs->description}' />
{$rs->description}
</div>";
}
}
$db->close();
?>
</div>
<!-- :: End display images :: -->
</body>
</html>
文件上传和图像显示
我试图做的是自动生成一个包含所有变量的div,然后将其附加到主容器中。你的代码怎么做?你怎么上传图像?如果是通过javascript,则该方法将不同于通过更传统的表单提交。我是通过传统的表单提交来实现的,如上面的代码所示。由于您使用的是标准表单POST而不是javascript,因此似乎不需要使用PHP或javascript进行DOM操作-只需在运行时使用查询从数据库中检索图像详细信息(对于所有图像),并通过记录集循环生成所需的html结构。您能指导我如何操作吗?我编辑了这篇文章,包括我的函数。是的,这就是我要找的,就像你发表评论一样,我发现我可以用php代码返回html!!。谢谢
<?php
session_start();
require_once 'database.php';
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* Perform any checks to ensure there is file to proceess */
/* Handle file upload */
/* Insert record into db */
}
?>
<html>
<head>
<title>File upload and image display</title>
</head>
<body>
<!--
by omitting the action, we automatically POST the form to the same page
and is processed at the top of the script
-->
<form name='fileupload' method='post' enctype='multipart/form-data'>
<!--
Include all the fields & buttons required to upload file(s)
-->
</form>
<!-- :: Display images :: -->
<div id='images'>
<?php
/*
This is slightly pseudo code because it is impossible to know the
exact manner in which your `database` class works - mysql, mysqli
or possibly pdo.
*/
$sql='select `name`,`image`,`description` from `image`;';
if( !$db ) $db = new Database;
$res=$db->query( $sql );
if( $res ){
while( $rs=$res->fetch_object() ){
echo "
<div>
{$rs->name}
<img src='/path/to/images/{$rs->image}' alt='{$rs->description}' />
{$rs->description}
</div>";
}
}
$db->close();
?>
</div>
<!-- :: End display images :: -->
</body>
</html>