I can not pass variable form by json but others if they spread, help please

1

Hi, thank you very much first of all, I can not send a variable name to a php script per post to insert it in the database, but the other variable of the form if it propagates, I do not see where I can anymore be doing things wrong.

Achievement to spread the id and email but the name nothing.

The form is this.

<div class="text-center"  v-show="email_locker">
                <p class="text-center"><?php _e('Enter your email to download this file!', 'onyxfiles'); ?></p>
                <div v-show="show_alert" class="alert {{ alert_class }}">{{ alert_message }}</div>
                <form id="jkof_emailDLForm" novalidate v-on:submit.prevent="submit_email">
                    <div class="form-group">
                        <label><?php _e('Enter E-mail', 'onyxfiles'); ?></label>
                        <input type="email" id="jkof_inputEmail" class="form-control" name="jkof_email" style="width:100%;">
                        <label>Nombre</label>
                        <input type="text" id="jkof_inputName" class="form-control" name="jkof_name" style="width:100%;">
                    </div>
                    <div class="form-group">
                        <button type="submit" class="rcw-purple rcw-medium rcw-button-7 btn-block">
                            <span class="icon-bg"></span><span class="button-icon fa fa-envelope"></span> <?php _e('Submit & Download File', 'onyxfiles'); ?>
                        </button>
                    </div>
                </form>
                <button type="button" class="rcw-silver rcw-small rcw-button-7 btn-block" v-on:click="go_back('email_locker')">
                    <span class="icon-bg"></span><span class="button-icon fa fa-arrow-left"></span> <?php _e('Go Back', 'onyxfiles'); ?>
                </button>
            </div>

json's function is this

 submit_email:                                   function(){
            this.show_alert                         =   true;
            this.alert_class                        =   'alert-info';
            this.alert_message                      =   jkof_dl_i18n.wait + ' ' + jkof_dl_i18n.adding_email;

            var formObj                             =   {
                action:                                 'jkof_check_email',
                fid:                                    this.file_id,
                jkof_email:                             $("#jkof_inputEmail").val(),
                jkof_name:                              $("#jkof_inputName").val()
            };

            $.post( jkof_ajax_url, formObj, function(data){
                if(data.status == 2){ // Instant
                    app.alert_class                 =   'alert-success';
                    app.alert_message               =   jkof_dl_i18n.success + ' ' + jkof_dl_i18n.getting_download;
                    app.check_download(app.file_id);
                }else if(data.status == 3){
                    app.alert_class                 =   'alert-success';
                    app.alert_message               =   jkof_dl_i18n.success + ' ' + jkof_dl_i18n.emailing_download;
                }else{
                    app.alert_class                 =   'alert-danger';
                    app.alert_message               =   jkof_dl_i18n.denied;
                }
            });
        }

And finally the script that manages it and that does not collect the name is this

