[在单击按钮时使用AJAX张贴图片的艰苦时间

问题描述 投票:0回答:2

我在理解ajax时遇到问题。单击HTML上的按钮时,我试图上载图片数组,以便图像更新到HTML站点而不刷新。

我的HTML:

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Homework 12</title>
</head>
<body>
  <h1>Pictures of Pets</h1>
  <form id="my_form_id" action="index.php" method="POST">
    <input type="submit" value="Dog">
  </form>
  <div id="answer"></div>
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script>
            $(document).ready(function(){
                $('#my_form_id').on('submit', function(e){
                    //Stop the form from submitting itself to the server.
                    e.preventDefault();
                    var dog = $('#dog');
                    $.ajax({
                        type: "POST",
                        url: 'index.php',
                        data: {postdog:dog},
                        success: function(data){
                           // alert(data);
                            $( "#answer" ).append(data);
                        }
                    });
                });
            });
        </script>
</body>
</html>

我的PHP

<?php
$dogs = array("Corgi", "Husky", "Samoyed");

if(isset($_POST['postdog'])){
foreach ($dogs as $dog) {
    echo "<img src='images/dogs/$dog.jpg'> <br>";
}
};

$cats = array("Scottish_Fold", "Persian", "Himalayan");
foreach ($cats as $cat) {
    echo "<img src='images/dogs/$cat.jpg'> <br>";
}

?>
php html ajax
2个回答
0
投票
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Homework 12</title>
</head>
<body>
  <h1>Pictures of Pets</h1>
  <a href="javaScript:void(0);" class="btn" onclick="get_result()">SUBMIT</a>
  <div id="answer"></div>
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script>
            function get_result()
                    $.ajax({
                        type: "POST",
                        url: 'upload.php',
                        data: {action: 'upload'},
                        success: function(data){
                           // alert(data);
                            $( "#answer" ).append(data);
                        }
                    });
            }
        </script>
</body>
</html>

upload.php

<?php

if(isset($_POST['action']) && $_POST['action'] == 'upload' ){

$dogs = array("Corgi", "Husky", "Samoyed");


foreach ($dogs as $dog) {
    echo "<img src='images/dogs/$dog.jpg'> <br>";
};

$cats = array("Scottish_Fold", "Persian", "Himalayan");
foreach ($cats as $cat) {
    echo "<img src='images/dogs/$cat.jpg'> <br>";
}

}
?>

0
投票

HTML:

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Homework 12</title>
</head>
<body>
  <h1>Pictures of Pets</h1>
  <form enctype="multipart/form-data" action="index.php" method="post" id="my_form_id">
        <input name="file[]" type="file" />
        <button class="pets">Add More</button>
        <input type="button" value="Image upload" id="upload"/>
</form>
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</body>
</html>

脚本:

          $(document).ready(function(){
            $('.pets').click(function(e){
                e.preventDefault();
                $(this).before("<input name='file[]' type='file'/>");
            });
          });
           $('body').on('click', '#upload', function(e){
                  e.preventDefault();
                  var formData = new FormData($(this).parents('form')[0]);

                  $.ajax({
                      url: 'index.php',
                      type: 'POST',
                      xhr: function() {
                          var myXhr = $.ajaxSettings.xhr();
                          return myXhr;
                      },
                      success: function (data) {
                          alert("Data Uploaded: "+data);
                      },
                      data: formData,
                      cache: false,
                      contentType: false,
                      processData: false
                  });
                  return false;
          });

index.php

for($i=0; $i<count($_FILES['file']['name']); $i++){
    $target_path = "pets/";
    $ext = explode('.', basename( $_FILES['file']['name'][$i]));
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1]; 

    if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
        echo "Your pets images has been uploaded successfully <br>";
    } else{
        echo "Error, please try again! <br />";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.