Jump to content

Submitting form data to an iframe without refreshing


Recommended Posts

Hi,

 

Im trying to create my own 'AJAX' image uploader which posts form data in a hidden iframe on the same page however I cannot get it to work without the page refreshing. The code is simple I hope I have made just a stupid error.

 

HTML File


<form id="upload_img_form" method="post" action="" onsubmit="uploadStart();" enctype="multipart/form-data">
	<input type="text" name="imgname" id="imgname" value="" />
	<input type="submit" name="imgbutton" id="imgbutton" value="Upload" />
	<iframe id="upload_iframe" name="upload_iframe"></iframe>	
</form>

 

JS File


function uploadStart ()
{
document.getElementById('img_form').target = 'upload_iframe';
document.getElementById('upload_iframe').src = 'upload.php';

$('#upload_iframe').load(function ()
{
	uploadComplete();
});

return false;
}

function uploadComplete ()
{
var responseMessage = $('#upload_iframe').contents().text();
alert(responseMessage);
}

 

PHP File


<?php

echo $_POST['imgname'];

?>

 

For now all I need is for the script to alert out the name of the text input without the page refreshing. From there I reckon I can sort the rest.

 

If anyone could help me out here, itll be greatly appreciated.

 

Thanks in advance

 

You can try something like this...

 

HTML:

<html>
<head>
<title>...</title>
<body>
<a href="whatever.php?alert=myAlert" target="myIframe">Post Alert!</a>
<iframe src="whatever.php" name="myIframe" style="do your styling"></iframe>
</body>
</html>

 

IFRAME:

<html>
<head>
<title>...</title>
</head>
<?php
$alert = $_GET['alert'];

echo '<body onload="alert(\''.$alert.'\');">';
?>
</body>
</html>

 

You can obviously elaborate on this method, create functions

Check this video out. Came across it just today whilst trying to learn some AJAX. Turns out this isn't using ajax, however it is using iFrames, as you're requesting.

 

http://www.youtube.com/watch?v=TiV5vCar_cI&feature=mfu_in_order&list=UL

 

Denno

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.