I have a form with a submit button whoses value is 'Proceed to Payment'.
When I press it, my _validate and then my _submit functions are called.

Under certain circumstances I need to change the text of my form's submit button from 'Proceed to Payment'
to 'Provisionally Book'.

I do this using the following jquery: $('#edit-next-button').val('Provisionally Book');

After this has happened and I then press the button, the _validate function is called but the _submit is not.

I have tried doing this in my _validate function to try and get the _submit to work, but to no avail:
unset($form_state['buttons']['submit'][0]['#post']['op']);
$form_state['buttons']['submit'][0]['#post']['op'] = 'Proceed to Payment';

How do I get it to call the _submit button?

The submit button was created using form api:
....

$nextText = 'Proceed to Payment';
$form['next_button'] = array(
'#type' => 'submit',
'#value' => t($nextText),
);
Any help appreciated.

Comments

jaypan’s picture

1) Is the submit function the default form submit function, or have you added it to the button only?
2) What does your submit function look like?

Contact me to contract me for D7 -> D10/11 migrations.

gony’s picture

The submit function is the default form submit function.

The module is called booking_form.

Here is the submit function:


// submit booking form step 1
function booking_form_bookingdetailsStep1_submit($form, &$form_state)
{
	$step1Details = getStep1Details($form, $form_state);
	storeStep1Details($step1Details);
	$step1Action = getStep1Action($form);
	switch ($step1Action)
	{
		case 'email':
			emailOwner($step1Details);
			break;
		case 'prov':
			makeAProvisionalBooking($step1Details);
			break;
		case 'pay':
			makeAnOnlineBooking($step1Details);
			break;
	}
}

Here is _validate function:


// validate booking form step 1
function booking_form_bookingdetailsStep1_validate($form, &$form_state)
{		
	$passed = true;	
	$step1Details = getStep1DetailsFromSession($_GET['code']);
	if ((isset($step1Details)) && ($step1Details->flowComplete))
	{		
		$passed = false;
		form_set_error('date_from', t("You have already completed booking this property."));
	}
	else
	{
		$property = $form['property']['#value'];
	
		// don't validate pets, children and adults if owner managed
		$isOwnerManaged = isOwnerManaged($property);
		if ($isOwnerManaged == false)
		{
			validatePeople($form_state, $property, $passed);
			validatePets($form_state, $property, $passed);
		}
	
		$startDateUnix = strtotime($form_state['values']['date_from']);
		if (validAvailability($startDateUnix, $form_state['values']['booking_period'], $property->ID) == false)
		{	
			form_set_error('date_from', t("This property is unavailable during this period, please make another date selection."));
			$passed = false;
		}
		validateEmail($form_state, $passed);
	}	
}

Thanks for any help you can give!

jaypan’s picture

How do you know the submit function is not being called?

Contact me to contract me for D7 -> D10/11 migrations.

gony’s picture

I have put logging helper call info into both the the validate and submit function (it writes to a log file and has been welll used) and its write to log file for validate but not submit.

I am simply returned to the same page with the same url.

jaypan’s picture

Ok. I think you should be able to fix this by explicitly adding your submit function to the form definition:

This is what I *think* is happening. When building forms, you give the submit button a value (the displayed text). Ex:

$form['submit'] = array
(
  '#type' => 'value',
  '#value' => t('Original value'), // <--- this is the text shown in the button)
);

When you submit a form, it submits to the current page. You may have more than one form on a page. So Drupal has to check the value of the clicked submit button in order to determine which function has been submitted. It does this by checking the value of the submitted submit button.

The problem is that you are changing the name of the submit button in the browser using javascript, after it has been sent from the server to the users computer. Then when you are submitting the form, the submit button value has changed from the original value. Drupal checks to see if your form has been submitted by checking the value of the submit button... and it has changed, so it doesn't execute your submit function.

I'm thinking maybe you can fix this a couple of different ways:
1) The easiest way would be to determine the value of the submit button on the server. This means that the server (Drupal) would know the value of the button, and could find the submit function.
2) If #1 isn't possible (maybe the value of the button needs to change depending on user interaction), then you can try these:
A) Explicitly declare the submit function in your form definition. I suspect this may cause you troubles though, as it could potentially be called twice.
B) Change the value of the submit button using the FAPI #ajax attribute. This will allow for you to change the value, and the server will know of the changes, meaning your form will properly submit.

Hopefully this helps.

Contact me to contract me for D7 -> D10/11 migrations.

gony’s picture

Many thanks for your help here Jay.
I am not too clear on what you mean in option 1), if you get a chance could you explain otherwise I believe from what you are saying that 2 B) will be the way to go.

jaypan’s picture

Right now you are using javascript to change the value of the button. This happens on the browser. Number one referred to using PHP on the server to set the value of the button before sending it to the browser. If you do this, Drupal will know the value of the button and it will solve your problem.

However, if you are changing the value of the button as a result of some user input or environment change, then you will need to do it using option 2B.

Contact me to contract me for D7 -> D10/11 migrations.

gony’s picture

Ahhh, thanks.
2B it is then.

Many thanks for all your help.