0

I have a form on wordpress page with a POST method. I need to add an extra field to the form after submission but before it is sent to the URL in the action attribute.

I was thinking to use an actin hook, not sure which one would be the best for this case? or if it's the correct tool in the first place?

Thank you

Edit
The form data is going to an e-banking service. the bank requires an extra field which is the hash value of all the variables concatenated together. the bank does the same operation when they receive a submission and compare with the extra field to ensure data integrity.

  • Why would you need to inject an extra field after submission? 1) I can't think a use case that cannot be achieved otherwise, and 2) your form will be utterly dependent on JavaScript on the client side. – s_ha_dum Dec 17 '15 at 15:17
  • @s_ha_dum, just a minute, I will edit the question and add the reason – Moha the almighty camel Dec 17 '15 at 15:18
  • 2
    The bank needs to be smacked upside the head, if they are counting on that idea as part of their security model... – C C Dec 17 '15 at 15:22
  • Thank you, but I can't do anything about it :) – Moha the almighty camel Dec 17 '15 at 15:23
  • 1
    Look at my answer, below. You can use .js (jQuery) as both s_ha_dum and I have stated, or you can post to "landing.php", then do some data manipulation there, then redirect (and exit) with the new argument string appended to the URL to some other destination. It's clunky but it should work... – C C Dec 17 '15 at 15:36
2

Im not entirely sure there is such action hook for custom forms (someone else might know) but there are definately other ways if form is submitted normally, without ajax.

Let me know if you have any questions.

//This applies if you want it to happen only if form is submited and page refreshed
//It doesn't work if user comes back later - there's no extra field later on

$form_submitted = false;

//If submit is pressed
if( isset( $_POST['submit'] ) ) {

    //There should also be a nonce check here

    $form_submitted = true;      
}

//All your form html here

//Check if form is submitted
if ( $form_submitted ) {

     //Show your extra fields
}



/*If you want it to be a bit more "permanent"

1. update_usermeta() to save the bool that user has submitted the form - logged-in users only
2. Use cookies which might not work properly in browser "stealth mode" and is gone if user deletes browser history with cookies 
3. Use sessions which destroys itselves if user closes the browser

*/
1

Form submission is all client-side, there will not be a hook in WordPress that will handle this. Best bet is to use jQuery to trap the #element.submit() action, then populate a hidden field on your form with whatever data you need.

Note that you will have to utilize the preventDefault() action in jQuery, to trap the actual form submission from happening. Do what you need to do, then have jQuery finally submit the form. Documentation is here:

https://api.jquery.com/submit/

The example they show on that page is almost exactly what you are looking to do.

This really is not a typical/usual situation, however (aside from input validation). You really should be doing your data manipulation on the server sided, after the form is posted back to the receiving .php code...

  • Can I post the form to a PHP page where I implement what I want , and from there I post again to the server after adding the extra field? – Moha the almighty camel Dec 17 '15 at 14:18
  • Once the form is posted (i.e. sent back to the server for the receiving URL, which is really a PHP function) - you could redirect to another URL at that point. See: codex.wordpress.org/Function_Reference/wp_redirect – C C Dec 17 '15 at 14:36
1

The only way to do this "server side" is to post the form to a script on your server then make a request to the bank from there.

You would need to post to admin-ajax.php. There are numerous Q/As here explaining how to do that such as this one I wrote.

What I don't know are the PCI Standards implications of doing that, and I am not about to start writing code for that reason.

  • can you explain why does it have to be through ajax, can't it be redirected to a php scrip which sends the date to the bank after processing ? – Moha the almighty camel Dec 17 '15 at 15:58
  • Submitting to admin-ajax.php is going to be the simplest way to process the form while having access to WordPress functions, even if not technically an AJAX request. – s_ha_dum Dec 17 '15 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.