使用url参数填充drupal表单api表单

时间:2014-07-15 15:12:12

标签: php forms drupal drupal-7

我有一个drupal表单,我记录了一个人的IP,提交的日期/时间以及他们的电子邮件地址。

<?php

//This custom module will be used on the website to gain consent from our clients because of the recent CASL anti spam laws that were passed in Cnada.

//Menu hook starts here, implements menu and sets the title, url of the page.




function form_casl_menu() {
  $items = array();
  $items['casl-consent/form'] = array( //this creates a URL that will call this form at "url"
    'title' => 'CASL Subscription', //page title
    'description' => 'A form that allows us to send emails to clients with their consent.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_casl_form'), //put the name of the form here
    'access callback' => TRUE
  );
  return $items;

}

//permission hook
function form_casl_permission() {
  return array(
    'administer my module' => array(
      'title' => t('Administer my module'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}



//form hook, form elements start here

function form_casl_form($form, &$form_state) {
//sometext here
 $form['some_text'] = array(
    '#markup' => '<p><b>Simply enter your email address to subscribe</b>
    </p>'
);


   $form['email'] = array(
    '#type' => 'textfield', //their email
    '#title' => 'Email:',
    '#size' => 30,
    '#maxlength' => 150,

    '#required' => TRUE,
  );







  //submit button
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Submit Data'),

  );
  return $form;
}
    //validate hook
    function form_casl_form_validate($form, &$form_state) { //invalid email error
    if (!valid_email_address($form_state['values']['email'])) {
    form_set_error('mail', t('You must enter a valid e-mail address.'));
    }


}




//submit hook
function form_casl_form_submit($form, $form_state) {
  $sDate = date("Y-m-d H:i:s"); //returns the date and time
  global $name;
  $subbed = 'Yes';

  //----------------------------------------------------------------\\
   $ip55 = &drupal_static(__FUNCTION__);

  //ip get function, returns a clients IP address and checks if they're behind a proxy.
  if (!isset($ip55)) {
    $ip55 = $_SERVER['REMOTE_ADDR'];

    if (variable_get('reverse_proxy', 0)) {
      $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
      if (!empty($_SERVER[$reverse_proxy_header])) {
        // If an array of known reverse proxy IPs is provided, then trust
        // the XFF header if request really comes from one of them.
        $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());

        // Turn XFF header into an array.
        $forwarded = explode(',', $_SERVER[$reverse_proxy_header]);

        // Trim the forwarded IPs; they may have been delimited by commas and spaces.
        $forwarded = array_map('trim', $forwarded);

        // Tack direct client IP onto end of forwarded array.
        $forwarded[] = $ip55;

        // Eliminate all trusted IPs.
        $untrusted = array_diff($forwarded, $reverse_proxy_addresses);

        // The right-most IP is the most specific we can trust.
        $ip55 = array_pop($untrusted);
      }
    }
  }


  //-----------------------------------------------------------------\\


  //inserting data into database
  db_insert('CASL')
    ->fields(array(


      'email' => $form_state['values']['email'],//email
      'ip' => $ip55,//ip
      'substatus' => $subbed,
      'datetime' => $sDate,//date and time


    ))->execute();
    //sending confirmation email to the user, letting them know they can unsub at any time.

    $values = $form_state['values'];
    $to = $form_state['values']['email'];
    $subject = 'Confirmation';
    $message ="Thank you for your submission. You may unsubscribe at any time by refilling out this form test and selecting the 'unsubscribe' option. Or, you can simply email us a request to unsubscribe, and we will remove you from our database immediately. 
    If you have any questions or concerns, you can email us at this link: http://www.esti.ca/contact";
    mail($to, $subject, $message);
    drupal_set_message("Thank you! Your information has been received successfully and you have been sent a confirmation email.");



    //thank you message after submission




}


?>

我想知道的是如何使用这样的url参数预填充电子邮件字段:

example.com/form?email=example@email.com

有办法做到这一点吗?或者我必须用PHP制作一个html表单。感谢

1 个答案:

答案 0 :(得分:0)

$form['email'] = array(
  '#type' => 'textfield', //their email
  '#title' => 'Email:',
  '#size' => 30,
  '#maxlength' => 150,
  '#required' => TRUE,
  '#default_value' => isset($_GET['email']) ? check_plain($_GET['email']) : '', 
);