如何在不重新加载页面的情况下提交此wordpress表单?

时间:2019-04-09 12:40:42

标签: javascript php jquery wordpress

所以基本上,我需要提交这种无需页面重新加载的快速兴趣幻灯片形式-https://loancalc.000webhostapp.com,这不是我的代码,我对wordpress或php不太熟悉。

在添加完$('。qis-form')。on('submit'...代码之后,一旦我单击“立即申请”,滑块将继续重新加载页面。

我不知道我应该使用什么代码,但是我被告知函数是...

qis-loop (验证并处理表单),qis_process_form也会处理表单并发送电子邮件。

function qis_loop($atts) {

global $post;

// Apply Now Button

if (!empty($_POST['qisapply'])) {
    $settings = qis_get_stored_settings();
    $formvalues = $_POST;
    $url = $settings['applynowaction'];
    if ($settings['applynowquery']) $url = $url.'?amount='.$_POST['loan-amount'].'&period='.$_POST['loan-period'];
    echo "<p>".__('Redirecting....','quick-interest-slider')."</p><meta http-equiv='refresh' content='0;url=$url' />";
    die();

// Application Form

} elseif (!empty($_POST['qissubmit'])) {
    $formvalues = $_POST;
    $formerrors = array();
    if (!qis_verify_form($formvalues, $formerrors)) {
        return qis_display($atts,$formvalues, $formerrors,null);
    } else {
        qis_process_form($formvalues);
        $apply = qis_get_stored_application_messages();
        if ($apply['enable'] || $atts['parttwo']) return qis_display_application($formvalues,array(),'checked');
        else return qis_display($atts,$formvalues, array(),'checked');
    }

// Part 2 Application

} elseif (!empty($_POST['part2submit'])) {
    $formvalues = $_POST;
    $formerrors = array();
    if (!qis_verify_application($formvalues, $formerrors)) {
        return qis_display_application($formvalues, $formerrors,null);
    } else {
        qis_process_application($formvalues);
        return qis_display_result($formvalues);
    }

// Default Display

} else {
    $formname = $atts['formname'] == 'alternate' ? 'alternate' : '';
    $settings = qis_get_stored_settings();
    $values = qis_get_stored_register($formname);
    $values['formname'] = $formname;
    $arr = explode(",",$settings['interestdropdownvalues']);
    $values['interestdropdown'] = $arr[0];
    $digit1 = mt_rand(1,10);
    $digit2 = mt_rand(1,10);
    if( $digit2 >= $digit1 ) {
        $values['thesum'] = "$digit1 + $digit2";
        $values['answer'] = $digit1 + $digit2;
    } else {
        $values['thesum'] = "$digit1 - $digit2";
        $values['answer'] = $digit1 - $digit2;
    }
    return qis_display($atts,$values ,array(),null);
}
}

qis_process_form

function qis_process_form($values) {
global $post;
$content='';
$register = qis_get_stored_register($values['formname']);
$settings = qis_get_stored_settings();
$auto = qis_get_stored_autoresponder();
$qis_messages = get_option('qis_messages');
$application = qis_get_stored_application_messages();
if(!is_array($qis_messages)) $qis_messages = array();

$ip=$_SERVER['REMOTE_ADDR'];
$url = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$page = get_the_title();
if (empty($page)) $page = 'Unknown Page';

$period = $values['loan-period'] == 1 ? $settings['singleperiodlabel'] : $settings['periodlabel'];
if (!$period) $period = $settings['period'];

$values['loan-amount'] = $settings['currency'].$values['loan-amount']; 
$values['loan-period'] = $values['loan-period'].' '.$period; 

$radio = explode(',',$register['radiolist']);

$values['yourradio'] = $radio[$values['radiooption']];

for ($i=1;$i<=3;$i++) {
    if ($values['check'.$i]) $checks .= $register['check'.$i] . '<br>';
}

if ($checks) $values['yourchecks'] .= substr($checks, 0, -4);

$values['sentdate'] = date_i18n('d M Y');
$values['timestamp'] = time();

if ($register['storedata']) {
    $newmessage = array();
    $arr = array(
        'reference',
        'yourname',
        'youremail',
        'yourtelephone',
        'yourmessage',
        'yourchecks',
        'yourradio',
        'yourdropdown',
        'yourconsent',
        'loan-amount',
        'loan-period',
        'confirmation',
        'formname',
        'sentdate',
        'timestamp'

    );

    foreach ($arr as $item) {
        if ($values[$item] != $register[$item]) $newmessage[$item] = $values[$item];
    }

    $qis_messages[] = $newmessage;
    update_option('qis_messages',$qis_messages);
}
if (!$auto['notification']) {
    qis_send_notification ($values);
}

if (($auto['enable'] || $values['qis-copy']) && !$application['enable']) {
    qis_send_confirmation ($auto,$values,$content,$register);
}

if ($register['qis_redirect_url']) {
    $location = $register['qis_redirect_url'];
    echo "<meta http-equiv='refresh' content='0;url=$location' />";
    exit;
}}

数据已在 qis_verify_application

中验证
function qis_verify_application(&$values, &$errors) {
$application = qis_get_stored_application();
$register = qis_get_stored_application_messages();

$arr = array_map('array_shift', $application);
foreach ($arr as $key => $value) {
    if ($application[$key]['type'] == 'multi') {
        $d = explode(",",$application[$key]['options']);
            foreach ($d as $item) {
                $values[$key] .= $values[$key.$item];
            }
    }
    if ($application[$key]['required'] == 'checked' && $register['use'.$application[$key]['section']] && (empty($values[$key]) || $values[$key] == 'Select...')) 
        $errors[$key] = 'error';
    }

$filenames = array('identityproof','addressproof');

foreach($filenames as $item) {
    $tmp_name = $_FILES[$item]['tmp_name'];
    $name = $_FILES[$item]['name'];
    $size = $_FILES[$item]['size'];
    if (file_exists($tmp_name)) {
        if ($size > $register['attach_size']) $errors['attach'.$item] = $register['attach_error_size']; 
        $ext = strtolower(substr(strrchr($name,'.'),1));
        if (strpos($register['attach_type'],$ext) === false) $errors['attach'.$item] = $register['attach_error_type'];
    }
}
return (count($errors) == 0);   
}

如果通过验证,则将在 qis_process_application 中处理该表单。

function qis_process_application($values) {
global $post;
$content='';
$register = qis_get_stored_register ('default');
$applicationmessages = qis_get_stored_application_messages();
$settings = qis_get_stored_settings();
$auto = qis_get_stored_autoresponder();
$application = qis_get_stored_application();
$message = get_option('qis_messages');

$arr = array_map('array_shift', $application);

if ($message) {
    $count = count($message);
    for($i = 0; $i <= $count; $i++) {
    if ($message[$i]['reference'] == $values['reference']) {
        $values['complete'] = 'Completed';
        $message[$i] = $values;
        update_option('qis_messages',$message);
        }
    }    
}

$filenames = array('identityproof','addressproof');

$attachments = array();
if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
add_filter( 'upload_dir', 'qis_upload_dir' );

$dir = (realpath(WP_CONTENT_DIR . '/uploads/qis/') ? '/uploads/qis/' : '/uploads/');
foreach($filenames as $item) {
    $filename = $_FILES[$item]['tmp_name'];
    if (file_exists($filename)) {
        $name = $values['reference'].'-'.$_FILES[$item]['name'];
        $name = trim(preg_replace('/[^A-Za-z0-9. ]/', '', $name));
        $name = str_replace(' ','-',$name);
        $_FILES[$item]['name'] = $name;
        $uploadedfile = $_FILES[$item];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
        array_push($attachments , WP_CONTENT_DIR .$dir.$name);
    }
}

remove_filter( 'upload_dir', 'qis_upload_dir' );

$content = qis_build_complete_message($values,$application,$arr,$register);

qis_send_full_notification ($register,$values,$content,true,$attachments);

qis_send_full_confirmation ($auto,$values,$content,$register);
}

我在这里拨打了ajax电话

jQuery('.qis-form').on('submit', function(event){
    event.preventDefault();
    var name = $("input#yourname").val();
    var email = $("input#youremail").val();
    if (name == ""){
        $("input#yourname").focus;
        return false;
    }

    else if (email == ""){
        $("input#youremail").focus;
        return false;
    }
    else{
        jQuery.ajax({
           type: "POST",
           url: "quick-interest-slider.php",
           data: {
                name:name,
                email:email,
                qissubmit:$(".qissubmit").val(),
                qisapply:$(".qisapply").val(),
                part2submit:$(".part2submit").val(),
     },
           done: function(msg){
                console.log(msg);
        }
    });
}

});

1 个答案:

答案 0 :(得分:0)

搜寻完html后,您将需要在APPLY上单击NOT form form来调用它。

jQuery(document).on('click','.toggle-qis a', function(event){
    var name = $("input#yourname").val();
    var email = $("input#youremail").val();
    if (name == ""){
        $("input#yourname").focus;
    }

    else if (email == ""){
        $("input#youremail").focus;
    }
    else{
        jQuery.ajax({
           type: "POST",
           url: "quick-interest-slider.php",
           data: {
                name:name,
                email:email,
                qissubmit:$(".qissubmit").val(),
                qisapply:$(".qisapply").val(),
                part2submit:$(".part2submit").val(),
     },
           done: function(msg){
                console.log(msg);
        }
    });
}

return false;

});