Jump to content

Recommended Posts

hi!

i'm new to the ajax world and i was trying to make a upload script,

i made the js script that work fine, the php scrip that work fine, but my file wasn't been uploaded..

 

i did a var dumb to see the file that were been read by the $_post and the $_files what i could see from the result

is that everything is working fine, but not the file.

 

my question is how come everything in the form work but not the file?

Link to comment
https://forums.phpfreaks.com/topic/82843-solved-php-ajax-trying/
Share on other sites

ok let me post all the code .......

 

the form

	<form action="" method="post" enctype="multipart/form-data" title="add a party" onsubmit=" var s= doSubmit('test');return s;">	
		<fieldset>
		<h2>Add your party date.</h2>
		<p>
			<label title="date of your party">Date the date of your party :</label>
			<select name="month"  id="month">
				<option value="00">Month</option>
				<option value="January">01</option>
				<option value="February">02</option>
				<option value="March">03</option>
				<option value="April">04</option>
				<option value="May">05</option>
				<option value="June">06</option>
				<option value="July">07</option>
				<option value="Agust">08</option>
				<option value="September">09</option>
				<option value="Octuber">10</option>
				<option value="November">11</option>
				<option value="December">12</option>
			</select>
			<select name="day" id="day">
			<option value="00">Day</option>
				<?php
				 for($i=1;$i <=31; $i++){
				 	echo "<option value=\"$i\">$i</option>";
				 }
				?>
			</select>
			<select name="year" id="year">
			<option value="00">Year</option>
				<?php
				 for($i=date("Y");$i < date("Y")+5; $i++){
				 	echo "<option value=\"$i\">$i</option>";
				 }
				?>
			</select>
			</p>
			<p>
				<label for="imgfile">Flyer :</label>
				<input type="file" name="imgfile" id="imgfile" title="JPG image"/>
				<br />
			</p>
			<p>
				<label for="desc">Description :</label>
				<input name="desc" type="text" id="desc"  title="40 letter description" size="40" maxlength="40"/>
			</p>
			<p>
				<button type="submit"  id="Upload" value="Upload">add this Party</button> 
			</p>

		</fieldset>

		<fieldset>
			<h2>Keep in mind or you will get errors</h2>
				<ul>
					<li>Don't use a date that has already passed!</li> 
					<li>Flyers has to be a JPG image</li>
					<li>The description has ot be less than 40 letters not 40 words</li>
				</ul>
		</fieldset>
		<fieldset>
			<h2>testing for results</h2>
				<div id="test"></div>
		</fieldset>

 </form>    

the JavaScript

function doSubmit(div){
var year =  "year=" + document.getElementById("year").value; 
var day = "day=" + document.getElementById("day").value;
var month = "month=" + document.getElementById("month").value;
var desc =  "desc=" + document.getElementById("desc").value;
var time = new Date();
var param =  month+"&"+day+"&"+year+"&"+desc+"&"+"time="+time.getTime();

doAjax(div,'../../test2.php?',param);
return false;
}

function doAjax(div,file,value){
var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

if(request){

	request.open("POST",file,true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", value.length);
	request.setRequestHeader("Connection", "close");

	request.onreadystatechange = function(){
		if(request.readyState == 4){
		 document.getElementById(div).innerHTML = request.responseText;   
		}
	}
	request.send(value);
}
}

correct according to php.net uploading form format.

 

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

 

php.net upload link

http://uk3.php.net/features.file-upload

does it matter the directory or the max file size of the values re not been read by the script?

 

i have done many upload form so I'm sure of what i am doing.

i never done it with Ajax so i am not sure why is the file properties are not been pass like the other properties?

thats is my only question!!

 

i been searching some other websites and found out the javascript or Ajax cannot handle file properties, but they are old documentations.

 

is iframe the only why to upload files with out refreshing the current page?

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.