javascript – Execute the function before submitting the form

I'd like to run a function before submitting a form, but apparently it never runs. Is it possible to run it before sending without using attributes such as onsubmit="return funcion()" or send it with ajax?

HTML form

         

Code JS

        let form = document.querySelector('.form-search')
            form.addEventListener('submit', (e) => {
              e.preventDefault()
              console.log('submit')
              //funciĆ³n a ejecutar antes de enviar
              saveSearch(input.value)
        })

400 incorrect queries when submitting the form with AJAX

I am poor in AJAX and I try to submit the form and insert the record in the custom table for a few days, but I can not get it working.

I'm getting a 400 Bad Query error on the console. Please have a look at the code.

In fact, I've tried several ways to submit data, but none of them
works.

HTML

function gs_enqueue_ajax_scripts()
{

    wp_register_script('gs-ajax', GROUP_SHOP_ROOT . 'public/js/add-to-cart-ajax.js', ('jquery'), 1.0, TRUE);
    wp_localize_script('gs-ajax', 'ajax_vars', (
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce'    => wp_create_nonce('gs_nonce'),
    ));
    wp_enqueue_script('gs-ajax');

}

add_action('wp_enqueue_scripts', 'gs_enqueue_ajax_scripts');

WordPress Hooks – Edit and insert data

function gs_add_to_cart_ajax()
{

    check_ajax_referer('gs_nonce', $_POST( 'nonce' ), FALSE);

    // validating stuffs ..

    $cart = new Group_Shop_Cart();
    $cart->add_to_cart($_POST( 'group_id' ), $_POST( 'product' ), $_POST( 'qty' ), $_POST( 'product_option' ));

    wp_die();
}

add_action('wp_ajax_gs_add_to_cart_ajax', 'gs_add_to_cart_ajax');
add_action('wp_ajax_nopriv_gs_add_to_cart_ajax', 'gs_add_to_cart_ajax');
(function ($) {

    $(document).on("click", ".add-to-cart-button", function () {

        let data = JSON.stringify({
            action: 'gs_add_to_cart_ajax',
            group_id: $('#group_id').val(),
            product: $('#product').val(),
            qty: $('#qty').val(),
            product_option: $('#product_option').val(),
        });

        $('form.addtocartform').on('submit', function (e) {
            e.preventDefault();

            $.ajax({

                method: 'POST',
                dataType: 'json',
                nonce: ajax_vars.nonce,
                url: ajax_vars.ajax_url,
                data: data,
                success: function (response) {
                    alert("Success");
                }

            });
        });

    });

})(jQuery);

I have no idea what's wrong with this code than not to submit.

custom publication types – Change the term according to the value of a $ variable with the help of wp_update_post when submitting a form

Context

  • I use a form with facade to submit data to a meta-box using the
    wp_update_post on a custom publication type (call) with a custom taxonomy (type of call).
  • He then also updated the terms and taxonomy of the publication.
  • The code works 100% for this.

Here is the work code

//update post
    $post_id = wp_update_post($post_information);
    if($post_id)
    {
        // Update Custom Meta
        update_post_meta($post_id, '_call_16', $_POST('call_16'));
        update_post_meta($post_id, '_call_17', $_POST('call_17'));
        update_post_meta($post_id, '_call_18', $_POST('call_18'));
        update_post_meta($post_id, '_call_19', $_POST('call_19'));
        // Update Custom Meta
        update_post_meta($post_id, '_call_20', $_POST('call_20'));
        // Update call "terms" or taxonomy
        $term_ids = array( 3, 14 );
        $taxonomy = 'call-type';
        wp_set_object_terms( $post_id, $term_ids, $taxonomy );
        // Redirect
        wp_redirect( '$url' );

}
?> 

HTML from

...
...

The problem

I would like the code to update the termbase to a value of the submitted entry.

