SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Upload using jquery and ajax

    Hi everyone, i create a simple php script to upload images it is working nice, but now i need to do it using jquery and ajax, because as i am using only php, when i send an image it redirect the page to a white page.

    well this is my simple comment system with jquery and ajax, it is simple and i tried to create some like this to image uploads but i cant use this method to image uploads,here is the code of the simple comment system, if anyone can help me with the images will be great...


    HTML Code
    PHP Code:
    <form id ="Forms" action="javascript:comments()" method="post">
                <
    textarea id="comment" 
    placeholder="Say something"></textarea>
                <
    input type="submit">
            </
    form
    JavascriptCode
    PHP Code:
    function comments() {
                
        var 
    comment = $('#comment').val();
        
        $.
    ajax({         
            
    asyncfalse,
            
    url:        'comment.php',
            
    type:       'POST',
            
    dataType:   'json',
            
    data: {
                
    comment:             $('#comment').val()
            },

            
    success:function(data) {

                $(
    "ul.#InsertThings").prepend(data);
            
            }
      
        });


    Comment.php
    PHP Code:
        $Comment = ($_POST['comment']);
     
         echo 
    json_encode(...some divs etc and $Comment inside of a div ); 
    Well it is working great, but with photos i cant use this method, i really need some help with that because i never create anything similar and really need to do it.
    I searched a lot to find some helps to my problem but cant get the best code for my problem.

    Thanks community

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Alex12345 View Post
    Hi everyone, i create a simple php script to upload images it is working nice, but now i need to do it using jquery and ajax, because as i am using only php, when i send an image it redirect the page to a white page.
    If you don't want it to redirect to white page, there's no need to use Ajax, just change your code so it shows what you want it to show.
    Well it is working great, but with photos i cant use this method
    Why not? What is the problem. Are you getting an error?

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is not possibel to send images using this method with jquery and the ajax function, it dont work. You know any method to post images using ajax?i dont want to use plugins

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Alex12345 View Post
    is not possibel to send images using this method with jquery and the ajax function, it dont work.
    What does "dont work" mean? Are you getting an error?
    Is it giving problems on the client side (JS) or server side (PHP)? If your problem is with the jQuery, then we'll move this thread to the JS forum.

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What does "dont work" mean? Are you getting an error?
    Is it giving problems on the client side (JS) or server side (PHP)? If your problem is with the jQuery, then we'll move this thread to the JS forum.
    The problem is that is not possibel to pass the image specifications by the jquery with the ajax function, to upload an image you use:

    PHP Code:
     $_FILES["file"]["name"
     
    $_FILES["file"]["type"
     
    $_FILES["file"]["size"]
     
    $_FILES["file"]["tmp_name"]; 
    and you cant pass this thing by the jquery with this ajax function.

    The problem is not only javascript, it is php,ajax and js

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Thread moved to the JS forum.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,044
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The simplest method is to post to a hidden iframe. Than have the page inside the iframe notify the parent window when a response is returned. Looking up uploading images with iframe without page refresh should yield some results on Google.
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •