The ajax file cannot pass the file field value to the php file -


i have problem ajax file. ajax file not work php form. when run code displays error file_get_content error, file name empty.

the php code working properly. ajax file cannot transfer file field value. file cannot attach mail.

it shows error: file name empty

plz me pass file field value through ajax file.

<div> <form  method="post" enctype="multipart/form-data" name="form1" id="form1"     action="contactus.php" onsubmit="xmlhttppost('contactus.php', 'form1', 'myresult', ''); return false;"> name:<input name="name1" type="text"   value="" /> address:<input name="address1" type="text"  value="" />  phone:<input name="phone1" type="text" value=""/> email: <input name="email1" type="text" value="" /> file:<input name="file" type="file" size="35" id="file" /> links:<input name="links1" type="text" value="" /> subject:<input name="subject1" type="text" value="" /> location:<select name="location">               <option value="" selected="selected">--select--</option>               <option>1</option>               <option>2</option>               <option>3</option>               <option>4</option>               <option>5</option>             </select>  comments:<textarea name="comment1"  ></textarea> <input name="submit" id="submit" type="submit"/>    <div id="myresult"></div> </form> </div> 

php form(contactus.php)

<?php if(isset($_post['submit'])) {   $name=$_post['name1']; $address=$_post['address1']; $phone=$_post['phone1']; $email=$_post['email1']; $subject=$_post['subject1']; $location=$_post['location']; $comment=$_post['comment1']; $links=$_post['links1'];  $to='mail@mail.com';   $message .= "\nname: ".$name."\n\n";  $message .= "address: ".$address."\n\n";  $message .= "phone: ".$phone."\n\n";  $message .= "email: ".$email."\n\n";   $message .= "links: ".$links."\n\n";   $message .= "location: ".$location."\n\n";   $message .= "comments:\n\n ".$comment."\n";    $attachment = chunk_split(base64_encode(file_get_contents($_files['file']['tmp_name'])));     $filename = $_files['file']['name'];     $filetype = $_files['file']['type'];             $boundary =md5(date('r', time()));   $headers = "from: $name <$email>\r\nreply-to: $name <$email>"; $headers .= "\r\nmime-version: 1.0\r\ncontent-type: multipart/mixed; boundary=\"_1_$boundary\"";   $message="this multi-part message in mime format.    --_1_$boundary  content-type: multipart/alternative; boundary=\"_2_$boundary\"   --_2_$boundary  content-type: text/plain; charset=\"iso-8859-1\"  content-transfer-encoding: 7bit   $message   --_2_$boundary--  --_1_$boundary  content-type: $filetype; name=\"$filename\"   content-transfer-encoding: base64   content-disposition: attachment    $attachment   --_1_$boundary--";   mail($to,$subject,$message,$headers);   print 'thanks, message sent!';   }   ?> 

ajax.js

