becca800 Posted October 3, 2008 Share Posted October 3, 2008 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''] Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/ Share on other sites More sharing options...
becca800 Posted October 4, 2008 Author Share Posted October 4, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656789 Share on other sites More sharing options...
.josh Posted October 4, 2008 Share Posted October 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656798 Share on other sites More sharing options...
becca800 Posted October 4, 2008 Author Share Posted October 4, 2008 Yes, I have been looking, but it is WAY above my head. I really know basic html. As for php, js and ajax, I just modify here and there when I already have a script. Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656802 Share on other sites More sharing options...
.josh Posted October 4, 2008 Share Posted October 4, 2008 Dunno what else to tell you except perhaps hire someone to do it for you... We aren't really here to write your code for you. Not saying that's what you're asking; just letting you know. Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656816 Share on other sites More sharing options...
becca800 Posted October 4, 2008 Author Share Posted October 4, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656847 Share on other sites More sharing options...
xylex Posted October 4, 2008 Share Posted October 4, 2008 Also, you can't do a file upload with a regular AJAX call. You need to add an iframe somewhere and do it from there. Quote Link to comment https://forums.phpfreaks.com/topic/126934-passing-php-session-variable-to-js-without-form-submission/#findComment-656904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.