function jkof_check_email(){
global $wpdb;
$output                     =   array('status' => 1);
$email                      =   secure($_POST['jkof_email']);
$nomb                       =   $_POST['jkof_name'];
$fid                        =   intval($_POST['fid']);
$file_settings              =   get_post_meta( $fid, 'jkof_dl_settings', true );
$jkof_settings              =   get_option( 'jkof_settings' );
$fields                     =   array();

if(empty($file_settings) || !filter_var($email, FILTER_VALIDATE_EMAIL)){
    $output['swag']=$email;
    wp_send_json($output);
}

/*if(empty($nomb)) {
    wp_send_json($output);
}*/

$domain_email               =    array_pop(explode('@', $email));
if(in_array($domain_email, $jkof_settings['blocked']['domains']) || in_array($email, $jkof_settings['blocked']['emails'])){
    wp_send_json($output);
}

foreach( $_POST as $fk => $fv ){
    if($fk == 'jkof_email'){
        array_push($fields, array(
            'email'     =>  $email,
            'name'      => $nomb
        ));
        continue;
    }
}

$wpdb->insert( 
    $wpdb->prefix . 'of_emails', 
    array( 
        'form_fields'   =>    json_encode($fields),
        'fid'           =>    $fid,
        "time_sent"     =>    time()
    ), 
    array( '%s', '%d', '%d' ) 
);

I do not know what it can be, because the problem is between the json and the php that collects the data by post, it does not receive the data, although instead of the variable it gives a fixed text, it does not reach the php.

Thank you very much.

    
asked by morpheux 19.05.2017 в 09:03
source

2 answers

1

The problem you suffer is due to the way in which jQuery.post() deals with undefined values.

When executing your call $.post (I recommend using $.ajax to better manage connection problems, etc. with a clearer code) there are data that remain as undefined (in your case $("#jkof_inputEmail").val() ) because they are left blank, you can not find that element, or because $ (the "mother" function of jQuery ) does not It is defined in that area. Whatever the problem is, that call returns undefined (at least I did) and jQuery removes all items from the shipment whose value is undefined and ignores them.

On the other hand, you are committing basic errors such as not defining in $scope variables and then assigning them to the value of the <input> fields you need.

You do not have to use jQuery to access the contents of a form field. Assign a data of $scope with ng-model and use it.

Finally, remember to use $scope.$apply() to apply the changes in the view immediately after updating variables of $scope .

In this example I leave you my suggestions so you can review them and apply them to your code.

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  /* Si no los definimos inicialmente darán "undefined" */
  $scope.jkof_inputEmail = '';
  $scope.jkof_inputName = '';
  $scope.file_id = 0;
  $scope.submit_email = function() {
    var jkof_ajax_url = 'https://httpbin.org/post';
    this.show_alert = true;
    this.alert_class = 'alert-info';
    //this.alert_message = jkof_dl_i18n.wait + ' ' + jkof_dl_i18n.adding_email;

    var formObj = {
      action: 'jkof_check_email',
      fid: this.file_id,
      jkof_email: this.jkof_inputEmail,
      jkof_name: this.jkof_inputName
    };
    $.post(jkof_ajax_url, formObj, function(data) {
      $scope.alert_message = data.form;
      $scope.$apply();
    });
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="text-center" v-show="email_locker">
  <p class="text-center">Enter your email to download this file!</p>
  <pre v-show="show_alert" class="alert {{ alert_class }}">{{ alert_message|json }}</pre>
  <form id="jkof_emailDLForm" ng-submit="submit_email()">
    <div class="form-group">
      <label>Enter E-mail</label>
      <input type="email" ng-model="jkof_inputEmail" id="jkof_inputEmail" class="form-control" name="jkof_email" style="width:100%;" value="">
      <label>Nombre</label>
      <input type="text" ng-model="jkof_inputName" id="jkof_inputName" class="form-control" name="jkof_name" style="width:100%;" value="">
    </div>
    <div class="form-group">
      <button type="submit" class="rcw-purple rcw-medium rcw-button-7 btn-block">
        <span class="icon-bg"></span><span class="button-icon fa fa-envelope"></span> Submit &amp; Download File
      </button>
    </div>
  </form>
  <button type="button" class="rcw-silver rcw-small rcw-button-7 btn-block" v-on:click="go_back('email_locker')">
    <span class="icon-bg"></span><span class="button-icon fa fa-arrow-left"></span> Go Back
  </button>
</div></div>
    
answered by 19.05.2017 в 11:15
0

I have been integrating that part of the code, the problem is that together it does not work with the rest of the system, and then it gets even more complicated, I tried in the js code to enter the variable manually

jkof_name: 'hello',

Or even putting in the call to $ .post (jkof_ajax_url, {variables ...}, function (data) {

Anyone who is not the 3 that works does not pay attention to them, which right now I do not have the slightest idea why it happens, that I pass those variables and not

In the end the call to the post, all this is inside a wordpress does to the file admin_ajax.php of the own WP whose code is this and that not even passing it by arguments within the call the variables the achievement propagate.

<?php

/ **  * WordPress Ajax Process Execution  *  * @package WordPress  * @subpackage Administration  *  * @link link  * /

/ **  * Executing Ajax process.  *  * @since 2.1.0  * / define ('DOING_AJAX', true); if (! defined ('WP_ADMIN')) {     define ('WP_ADMIN', true); }

/ ** Load WordPress Bootstrap * / require_once (dirname (dirname ( FILE )). '/wp-load.php');

/ ** Allow for cross-domain requests (from the front end). * / send_origin_headers ();

// Require an action parameter if (empty ($ _REQUEST ['action']))     die ('0');

/ ** Load WordPress Administration APIs * / require_once (ABSPATH. 'wp-admin / includes / admin.php');

/ ** Load Ajax Handlers for WordPress Core * / require_once (ABSPATH. 'wp-admin / includes / ajax-actions.php');

@header ('Content-Type: text / html; charset ='. get_option ('blog_charset')); @header ('X-Robots-Tag: noindex');

send_nosniff_header (); nocache_headers ();

/ ** This action is documented in wp-admin / admin.php * / do_action ('admin_init');

$ core_actions_get = array (     'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',     'autocomplete-user', 'dashboard-widgets', 'logged-in', );

$ core_actions_post = array (     'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',     'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',     'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',     'edit-comment', 'add-menu-item', 'add-goal', 'add-user', 'closed-postboxes',     'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',     'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',     'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',     'save-widget', 'delete-inactive-widgets', 'set-post-thumbnail', 'date_format', 'time_format',     'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',     'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',     'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',     'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',     'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',     'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',     'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',     'install-theme', 'get-post-thumbnail-html', );

// Deprecated $ core_actions_post [] = 'wp-fullscreen-save-post';

// Register core Ajax calls. if (! empty ($ _GET ['action']) & in_array ($ _GET ['action'], $ core_actions_get))     add_action ('wp_ajax_'. $ _GET ['action'], 'wp_ajax_'. str_replace ('-', '_', $ _GET ['action']), 1);

if (! empty ($ _POST ['action']) & in_array ($ _POST ['action'], $ core_actions_post))     add_action ('wp_ajax_'. $ _POST ['action'], 'wp_ajax_'. str_replace ('-', '_', $ _POST ['action']), 1);

add_action ('wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1);

if (is_user_logged_in ()) {     / **      * Fires authenticated Ajax actions for logged-in users.      *      * The dynamic portion of the hook name, $_REQUEST['action'] ,      * refers to the name of the Ajax action callback being fired.      *      * @since 2.1.0      * /     do_action ('wp_ajax_'. $ _REQUEST ['action']); } else {     / **      * Fires non-authenticated Ajax actions for logged-out users.      *      * The dynamic portion of the hook name, $_REQUEST['action'] ,      * refers to the name of the Ajax action callback being fired.      *      * @since 2.8.0      * /     do_action ('wp_ajax_nopriv_'. $ _REQUEST ['action']); } // Default status die ('0');

    
answered by 19.05.2017 в 13:19