1

Possible Duplicate:
putting html inside an iframe (using javascript)

I'm using the following code and what I want to do essentially (I'm new to jquery) is submit this form, but then have what is normally outputted on the uploadpic.php page - appear on this page where the form is, in an iframe. I just can't work out how to do it. I added the Jquery Form Plugin - http://www.malsup.com/jquery/form/ So I've gone through the code and commented what it does now. But I've no idea how to add that iframe exchange. Basically the uploadpic.php does this:

$add_one = $membership->add_photo($_POST['photo'], $_POST['caption']);

And if it is all submitted successfully, or if it is a failure, it echos either "sorry your file wasn't uploaded" or "file was uploaded successfully.

How would I stop from having to go to a new page, or resorting to an irritating popup? I thought a temporary iframe would be a good idea - but simply no idea how to implement such a thing.

<div id="uploadform">
            <form id = "uploadpicform" enctype="multipart/form-data" action="uploadpic.php" method="POST"> 
            <p>Photo: </p><input type="file" name="photo"><br />
            <p>Caption:</p> My <input type="text" name="caption"><br />
                   <input type="submit" class="large blue button" value="Add"> 
            </form>
            <script>
            // wait for the DOM to be loaded
            $(document).ready(function() {
                // bind 'myForm' and provide a simple callback function
                $('#uploadpicform').ajaxForm(function() {
                $("#hideuploadbutton").hide();
                $("#uploadbutton").show();
                $("#uploadform").hide();
                });
            });             
            </script>
        </div>

What that does at the moment, is submits the form, with no notice, hides the upload button and shows an open upload button to start the process again. I figure there must be some sort of 'add iframe' or 'print php variable from the other page' option or something.

I'd appreciate any help, thanks a bunch!

Community
  • 1
  • 1
Deevee
  • 11
  • 1
  • Can I see your ajax request? Html? – VIDesignz Nov 03 '12 at 02:30
  • @Barmar - not really. The central concern is that the php information needs to appear in the temporary iframe. It comes from the opened uploadpic.php - which on the condition of a successfully uploaded file reads out a particular message, or unsuccessful - reads out the error. The thing you linked to does no such thing. – Deevee Nov 03 '12 at 03:35
  • The question I linked to tells you what you need to put in your success function so it can put the returned data in the iframe. The data is the parameter to the success function. – Barmar Nov 03 '12 at 03:38
  • @Barmar - Sorry, bearing in mind that I'm relatively new to JQuery, could you elaborate a bit? I'm a bit confused by this and how it relates in to my function as above. It seems to have "$body.html('

    Test

    ');" But then, I don't want to define it here, I want it defined in the php doc and have it link back. Sorry if I sound a bit dumb having said that. I just don't understand really. Do I need to define the iframe?
    – Deevee Nov 03 '12 at 04:03
  • Add an argument `data` to the callback function in your `.ajaxForm` call. Then use `$body.html(data)`. – Barmar Nov 03 '12 at 04:06
  • So you really want an iframe just to show the success/fail result? Will this iframe be embedded in the main page or as a pop up? If its embedded, whats the point to do all the extra code needed just for the result. A hidden div seems the way to go. – VIDesignz Nov 03 '12 at 04:20

1 Answers1

0

EDITED Added code for populating iframe

HTML

<form id="uploadpicform">

    <p>Photo:</p>
    <input type="file" name="photo"><br />
    <p>Caption:</p>
    <input type="text" name="caption"><br />

</form>


<div id="uploadpicbutton" class="bluebutton">Upload</div>

JQuery

// **ADDED** Populate the iframe 
function iframeresults() {  

    var content = phpresults;
    var tFrame = document.getElementById("iframeid");
    var doc = tFrame.contentDocument;
    if (doc == undefined || doc == null)
        doc = tFrame.contentWindow.document;
    doc.open();
    doc.write(content);
    doc.close();

}

//Submit Upload Ajax Call Function

function submit_upload() {

    $('#status').html('Uploading, Please Wait').fadeIn(500);

    $.ajax({
        type: "POST",
        url: "/uploadpic.php",
        cache: false,
        data: $('#uploadpicform').serialize(), //This adds the variable name and input values automatically
        dataType: "script", // Returns Javascript outputted from PHP page and Launches the Script
        success: function(){

        iframeresults();

    });        

}

// Bind the upload function to the button    
$('#uploadpicbutton').on('click', submit_upload); 

PHP

 //Use this echo in your PHP after your PHP Successful validation
 echo "var phpresults = 'Upload Succesful'; ";

 //Use this echo in your PHP after your PHP Failure validation
 echo "var phpresults = 'Upload Failed'; ";     
VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • "I thought a temporary iframe would be a good idea" This statement led me to believe he wasn't set on an iframe...my bad – VIDesignz Nov 03 '12 at 04:22
  • A hidden div works just as well actually. As a quick question, is there a way to get it to work using: Within the form, rather than the separate div that you've used? Or even input type = "button" – Deevee Nov 03 '12 at 17:21
  • Surely! Just add id='uploadpicbutton' to the that you want to use. Like **< input id='uploadpicbutton' type='button' class="large blue button" value="Add">** – VIDesignz Nov 03 '12 at 19:28
  • If you like the answer, make sure to accept it! :) :) – VIDesignz Nov 03 '12 at 19:32