Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
can someone please help me with this code, Im trying to change my file upload scheme to use JavaScript so that my page doesnt get submitted ever time user uploads a file, THANK YOU IN ADVANCE; belove ismy code

<script type="text/javascript">

    function fileUplodBtn(fileID) {
    function fileUploder(fileID) {

        //var formData = new FormData($("#myForm").[0]);

        //$.post('AddFormLogic.php', formData, function(response)
        var fileInput =  document.getElementById("uploadrecipephoto");
        $.post("upload.php",{uploadrecipephoto: 'uploadrecipephoto'}, function(data) {


        alert('got it');

    <div id="response"></div>
<input type="file" id="uploadrecipephoto" value="uploadfile" name="file" style="visibility: hidden" />

<tr  >
    <th align="left" >
        <input type="submit" class="btnLogin" name='btnCreateRecipe' value="Create Recipe" />
    <th class="file-upload" align="right">
        <div id='file_browse_wrapper' type="submit" onclick="return false" onmousedown="javascript:fileUplodBtn('uploadrecipephoto');"          >

            <input type="hidden" name="uploadrecipephoto" value="recipephoto">

and on my PHP side I have the catcher

if (isset($_FILES['file']['name'])){
    $fileloc= $_SESSION['temprecimg'];
    echo $fileloc;
    echo '--------------'.$_SESSION['ufolder'];
    echo '+++++++++'.$_FILES['file']['type'];
    echo '<script type="text/javascript">   alert("22222222");  </script>';

I have been trying to figure this out for 3 days now! I know it seems dumb but i just cant figure outhow to post the File info to the php

This is not going to work. I do not think you understand what the code does. You will need to acquire knowledge of the following areas:
1. Learn HTML[^]
2. Learn JavaScript[^]
3. Learn AJAX[^]
4. Learn jQuery[^]
5. Learn PHP[^]
You should learn these with patience and try out their examples. Remember that "Rome was not built in one day".
However, to relieve you of your immediate anxiety, you may check this out: ajax-file-upload-jquery[^],
Good luck.
[This part is added in respond to your follow-up comment]
Yes, the complete code presented at the end of the link is working, no question about it. The problem lies in you do not know how to adapt it. You cannot just copy and paste some code from somewhere and expect it to work out of the box. You will have to modify and adapt it. And the pre-condition for this is that you must have knowledge of the code. That bring you back to the first part of my solution.
To help you further, if you run the code from the link without changes, then when you upload a file it will complain of "no such file or directory", the remedy is: in the "upload.php" file -
1. remove the
$output_dir = "uploads/";

2. replace
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);


That should do. Lastly, "luck" is elusive but "Learning" stays.
Share this answer
vanakhas 4-Jan-14 14:59pm    
thanks for the quick reply and yes I think you are right!the code you have posted the link for doe not work on my end. so I realized that the only way I can go around to doing it is by submiting the forms
like this

but the problem I get with this is that itdoesnt actually submit, I DO NOT WANT TO REFRESH THE PAGE!!!!
Peter Leow 4-Jan-14 21:29pm    
see my last and final reply in solution 1.
vanakhas 5-Jan-14 12:24pm    
thanks for the reply, but the code is not submitting the "myForm" to upload.php. it seems that the "if(isset($_FILES["myfile"]))" does not catch. I just cant figure that part out
vanakhas 5-Jan-14 12:26pm    
traditionaly I use Jquery and javascript topost values and recipe back fromthe php back end but I have trouble posting/submiting forms
hey guys im sorry for wasting everyones time, i just found out that my server was not working correctly
Share this answer

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