hello,

i started to create my first module. it should be a customized contact form.
in my modulename.module file i have the following hook_form implementation:

function kontakt_form($form, &$form_state) {
	
	$form["description"] = array(
		"#type" => "item",
		"#title" => "Some text"
	);
	
	$form["name"] = array(
		"#type" => "textfield",
		"#title" => "Name",
		"#description" => "Your full name",
		"#required" => TRUE
	);
	
	$form["email"] = array(
		"#type" => "textfield",
		"#title" => "E-Mail",
		"#description" => 'Your e-mail adress (e.g. john@doe.net)',
		"#required" => TRUE
	);
	
	$form["message"] = array(
		"#type" => "textarea",
		"#title" => "Message",
		"#rows" => 5,
		"#required" => TRUE
	);
	
	$form["send_copy"] = array(
		"#type" => "checkbox",
		"#title" => "Send me a copy of this form",
		"#required" => FALSE
	);
	
	$form["submit_btn"] = array(
		"#type" => "submit",
		"#value" => "Submit"
	);
	
	return $form;
}

i've implemented the menu hook to access this form. now, after filling the form with some data, i clicked the submit button, but nothing happened. no post to the server, no action.

i had a look at the html code and inspected the submit button (my first thought was that it maybe rendered a input type="button") but everything was correct.

then i noticed that there is no form-tag in the whole code! so probably i found the reason why the submit button refused to work. but why isn't there a form tag?

Comments

sagar ramgade’s picture

Hi,
You need to write the separate logic for validation and submit like this :

<?php
function kontakt_form_validate($form, &$form_state) {
  // Validation logic.
 //you will get submitted values in $form_state['values'];
}
function kontakt_form_submit($form, &$form_state) {
  // Submission logic.
  //you will get submitted values in $form_state['values'];
}
?>

Hope that helps.

Regards
Sagar

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

kreilinger’s picture

thanks for the answer, but i already did that.

function kontakt_form_validate($form, &$form_state) {
	if($form_state["values"]["name"] == "") {
		form_set_error("name", "You have to enter a name");
	}
}

function kontakt_form_submit($form, &$form_state) {
	// nothing till now
}

the form is still not submitting it's content and there is no form-tag in the html code!

dtengeri’s picture

How does your menu hook look like? Do you access this form via drupal_get_form()?

Regards,
David

kreilinger’s picture

function kontakt_menu() {
	$items = array();
	$items["seite/kontakt"] = array(
		"title" => "Kontakt",
		"page callback" => "kontakt_form",
		"access arguments" => array("access content"),
		"description" => "Kontaktformular"
	);
	
	return $items;
}

any ideas why it's not working?

Chandan Chaudhary’s picture

Maked the following correction in your menu hook

<?php
function kontakt_menu() {
    $items = array();
    $items["seite/kontakt"] = array(
        "title" => "Kontakt",
        "page callback" => "drupal_get_form",
        "page arguments" => array('kontakt_form'),
        "access arguments" => array("access content"),
        "description" => "Kontaktformular"
    );
    
    return $items;
}
?>

now by implement hooks, _validate and _submit you can validate and submit your form

Hope this will submit your form :)

Need Drupal help?
Reach me

Acquia Certified Developer

Backend Frontend and DevOps.

kreilinger’s picture

oh thank you, works pretty good now :)

rectangel’s picture

I had the same problem using D7, it's weird cause the last time i just called the page using page callback in drupal 5 it just work just fine. Any ideas what's going on ?

edit : nvm, found out that in my old app i just called it using drupal_get_form, but not in the menus.

abbas.mulani’s picture

Thank you so much it is worked for me.

agudivad’s picture

Thanks boss, it saved my time a lot :)