function xmlhttppost(strurl, formname, responsediv, responsemsg) {     var xmlhttpreq = false;     var self = this;     // xhr per mozilla/safari/ie7     if (window.xmlhttprequest) {         self.xmlhttpreq = new xmlhttprequest();     }     // per tutte le altre versioni di ie     else if (window.activexobject) {         self.xmlhttpreq = new activexobject("microsoft.xmlhttp");     }     self.xmlhttpreq.open('post', strurl, true);     self.xmlhttpreq.setrequestheader('content-type', 'application/x-www-form-urlencoded');     self.xmlhttpreq.onreadystatechange = function () {         if (self.xmlhttpreq.readystate == 4) {             // quando pronta, visualizzo la risposta del form             updatepage(self.xmlhttpreq.responsetext, responsediv);         } else {             // in attesa della risposta del form visualizzo il msg di attesa             updatepage(responsemsg, responsediv);         }     }     self.xmlhttpreq.send(getquerystring(formname)); }  function getquerystring(formname) {     var form = document.forms[formname];     var qstr = "";      function getelemvalue(name, value) {         qstr += (qstr.length > 0 ? "&" : "") + escape(name).replace(/\+/g, "%2b") + "=" + escape(value ? value : "").replace(/\+/g, "%2b");         //+ escape(value ? value : "").replace(/\n/g, "%0d");     }      var elemarray = form.elements;     (var = 0; < elemarray.length; i++) {         var element = elemarray[i];         var elemtype = element.type.touppercase();         var elemname = element.name;         if (elemname) {             if (elemtype == "text" || elemtype == "textarea" || elemtype == "password" || elemtype == "button" || elemtype == "reset" || elemtype == "submit" || elemtype == "file" || elemtype == "image" || elemtype == "hidden")              getelemvalue(elemname, element.value);             else if (elemtype == "checkbox" && element.checked) getelemvalue(elemname,             element.value ? element.value : "on");             else if (elemtype == "radio" && element.checked) getelemvalue(elemname, element.value);             else if (elemtype.indexof("select") != -1)              (var j = 0; j < element.options.length; j++) {                 var option = element.options[j];                 if (option.selected) getelemvalue(elemname,                 option.value ? option.value : option.text);             }         }     }     return qstr; }  function updatepage(str, responsediv) {     document.getelementbyid(responsediv).innerhtml = str; } 

cannot reproduce error. try this:

<?php if ($_server['request_method'] === 'post') {     if (isset($_post['submit1'])) {          $name     = $_post['name1'];         $address  = $_post['address1'];         $phone    = $_post['phone1'];         $email    = $_post['email1'];         $subject  = $_post['subject1'];         $location = $_post['location'];         $comment  = $_post['comment1'];         $links    = $_post['links1'];          $to = 'mail@mail.com';          $message .= "\nname: " . $name . "\n\n";         $message .= "address: " . $address . "\n\n";         $message .= "phone: " . $phone . "\n\n";         $message .= "email: " . $email . "\n\n";         $message .= "links: " . $links . "\n\n";         $message .= "location: " . $location . "\n\n";         $message .= "comments:\n\n " . $comment . "\n";           $attachment = chunk_split(base64_encode(file_get_contents($_files['file']['tmp_name'])));         $filename   = $_files['file']['name'];         $filetype   = $_files['file']['type'];         $boundary   = md5(date('r', time()));          $headers = "from: $name <$email>\r\nreply-to: $name <$email>";         $headers .= "\r\nmime-version: 1.0\r\ncontent-type: multipart/mixed; boundary=\"_1_$boundary\"";          $message = "this multi-part message in mime format.            --_1_$boundary          content-type: multipart/alternative; boundary=\"_2_$boundary\"           --_2_$boundary          content-type: text/plain; charset=\"iso-8859-1\"          content-transfer-encoding: 7bit           $message           --_2_$boundary--          --_1_$boundary          content-type: $filetype; name=\"$filename\"           content-transfer-encoding: base64           content-disposition: attachment            $attachment           --_1_$boundary--";          mail($to, $subject, $message, $headers);          print 'thanks, message sent!';         exit;     }      print 'some error';     exit; } ?> <!doctype html> <html>     <head>         <script>         function xmlhttppost(strurl, formname, responsediv, responsemsg) {             var xmlhttpreq = false;             var self = this;             if (window.xmlhttprequest) {                 self.xmlhttpreq = new xmlhttprequest()             } else if (window.activexobject) {                 self.xmlhttpreq = new activexobject("microsoft.xmlhttp")             }             self.xmlhttpreq.open('post', strurl, true);             self.xmlhttpreq.setrequestheader('content-type', 'application/x-www-form-urlencoded');             self.xmlhttpreq.onreadystatechange = function () {                 if (self.xmlhttpreq.readystate == 4) {                     updatepage(self.xmlhttpreq.responsetext, responsediv)                 } else {                     updatepage(responsemsg, responsediv)                 }             }             self.xmlhttpreq.send(getquerystring(formname))         }         function getquerystring(formname) {             var form = document.forms[formname];             var qstr = "";              function getelemvalue(name, value) {                 qstr += (qstr.length > 0 ? "&" : "") + escape(name).replace(/\+/g, "%2b") + "=" + escape(value ? value : "").replace(/\+/g, "%2b")             }             var elemarray = form.elements;             (var = 0; < elemarray.length; i++) {                 var element = elemarray[i];                 var elemtype = element.type.touppercase();                 var elemname = element.name;                 if (elemname) {                     if (elemtype == "text" || elemtype == "textarea" || elemtype == "password" || elemtype == "button" || elemtype == "reset" || elemtype == "submit" || elemtype == "file" || elemtype == "image" || elemtype == "hidden") getelemvalue(elemname, element.value);                     else if (elemtype == "checkbox" && element.checked) getelemvalue(elemname, element.value ? element.value : "on");                     else if (elemtype == "radio" && element.checked) getelemvalue(elemname, element.value);                     else if (elemtype.indexof("select") != -1) (var j = 0; j < element.options.length; j++) {                             var option = element.options[j];                             if (option.selected) getelemvalue(elemname, option.value ? option.value : option.text)                     }                 }             }             return qstr         }         function updatepage(str, responsediv) {             document.getelementbyid(responsediv).innerhtml = str         }         </script>     </head>     <body>         <div>             <form  method="post" enctype="multipart/form-data" name="form1" id="form1" onsubmit="xmlhttppost('', 'form1', 'myresult', ''); return false;">                 <input type="submit" name="submit1" value="submit">                 <div id="myresult"></div>             </form>         </div>     </body> </html> 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -