Jump to content

Passing PHP Variable to JS Without Form Submission With AJAX


becca800

Recommended Posts

I think I posted in the wrong board, so here I go again.

 

I have a php form which also uses js. The form allows for upload attachments. On submit, the form accesses a php set of functions and one of those functions is for the upload attachments which is:

 

   1.     /**
   2.      * Upload attachments
   3.      */
   4.     $attachments = array();
   5.     if ( trim($paramsvalues->uploads == 'Yes' ) && trim($paramsvalues->uploadfields) ) {
   6.         //$allowed_s1 = explode(",", trim($paramsvalues->uploadfields));
   7.             if ( is_array($paramsvalues->uploadfields) ) {
   8.                 $allowed_s1 = implode('|', $paramsvalues->uploadfields);
   9.             } else {
  10.                 $allowed_s1 = $paramsvalues->uploadfields;
  11.             }
  12.             $allowed_s1 = explode(",", trim($allowed_s1));
  13.        
  14.         foreach ( $allowed_s1 as $allowed_1 ) {
  15.             $allowed_s2      = explode(":", trim($allowed_1));
  16.             $allowed_s3      = explode("|", trim($allowed_s2[1]));
  17.             $allowed_s4      = explode("{", trim($allowed_s3[count($allowed_s3) - 1]));
  18.             $allowed_s3[count($allowed_s3) - 1] = $allowed_s4[0];
  19.             $allowed_s5      = explode("-", str_replace('}', '', trim($allowed_s4[1])));
  20.             $original_name   = $_FILES[$allowed_s2[0]]['tmp_name'];
  21.             $filename        = date('YmdHis').'_'.preg_replace('`[^a-z0-9-_.]`i','',$_FILES[$allowed_s2[0]]['name']);
  22.             $fileok          = true;
  23.             if ( $original_name ) {
  24.                 if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) > trim($allowed_s5[0]) ) {
  25.                     $fileok = false;
  26.                     $session->set("chrono_verification_msg", 'Sorry, Your uploaded file size exceeds the allowed limit.', md5('chrono'));
  27.                     showform($_POST);
  28.                     return;
  29.                 }
  30.                 if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) < trim($allowed_s5[1]) ) {
  31.                     $fileok = false;
  32.                     $session->set("chrono_verification_msg", 'Sorry, Your uploaded file size is less than the allowed limit', md5('chrono'));
  33.                     showform($_POST);
  34.                     return;
  35.                 }
  36.                 $fn     = $_FILES[$allowed_s2[0]]['name'];
  37.                 $fext   = substr($fn, strrpos($fn, '.') + 1);
  38.                 if ( !in_array(strtolower($fext), $allowed_s3) ) {
  39.                     $fileok = false;
  40.                     $session->set("chrono_verification_msg", 'Sorry, Your uploaded file type is not allowed', md5('chrono'));
  41.                     showform($_POST);
  42.                     return;
  43.                 }
  44.                 if ( $fileok ) {                   
  45.                     $uploadedfile = handle_uploaded_files($original_name, $filename);
  46.                     $_POST[$allowed_s2[0]] = $filename;
  47.                     if ( $uploadedfile ) {
  48.                         $attachments[$allowed_s2[0]] = $uploadedfile;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }

 

 

This function renames the file. I need to be able to get the name of this file for my form before it is submitted because it is needed to be placed in the html of the hidden form field. The rest of the form field which are filled out by the user are transferred to a hidden field using js mixed with html. You might ask why I am doing it this way...it is a joomla site and any other way would entail modification of the core content component which I don't want to do.

 

So.....any thoughts? Maybe have this upload function post to a text file or use ajax? I don't know how though...

Link to comment
Share on other sites

Ah, I didn't understand exactly what you were asking, but I think I do now. You want to grab information from the file input (after they click browse). If that's right, you can't do that. That is disabled for security reasons. If you could access that value, you would potentially have access to the user's file system.

 

Why exactly do you need that information?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.