dongshao5573 2015-03-17 21:53
浏览 42

使用表单从Jquery Mobile Phonegap应用程序附加图像?

In my app (JQM + Phonegap) I have created a form where the user can take an image using the camera and want to submit the form via php email with image attached. I am getting the form fields, but no the image.

I had found this example of a file upload and tried to use some of the php.

<form action="....onmyserver..../bcmc-wnv-mailer.php" method="post" data-ajax="false" id="reportBirdForm" enctype="multipart/form-data">
        <label for="textinput" class="ui-hidden-accessible">Name:</label>
        <input type="text" name="f2_fullName" id="f2_fullName" value="" placeholder="Full Name *" required />

        <label for="f2_phone" class="ui-hidden-accessible">Phone:</label>
        <input type="tel" name="f2_phone" id="f2_phone" value="" placeholder="Phone #" required/>
        <label for="f2_email" class="ui-hidden-accessible">Phone:</label>
        <input type="email" name="f2_email" id="f2_email" value="" placeholder="Email Address *" required />
        <a href="#mapPage" onClick="locFormFieldName='deadBirdLocation';" class=" ui-btn ui-btn-icon-right ui-icon-location ui-corner-all ui-nodisc-icon" data-transition="pop">Click to find Location Address...</a>
        <label for="f2_locationAddress" class="ui-hidden-accessible">Location Address:</label>
        <input type="text" name="f2_locationAddress" id="f2_locationAddress" value="" placeholder="Location Address *" />
        <input type="hidden" name="f2_GPSLocation" id="f2_GPSLocation" value="" />
        <select name="f2_numBirds" id="f2_numBirds" data-native-menu="false" data-theme="c" required>
            <option>Number of Birds?</option>
            <option value="1">One(1)</option>
            <option value="2">Two(2)</option>
            <option value="3">Three(3)</option>
            <option value="4+">Four+(4+)</option>
        </select>
        <label for="f2_additionalDetails" class="ui-hidden-accessible">Description:</label>
        <textarea name="f2_additionalDetails" id="f2_additionalDetails" placeholder="Additional Details" style="height:80px !important;" ></textarea>

        <a href="" onClick="capturePhoto();" class="ui-btn ui-btn-icon-right ui-icon-camera ui-nodisc-icon ui-corner-all" id="photoBtn" >Attach Photo</a>
        <input type="hidden" name="f2_photoURI" id="f2_photoURI" value="" placeholder="Photo URI"/>

        <div style="border:thin rgba(172,172,172,1.00) solid; padding:0.5em; border-radius:0.9em;"><center><img src="img/imgPlaceholder.jpg" id="photoPreview" style="width:50%;"/></center></div>

        <button class="ui-btn ui-corner-all" type="submit" id="submit">Submit Form</button>

    </form>

Javascript

function capturePhoto(){
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
    destinationType: Camera.DestinationType.FILE_URI, correctOrientation:true});
} // end function capturePhoto()

function onSuccess(imageURI) {
var image = document.getElementById('photoPreview');
image.src = imageURI;
//$('#photoPreview').attr("src",imageURI);
var formElem = document.getElementById("photoURI");
formElem.value = imageURI;
}

function onFail(errorMessage) {
showAlert('Photo Attchment Failed: ' + errorMessage,'Error!');
}

Here is my PHP.

<?php
// VARS

$BCMC_email="me@myemail.com";

$email_subject="Bird Report - BCMC Mobile App";


$FullName=$_POST["f2_fullName"];
$FromEmail=$_POST["f2_email"]; 
$Phone=$_POST["f2_phone"];
$LocationAddress=$_POST["f2_locationAddress"];
$GPSLocation=$_POST["f2_GPSLocation"]; 
$NumberOfBirds=$_POST["f2_numBirds"];
$AdditionalDetails=$_POST["f2_additionalDetails"]; 

// get photo attachment
$attachment = $field_file = $_POST['f2_photoURI'];
$tmpName = $_FILES['f2_photoURI']['tmp_name'];
$fileType = $_FILES['f2_photoURI']['type'];
$fileName = $_FILES['f2_photoURI']['name']; 


// setup email header
//$Headers = "From:" .$FromEmail;

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

// email to BCMC
$email_message .= "You have recieved a Bird Report through the BCMC Mobile App
";
$email_message .= "

";
$email_message .= "Name: ".$FullName."
";
$email_message .= "Address: ".$LocationAddress."
";
$email_message .= "GPS Location: ".$GPSLocation."
";
$email_message .= "E-mail: ".$FromEmail."
";
$email_message .= "Telphone: ".$Phone."

";
$email_message .= "Number of Birds?  ".$NumberOfBirds."

";
$email_message .= "Additional Details:
    ".clean_string($AdditionalDetails)."

";
$email_message .= "

";

// create email headers
 $headers = 'From: '.$FromEmail."
".
        'Reply-To: '.$FromEmail."
" .
        'X-Mailer: PHP/' . phpversion();

if (!empty($tmpName)) 
{
      /* Reading file ('rb' = read binary)  */
      $file = fopen($tmpName,'rb');

      $data = fread($file,filesize($tmpName));
      fclose($file); 
        /* a boundary string */
      $randomVal = md5(time());
      $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

      /* Header for File Attachment */
      $headers .= "
MIME-Version: 1.0
";
      $headers .= "Content-Type: multipart/mixed;
" ;
      $headers .= " boundary=\"{$mimeBoundary}\"";

      /* Multipart Boundary above message */
      $email_message = "This is a multi-part message in MIME format.

" .
      "--{$mimeBoundary}
" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"
" .
      "Content-Transfer-Encoding: 7bit

" .
      $email_message . "

";

      /* Encoding file data */
      $data = chunk_split(base64_encode($data));

      /* Adding attchment-file to message*/
      $email_message .= "--{$mimeBoundary}
" .
      "Content-Type: {$fileType};
" .
      " name=\"{$fileName}\"
" .
      "Content-Transfer-Encoding: base64

" .
      $data . "

" .
      "--{$mimeBoundary}--
";
}

    // send email to BCMC (and DHEC?)
    mail($BCMC_email, $email_subject, $email_message, $Headers);


?>

Any help would be greatly appreciated.

Thanks,

RGecy

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。