Jump to content

Recommended Posts

Hi there. I have a pretty complicated form designed for Joomla where the form input boxes combine all user data and some html to create an article in Joomla. It uses php and js. I am almost finished but have hit a snag.

 

There is a file upload portion in the form. When the file is uploaded upon submit, a php function is triggered which creates a unique file name for that file and uploads it into one folder. I need to be able to catch that variable BEFORE submission, transfer it to a js variable, and inserted back into the hidden field value which is the html creation. My js portion of the form takes all form fields onblur and copies them into my hidden variable which is then passed onsubmit into the db as one long html article. But if I add in this variable converted to js, when the onblur is triggered, it prematurely triggers the php function and I suffenly get a white screen. how can I pass the variable as soon as a file is entered into the box without refreshing the screen or submitting?

 

The php function variable I need is called $attachments['file_13'']

Okay, no help yet.  Here is another way of asking.  I have done some research but not sure how to proceed.

 

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:

 

    /**
     * Upload attachments
     */
$attachments = array();
if ( trim($paramsvalues->uploads == 'Yes' ) && trim($paramsvalues->uploadfields) ) {
	//$allowed_s1 = explode(",", trim($paramsvalues->uploadfields));
		if ( is_array($paramsvalues->uploadfields) ) {
			$allowed_s1 = implode('|', $paramsvalues->uploadfields);
		} else {
			$allowed_s1 = $paramsvalues->uploadfields;
		}
		$allowed_s1 = explode(",", trim($allowed_s1));

	foreach ( $allowed_s1 as $allowed_1 ) {
		$allowed_s2      = explode(":", trim($allowed_1));
		$allowed_s3      = explode("|", trim($allowed_s2[1]));
		$allowed_s4      = explode("{", trim($allowed_s3[count($allowed_s3) - 1]));
		$allowed_s3[count($allowed_s3) - 1]	= $allowed_s4[0];
		$allowed_s5      = explode("-", str_replace('}', '', trim($allowed_s4[1])));
		$original_name   = $_FILES[$allowed_s2[0]]['tmp_name'];
		$filename        = date('YmdHis').'_'.preg_replace('`[^a-z0-9-_.]`i','',$_FILES[$allowed_s2[0]]['name']);
		$fileok          = true;
		if ( $original_name ) {
			if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) > trim($allowed_s5[0]) ) {
				$fileok = false;
				$session->set("chrono_verification_msg", 'Sorry, Your uploaded file size exceeds the allowed limit.', md5('chrono'));
				showform($_POST);
				return;
			}
			if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) < trim($allowed_s5[1]) ) {
				$fileok = false;
				$session->set("chrono_verification_msg", 'Sorry, Your uploaded file size is less than the allowed limit', md5('chrono'));
				showform($_POST);
				return;
			}
			$fn     = $_FILES[$allowed_s2[0]]['name'];
			$fext   = substr($fn, strrpos($fn, '.') + 1);
			if ( !in_array(strtolower($fext), $allowed_s3) ) {
				$fileok = false;
				$session->set("chrono_verification_msg", 'Sorry, Your uploaded file type is not allowed', md5('chrono'));
				showform($_POST);
				return;
			}
			if ( $fileok ) {					
				$uploadedfile = handle_uploaded_files($original_name, $filename);
				$_POST[$allowed_s2[0]] = $filename;
				if ( $uploadedfile ) {
                        $attachments[$allowed_s2[0]] = $uploadedfile;
				}
			}
		}
	}
}

 

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...

Yes, you would use ajax.  Just google for any basic ajax form validation tutorial: you know, the ones that check to see if username or emails are already registered.  Same principle.  Just have the ajax object send the file to the php script that makes the name and return the name and have js then put it into your form as the hidden field.

I hear ya.  Just thought maybe someone would have snippets and that it might be "easy" for someone else.  Takes me days upon days just to figure out one small thing.  :-)  I have been searching for about a week to figure this part out.  It is hard when you are completely clueless (and broke).

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.