我在drupal 7中使用hook_form()创建了一个表单,它可以作为我使用hook_menu()的页面进行访问,我需要将表单用作块。这是我的代码
function contact_form_form($form, &$form_state) { //
$form['name'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Name',
'#size' => 20,
'#maxlength' => 15,
'#required' => TRUE, //make this field required
);
$form['mobileNo'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Mobile Number',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['email'] = array(
'#title' => 'Email id',
'#type' => 'textfield' ,
'#required' => TRUE ,
);
$form['address'] = array(
'#title' => 'Address',
'#type' => 'textarea',
'#cols' => 60,
'#resizable' => TRUE,
'#rows' => 5,
'#required' => TRUE ,
);
$form['image'] = array(
'#type' => 'file',
'#name' => 'custom_content_block_image',
'#title' => t('Block image'),
'#size' => 40,
'#description' => t("Upload a image.Only JPG, JPEG, PNG & GIF files are allowed."),
'#upload_location' => 'public://'
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
//我使用的hook_submit()
function contact_form_form_submit($form, &$form_state) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["custom_content_block_image"]["name"]);
drupal_set_message('this is inside submit
drupal_set_message('this is inside submit
$fileName = $_FILES['custom_content_block_image']['name'];
$tmpName = $_FILES['custom_content_block_image']['tmp_name'];
$fileSize = $_FILES['custom_content_block_image']['size'];
$fileType = $_FILES['custom_content_block_image']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
将所有获取的数据插入.install文件
中创建的drupals数据库中 try {
db_insert('contact_us')
->fields(array(
'name' => $form_state['values']['name'],
'mobile_no' => $form_state['values']['mobileNo'],
'email' => $form_state['values']['email'],
'address' => $form_state['values']['address'],
'image' => $content,
'imageName' => $fileName,
'imageSize' => $fileSize,
'imageType' => $fileType
))
->execute();
}
catch (PDOException $e) {
//print_r($e->getMessage());
drupal_set_message('this is after db_insert <pre>'.print_r($e->getMessage(),true).'</pre>');
}
}
答案 0 :(得分:0)
这样的事情:
function contact_form_block_info() {
$blocks['contact-form-block'] = array(
'info' => t('My Block'),
'cache' => DRUPAL_NO_CACHE
);
return $blocks;
}
function contact_form_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'contact-form-block':
$block['subject'] = t('Contact Form Block Subject');
$block['content'] = drupal_render(drupal_get_form('contact_form'));
}
return $block;
}