Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a form that insert some data to a db with an image file upload
saving image url to db after i resize it ad save the image to my folder
here is the code
HTML
<form id="form_insert_edit" action="insert.php" method="post">
    <table dir="rtl" cellpadding="5" cellspacing="5" width="500px">
        <tr>
            <th valign='top' align='right'>name</th>
            <td><input type='text' name='name' id="name" /></td>
        </tr>
        <tr>
            <th valign='top' align='right'>job description</th>
            <td>
                <select name='pName' id="pName">
                <option value='0'></option>
                ...etc
                </select>
            </td>
        </tr>
        <tr>
            <th valign='top' align='right'>القسم</th>
            <td>
                <!--Fill the Departments-->                
                <select name='sectionNames' id="sectionNames">
                    <option value='0'>Fill Data From DB</option>
                </select>
            </td>
        </tr>
        <tr>
            <th valign='top' align='right'>ext no</th>
            <td align='right'><input type='text' name='pNo' id="pNo" /></td>
        </tr>
        <tr>
            <th align='right'>image</th>
            <td><input type='file' name='file' id="file" /></td>
        </tr>			 		 
        <tr>
            <td></td>
            <td>
                <input type='submit' id="ADD" name='ADD'  value='save' />    
                <input type='button' id="Cancel" name='Cancel'  value='cancel' />
            </td>
        </tr>
        <tr>
            <td>
                <span id="bottom"></span>
                <div id="divdate">
                	<?php 
					if(isset($_SESSION['user_name'])){
						echo $_SESSION['user_name'].' inserted successfully ';
					}
					?>
                </div>
                
            </td>
            <td></td>
        </tr>
    </table>
</form>


and my js code is

JavaScript
$(document).ready( function() {		
	$('#Editpopup').click( function(){				
		loadPopupBox();
		$("#name").focus();
	});
	function loadPopupBox() {    // To Load the Popupbox
		$('#popup').fadeIn("slow");
		$("#container").css({ // this is just for style
			"opacity": "0.3"  
		});         
	}
	
	$('#popupBoxClose').click( function() {            
		unloadPopupBox();
	});	
	$("#Cancel").click(function(){
		unloadPopupBox();
	});	
	function unloadPopupBox() {	// TO Unload the Popupbox
		$('#popup').fadeOut("slow");
		$("#container").css({ // this is just for style		
			"opacity": "1"  
		}); 
	}		
	
	/* Insert part */
	$("#ADD").click(function(){
		name=$("#name").val();
		pName=$("#pName").val();
		section=$("#sectionName").val();
		ext=$("#pNo").val();
                img=$("#file").val();
		$.ajax({
			type: "POST",
			url: "insert.php",
			data: "name="+name+"&pName="+pName+"§ion="+section+"&ext="+ext+"&img="+img,
			success: function(html){
				if(html=='true'){
					$("#divdate").html("Data Inserted Successfully");
				}else{
					$("#divdate").html("something wrong, please try later");
				}
			},
			beforeSend:function(){
				$("#divdate").html("Loading...")
			}
		});
		return false;
	});
});

i need also to upload the image i could not do it like this

help needed not all data posted to insert.php correctly idk why
Posted

1 solution

You cannot post your image to the server like that. Javascript won't allow you to.
Take a look at some existing components that upload files asynchonously, it's complicated because of client side permissions & stuff. I like dropzone.js[^] for upload files using javascript.
 
Share this answer
 
Comments
islamco87 3-Oct-13 6:28am    
to upload an image as i understand i need to use formdata and send it in the data: formdata_var
but i am trying to send more than just an image i have text's also
i need to merg all of them

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900