1

各位程序员好,

这是我第一次使用 AJAX,所以我真的需要一些帮助(项目截止日期是下周,只有几件事,我已经完成了)。

所以理论如下:

我正在做一个学生项目,这是一个人们可以宣传他们的待售房屋的网站。

它有一个 MySQL 数据库,其中包含用户和属性的相关信息。关于房产,我有 2 张桌子:

  • 属性(存储 id_property、id_user、名称、描述、地址等...)
  • propertyimages(存储 id_property、id_image、路径、上传日期)

我需要的是 AJAX 代码,它将一次显示一个图像,直到用户单击图像本身或单击图像下方的下一个/上一个 按钮,创建幻灯片,并使用 id_image 自动填充隐藏的文本框当前图像,因此用户可以单击将从数据库中删除图像的按钮/链接。

到目前为止,这是我的代码:

<?php session_start()?>
<?php include 'w3.html'; ?>
        <!-- put <title> code under here -->
<?php include 'essentialhead.html'; ?>
        <!-- put <head> code under here -->

<?php include 'closeheadopenbody.html'; ?>
            <!-- header -->
            <?php include 'header.html'; ?>
            <!-- content -->
            <?php include 'row1clear.html'; ?>
                    <?php 
                        $urlexplode = explode('?', "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
                        $token = end($urlexplode);

                        $connection = mysql_connect("localhost","root")
                                        or die("Problem connecting to MySQL!");
                        mysql_select_db("imobili");

                        $propertyquery = mysql_query("SELECT * 
                                                      FROM property 
                                                      WHERE id_property='".$token."'");             
                        $rowproperty = mysql_fetch_array($propertyquery);
                        echo $rowproperty['name'];
                    ?>
            <?php include 'row2clear.html'; ?>
                    <?php
                        echo'<div class="profilewrapper">
                                <div class="propertyslideshow">
                                    <div class="tablecell">
                                        <div id="slide" class="slider">'; **HERE IS WHERE I NEED AJAX TO SHOW THE IMAGES**
                                            $imagesquery = mysql_query("SELECT * 
                                                              FROM propertyimages
                                                              WHERE id_property='".$rowproperty['id_property']."'");                
                                            while($rowimage = mysql_fetch_array($imagesquery))
                                            {
                                                echo'<img class="sliderimg" alt="no image" src="'.$rowimage['path'].'">';                               
                                            }

                        echo'           </div>
                                    </div>
                                </div>
                                <div class="propertytools">'; **IF THE USER IS THE OWNER OF THE PROPERTY, OR AN ADMINISTRATOR, HE GETS ACCESS TO THE FOLLOWING 3 TOOLS: USE AS MAIN PHOTO, DELETE CURRENT IMAGE, AND DELETE PROPERTY**
                                    if($_SESSION['username'] == $rowproperty['username'] or $_SESSION['permissions'] == 'admin')
                                    {
                                    echo'<form class="property" method="post" name="uploadform" action="" enctype="multipart/form-data">
                                         <fieldset>
                                         <legend>Upload another image</legend>
                                         <input type="file" id="foto" name="foto"/><input class="submit" type="submit" value="Submit"/>
                                         </fieldset>
                                         </form>
                                         <form class="property" method="post" name="uploadform" action="">
                                         <input type="hidden" name="currentimgid" value="**I NEED THE id_img here in real time!**"/>


                                         <input onclick="submitform()" class="submitlink" type="submit" id="propform" name="setprev" value="Use this image as Preview"/>
                                         **THE INPUT BELOW WILL POST THE id_image**
                                         <input onclick="submitform()" class="submitlink" type="submit" id="propform" name="delimg" value="Delete this image"/>
                                         <input onclick="submitform()" class="submitlink" type="submit" id="propform" name="delprop" value="Delete this property"/>
                                         <script>
                                            function submitform()
                                            {
                                                var r=confirm("Are you sure?");
                                                if(r==true)
                                                {
                                                    document.propform.submit();
                                                }
                                            }
                                         </script>
                                         </form>';
                                    }
                        echo'   </div>
                                <div class="propertydetails">
                                    <div class="profiledetails">Date Of Construction</div>'.$rowproperty['dateconstruction'].'
                                    <div class="profiledetails">Rent</div>'.$rowproperty['rentvalue'].'
                                    <div class="profiledetails">Sell</div>'.$rowproperty['sellvalue'].'
                                </div>
                            </div>';



                        echo'   <form class="comment" name="commentform" action="">
                                Leave a comment<br>
                                <textarea name="commentinput"></textarea>
                                <input type="submit" class="submit" value="Send">
                                </form>';
                            mysql_close();
                    ?>
                    <?php
                        if(ISSET($_POST['delprop']))
                        {
                            echo'';
                            $sql = "DELETE FROM property
                                   WHERE id_property = ".$token;
                            mysql_query($sql);

                            $sql = "DELETE FROM propertyimages
                                   WHERE id_property = ".$token;
                            mysql_query($sql);
                        }
                    //var_dump($_FILES['foto']);
                        if(ISSET($_FILES['foto']))
                        {
                            $username = $_SESSION['username'];
                            //upload
                            $allowedExts = array("gif", "jpeg", "jpg", "png", "JPG");
                            $explode = explode(".", $_FILES["foto"]["name"]);
                            $extension = end($explode);
                            //var_dump($extension);
                            if ((($_FILES["foto"]["type"] == "image/gif")
                            || ($_FILES["foto"]["type"] == "image/jpeg")
                            || ($_FILES["foto"]["type"] == "image/jpg")
                            || ($_FILES["foto"]["type"] == "image/pjpeg")
                            || ($_FILES["foto"]["type"] == "image/x-png")
                            || ($_FILES["foto"]["type"] == "image/png"))
                            && ($_FILES["foto"]["size"] < 200000000) //20MB
                            && in_array($extension, $allowedExts))
                            {
                                if ($_FILES["foto"]["error"] > 0)
                                {
                                echo "Return Code: " . $_FILES["foto"]["error"] . "<br>";
                                }
                                else
                                {
                                echo "Upload: " . $_FILES["foto"]["name"] . "<br>";
                                echo "Type: " . $_FILES["foto"]["type"] . "<br>";
                                echo "Size: " . ($_FILES["foto"]["size"] / 1024) . " kB<br>";
                                echo "Temp file: " . $_FILES["foto"]["tmp_name"] . "<br>";
                                $path1 = "upload/".$username;
                                echo $path1;
                                $path2 = "upload/".$username."/".$token;
                                    $dest = "upload/".$username."/".$token. "/". $_FILES["foto"]["name"];
                                    echo $dest;
                                    if (file_exists("upload/" . $_FILES["foto"]["name"]))
                                    {
                                        echo $_FILES["foto"]["name"] . " already exists. ";
                                    }
                                    else
                                    {
                                    if ( ! is_dir($path1)) {
                                        mkdir($path1);
                                    }
                                    if ( ! is_dir($path2)) {
                                        mkdir($path2);
                                    }
                                    move_uploaded_file($_FILES["foto"]["tmp_name"], $dest);
                                    }
                                }
                            $datetime = date_create()->format('Y-m-d H:i:s');
                            $sqlimages = "INSERT INTO propertyimages (path, uploaddate, id_property) VALUES('$dest', '$datetime', '$token')";                       
                            //var_dump($sqlimages);
                            mysql_query($sqlimages);
                            }
                            else
                            {
                                echo "<script>alert(\"Wrong extension\")</script>";
                            }
                        }
                    ?>
            <?php include 'contentclose.html'; ?>
            <!-- footer -->
            <?php include 'footer.php'; ?>
4

2 回答 2

0
  1. 您必须只向用户显示第一张图片。
  2. 将当前图像变白到会话。
  3. 制作一个请求下一张图片的按钮。
  4. 当从服务器获取下一张图像时,在同一会话中将新图像索引白下来并继续。我不能给你写代码它很复杂。
于 2013-06-28T13:17:36.880 回答
0

使用像jQuery这样的实用程序库将在这里为您提供极大的帮助。看看$.ajax() 方法,你应该能够从那里掌握它。还有$.post()一个速记版本,用于从服务器发送和检索数据。例如:

$('.button').on('click', function(e) {
    e.preventDefault();

    $.post('url.php', { data: 'here' }, function(result) {
        // `result` contains the server response
    });
});

url.php只是一个接受 POST 数据的普通 PHP 脚本。任何echoed 都会被发送回result.

于 2013-06-28T13:19:20.510 回答