Jump to content

Using $.ajax to post data and upload picture using wireupload not working


ebencosoft
 Share

Recommended Posts

Hi,

I want to form and i want client to insert the details and picture.The data get insert but the picture is not uploaded.

Code Below

-------------------------------------------------------JS-------------------------------------------------------------------------------------


$(".next").click(function(e){

    //e.preventDefault();
var name = $('#name').val();
var work = $('#work').val();
var location = $('#location').val();
var phone = $('#phone').val();
var foto = $('input[name=file]')[0].files[0]

var datastring = 'name='+name+'&work='+work+'&location='+location+'&phone='+phone+'&foto='+foto;

$.ajax({
 url: '/wan/register/',
 type: 'POST',
 data: datastring,
 dataType:'json',
          success: function(response, textStatus, jqXHR)
{
//data - response from server
alert('Sucess '+response)
},
error: function (jqXHR, textStatus, errorThrown)
{
     alert('Fail')
            }  
      }); 
 }); 
 
---------------------------------------------Server Side Code PHP -------------------------------------------------------------
 
 
$upload_path = $config->paths->assets . 'files/temp/';
    // new wire upload
    $u = new WireUpload('file');
    $u->setMaxFiles(1);
    $u->setMaxFileSize(1*1024*1024);
    $u->setOverwrite(false);
    $u->setDestinationPath($upload_path);
    $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));

    // execute upload and check for errors
    $files = $u->execute();

    if(!$u->getErrors()){

        // create the new page to add the images
$parent = $pages->get('/register/');
        $uploadpag = new Page();            

        $uploadpag->template = 'register'; // required
        $uploadpag->parent = $parent; // required 
$uploadpage->title = date("d-m-Y H:i:s") . " - " . uniqid() . ($input->post->name);

        $uploadpag->of(false);
$uploadpag->Fullname = $sanitizer->text($input->post->name); 
$uploadpag->Occupation = $sanitizer->text($input->post->work); 
$uploadpag->Location = $sanitizer->text($input->post->location); 
$uploadpag->MobilePhone = $sanitizer->text($input->post->phone); 

$uploadpag->addStatus(Page::statusUnpublished);
        $uploadpag->save();

// add images upload
         foreach($files as $filename){
            $uploadpag->foto = $upload_path . $filename;
        }
        // save page
        $uploadpag->save();

       // remove all tmp files uploaded
        foreach($files as $filename) unlink($upload_path . $filename);
               echo $filename; 
     } else {
        // remove files
        foreach($files as $filename) unlink($upload_path . $filename);
        // get the errors
        foreach($u->getErrors() as $error) echo "<p class='error'>$error</p>";
    } 
 
--------------------------------------------------------------------HTML----------------------------------------------------------
 
<form id="msform" method="post" action="" enctype="multipart/form-data">

<input type="text" name="name" id="name"  placeholder="Name / Company Name" />
<input type="text" name="work" id="work" placeholder="Occupation / Work" />
<input type="text" name="phone" id="phone" placeholder="Mobile Phone" />
<input type="text" name="location" id="location" placeholder="Location" />
<input type="file"  name="file" id="file" placeholder="Profile picture"  />
<input type="button" name="next" id="next" class="next action-button" value="Register" />

</form>
Edited by LostKobrakai
added codeblocks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...