The desired result is.

  • If $ _POST (& # 39; call_16 & # 39;) = No change the term into 18.
  • If $ _POST (& # 39; call_16 & # 39;) = Yes change the term into 14.

The code I've tried

//update post
    $post_id = wp_update_post($post_information);
    if($post_id)
    {
        // Update Custom Meta
        update_post_meta($post_id, '_call_16', $_POST('call_16'));
        update_post_meta($post_id, '_call_17', $_POST('call_17'));
        update_post_meta($post_id, '_call_18', $_POST('call_18'));
        update_post_meta($post_id, '_call_19', $_POST('call_19'));
        // Update call terms or taxonomy
        if (isset($_POST('call_16')) == 'No' ) {
            $term_ids = array( 3, 18 );
            $taxonomy = 'call-type';
            wp_set_object_terms( $post_id, $term_ids, $taxonomy );
            // Redirect
            wp_redirect( '$url2' );
        }
        else {
            $term_ids = array( 3, 14 );
            $taxonomy = 'call-type';
            wp_set_object_terms( $post_id, $term_ids, $taxonomy );
            // Redirect
            wp_redirect( '$url' );
        }
    }
?>

Anyone can he advise if it is possible and how to do it in detail please.

.

How to post a success message after submitting the form?

In my current Laravel app, all you have to do is fill out a form. So, after submission, nothing happens.

So now, I wonder how it would be better to post the success message and I thought about it in three different ways:

  1. Just redirect with a success message (where the user can view the data that he just sent)

  2. to display a new complete page with the success message says -only

  3. show a modal (which seems to me the worst)

After submitting the form, nothing else happens;
I do not know what is the best solution or if one of them has advantages or disadvantages compared to others.
Maybe you have another better idea?

json rpc – Why am I receiving an Auth error when submitting rpc credentials on the command line?

Direct entry of RPC credentials fails, as follows:

herbert@Spencer:~/bitcoin-0.18.0/bin$ ./bitcoin-cli -testnet -rpcconnect=127.0.0.1 -rpcuser=x -rpcpassword=p getblockchaininfo
error: Authorization failed: Incorrect rpcuser or rpcpassword

while picking them up succeeds indirectly:

herbert@Spencer:~/bitcoin-0.18.0/bin$ ./bitcoin-cli -conf=/home/herbert/.bitcoin/bitcoin.conf -datadir=/home/herbert/.bitcoin getblockchaininfo
{
  "chain": "test",
  "blocks": 1576504,
  "headers": 1576504,
  "bestblockhash": "00000000318a0aa170e250d72d2973b4af97812c2c2704a457ce2e7bcbb8c8df",
...

The .conf file is minimal:

testnet=1

server=1

rpcusername=x

rpcpassword=p

it is definitely in the right place – hence the fact that Bitcoin works in testnet mode.

journals – Should I report minor writing errors that I have noticed after submitting the revised version?

I received a letter of decision from the editor on my document. "I am ready to accept this manuscript for publication, provided that the authors make certain modifications to improve the grammar in the document".
After submitting the revised version, I noticed some minor corrections to make regarding post-revision write errors (one sentence was written twice and some punctuation errors). Should I approach them now or wait for the editing phase?

magento2.3 – Unique Violation Violation Detected When Submitting an Order

Has anyone ever encountered, after updating Magento 2.3, a "Single Violation Violation Discovered" error when generating a mailing? I could only find answers with the same error regarding categories or products.

There were a lot of errors in Klarna Checkout module 7.1.0, and I updated it to 7.3.0 – now the payments go by.

For the error "A unique constraint violation found", Magento adds nothing to the logs. So it's a bit difficult to check where it's coming from.

This sounds like a database problem, but even if I use backup sales tables prior to the update to version 2.3, the error persists.

Can the error be generated on the Klarnas side and simply displayed on the backend of Magentos?

How can I find out what is causing the error in the first place?

Thanks to you all!

How to redirect to a page after submitting a form

At D8, I did it like that (but it will not work …

/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_FORM_ID_form_alter( array &$form, FormStateInterface $form_state, $form_id ) {

   // Force redirect to "/thankyou" page
   $url = "/thankyou";
   $path = Drupal::service('path.alias_manager')->getPathByAlias($url);
   if(preg_match('/node/(d+)/', $path, $matches)) {
     $form_state->setRedirect('entity.node.canonical',
       array('node' => $matches(1))
     );    
   }

}

Edit/

I missed setRedirect / setRedirectUrl AFTER the module was submitted. Here is my solution working on D8:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_FORM_ID_form_alter( array &$form, FormStateInterface $form_state, $form_id ) {
  $form('actions')('submit')('#submit')() = 'MYFORM_redirect_handler';
}

function MYFORM_redirect_handler($form, FormStateInterface $form_state) {
  // Force redirect to "/sent"
  $dest_url = "/sent";
  $url = Url::fromUri('internal:' . $dest_url);
  $form_state->setRedirectUrl( $url );
}

I hope it could help

7 – How to keep the checkbox checked after submitting the form in drupal?

I try to keep the box "checked" after submitting the form and loading the page. I have the following simple code from .module file.

     'Example Form', //page title
    'description' => 'A form to mess around with.',
    '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('my_module_form'), //put the name of the form here
    'access callback' => TRUE
  );
  return $items;
}

function my_module_form($form, &$form_state) {

$form('terms') = array(
        '#type' => 'checkbox',
        '#title' => t('Your checkbox'),
    );
  $form('submit_button') = array(
    '#type' => 'submit',
    '#value' => t('Click Here!'),
  );

  return $form;
}

function my_module_form_submit($form, &$form_state) {
  $form('terms')('#default_value') = 1;
  }

?>

How can I achieve this goal?

Prevent emails from submitting web forms

I have a list of email addresses I would like not to be able to submit in web forms. I can not find a setting for this? Is it currently possible or do I need to create a custom module to implement it?