Does anyone know how to not clear the form on submit when using WPForms (Wordpress). I have hooked in custom processing on submit and I want to just leave the form up on the page, and ajax process the input, and not clear the form on completion (so the user can submit again easily on any error).
If you are willing to make changes to the code, you could consider returning the input value which has been submitted to be displayed in the input field... I have extracted some code from the woocommerce form-login.php as an example:
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" />
If you input a value for username, hit submit, and the form returns for any reason to that same page, the last entered value would be displayed in that input field.
Related
I have created the following code which I have added to the PHP function to create two input fields placed just below the fixed-rate shipping method on the check out page. In these two input fields customers can enter their shipping provider and account number.
The input fields work perfectly, the problem is that when the order is placed, the values entered (shipping provider name and account number) in these two fields do not appear in the order details on woocommerce.
Can you please advise me how I can make the two fields appear on the woocommerce order management page so that I know what customers have entered?
code:
function checkout_shipping_additional_field( $method, $index )
{
if( $method->get_id() == 'flat_rate:35' ){
echo '<br>
<input type="text" id="Name" name="Name" placeholder="Shipping Provider">
<input type="text" id="Name" name="Name" placeholder="Account Number">';
}
}
I am using Wordpress and framework Gantry 5, I have custom html form added via JS as an innerHTML added to existing container.
I want this form values be submitted to email adress defined in WordPress administration settings. Is there any way i can achieve it?
it depends from the form action, if your form action call a function inside your wordpress (for example in function.php) you can pick the email address
get_option('admin_email')
and use it to send the post data.
If the form action call a function external to you wordpress you can add the email as an hidden field in your form
<input type="hidden" id="email" name="email" value="<?php echo get_option('admin_email'); ?>">
and get the value in the $_POST object.
Because you form is added by javascript you can add the hidden field by javascript before the form submit maybe using jquery (you also can do this in vanilla js).
If you print the script directly inline with php
$("#yourFormID").submit( function(eventObj) {
$("<input />").attr("type", "hidden")
.attr("name", "email")
.attr("value", "<?php echo get_option('admin_email'); ?>" )
.appendTo("#form");
return true;
});
If you put the script in js file you can print the hidden field outside with php and then pick the value with jquery (or also vanilla)
<input type="hidden" id="email" name="email" value="<?php echo get_option('admin_email'); ?>">
$("#yourFormID").submit( function(eventObj) {
$("<input />").attr("type", "hidden")
.attr("name", "email")
.attr("value", $('#email').val() )
.appendTo("#form");
return true;
});
So, I got a payment plugin where some information is entered in an popup.
The popup stores the input data in a hidden input on the payment page
In payments field I got this for the hidden input:
<input type="hidden" class="" name="auth" id="auth" placeholder="" value="">
Via JS I put the value from the popup to the input hidden data, which definetly works!
Then I add an action:
add_action( 'woocommerce_checkout_update_order_meta', 'ccp_payment_update_order_meta' );
function ccp_payment_update_order_meta( $order_id ) {
update_post_data( $order_id, 'auth', sanitize_text_field($POST['auth']) );
}
Somehow after the submission of the checkout form the data ($POST['auth']) is empty.
Why is this happening?
How can I store the value right?
Got it sorted out. It need to be in an paragraph like that
<p class="form-row form-row-wide"><input type="hidden" ...></p>
To make an WordPress widget I created a select box which gets options generated dynamically from $post object. The select box's name is also generated by dynamically. Here is the code
<select class="widefat" name="<?php echo $this->get_field_name( 'employee' );?>" id="<?php echo $this->get_field_id( 'employee' ); ?>">
Results this :
<select class="widefat" name="widget-employee[13][department]" id="widget-employee-13-department">
To check the value of the select box I need to pass it to $_POST Array.
How do I pass the select box's value to $_POST array ?
Thanks in advance.
On submit, the $_POST variable will contain all the input fields/selected values etc. from your form in it. You don't have to manually add anything. You can manually pull info from your $_POST variable if you wish.
You said you want to check $_POST against your value ($post->post_title), in that case, since your select has a name
widget-employee[13][department]
You'll use something like
$_POST['widget-employee'][13][department]
The best bet is to just use print_r($_POST) to see the contents of your $_POST variable and see what is in it, so that you can check against.
I created a front end registration form, I used the filters 'registration_errors' to customize the messages.
After WP detects the error and use 'wp-redirect' to return to the registration page and display an error if the email or the user exists for example.
My question is: how I can keep the previous values that generated the error.
¿JS?
Thanks in advance!
To keep values in the form after the error message:
function my_register_sesion (){
session_start();
$_SESSION['key_login']=$_REQUEST['user_login'];
$_SESSION['key_email']=$_REQUEST['user_email'];
}
add_action ('register_post', 'my_register_sesion');
My inputs form should be as follows:
<input type="text" name="user_login" id="user_login" class="input" value="<?php echo $_SESSION['key_login'];?>">
<input type="text" name="user_email" id="user_email" class="input" value="<?php echo $_SESSION['key_email'];?>">
Thank you David!