Form Submit Issue
Below is my form submit statement for my site. I know that the query works by it self however it does not seem to be setting the variable correctly. Does anybody see why this would not work, or am I going about this the wrong way? Basically my module uploads a file and then on submit this function query’s the DB for the upload path and needs to set that value to be a drupal variable. Any Ideas?
function background_audio_form_submit($form, $form_state) {
$result = db_query("SELECT uri
FROM `file_managed`
WHERE `filemime` = 'audio/mpeg'
LIMIT 1");
return $result;
variable_set('background_audio_upload_path', $result-->uri);
}
– Answer –
- 23 February 2012: Answer by Clive for Form Submit Issue -
A couple of things:
- You're returning the
$resultvariable from the function before callingvariable_setso that bit of code will never run. - You have a slight syntax error in your variable set code,
$result-->uri(you should change it to:$result->uri)
This code should work:
function background_audio_form_submit($form, $form_state) { $result = db_query("SELECT uri FROM `file_managed` WHERE `filemime` = 'audio/mpeg' LIMIT 1"); variable_set('background_audio_upload_path', $result->uri); }The return from a form submit function is never used in Drupal so there's no need to perform the
returnat all.EDIT
Just one other thing, if you're uploading the file in a custom form using a
managed_fileelement then you'll have the file ID of the uploaded file available in your submit function. You could then usefile_load()to get the file object and URI without having to use a custom query. Assuming your element is defined as$form['audio_file']the following should work:function background_audio_form_submit($form, $form_state) { $fid = $form_state['values']['audio_file']; $file = file_load($fid); variable_set('background_audio_upload_path', $file->uri); } - You're returning the
- 23 February 2012: Form Submit Issue -
Below is my form submit statement for my site. I know that the query works by it self however it does not seem to be setting the variable correctly. Does anybody see why this would not work, or am I going about this the wrong way? Basically my module uploads a file and then on submit this function query's the DB for the upload path and needs to set that value to be a drupal variable. Any Ideas?
function background_audio_form_submit($form, $form_state) { $result = db_query("SELECT uri FROM `file_managed` WHERE `filemime` = 'audio/mpeg' LIMIT 1"); return $result; variable_set('background_audio_upload_path', $result-->uri); }