用onsubmit="return false;"来禁止自动提交

本文介绍了一种方法来阻止HTML表单在用户完成输入并按下回车键后的自动提交行为。通过在FORM标签中添加onsubmit属性设置return false;即可实现这一功能。

今天发现在FORM中只有一个INPUT时,输入完成后回车会自动提交表单,加入onsubmit="return false;"可以防止此动作,如下:

 

<form name="form1" method="post" onsubmit="return false;">­

 

<?php /* Plugin Name: 手动输入表单 Plugin URI: http://192.168.5.67/qiu Description: 提供一个短代码 [manual_input_form],用于提交品种、仓位、方向和生效时间,并写入数据库。总仓位不能超过100%。支持品种校验、删除行及生效时间选择。已移除“仓位重置”功能。新增:禁止重复提交相同品种(不区分大小写)。 Version: 2.7 Author: zhl License: GPL2 */ if (!defined('ABSPATH')) { exit; // 防止直接访问 } // 注册激活插件时创建主表 register_activation_hook(__FILE__, 'mif_create_shoudong_table'); function mif_create_shoudong_table() { global $wpdb; $table_name = $wpdb->prefix . 'shoudong'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id INT AUTO_INCREMENT PRIMARY KEY, var VARCHAR(255) NOT NULL, cangwei FLOAT NOT NULL, fangxiang INT NOT NULL, effect_time DATETIME NULL DEFAULT NULL ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } // 插件加载时检查是否需要升级表结构(添加 effect_time 字段) function mif_update_shoudong_table() { global $wpdb; $table_name = $wpdb->prefix . 'shoudong'; $column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'effect_time'"); if (!$column_exists) { $sql = "ALTER TABLE $table_name ADD COLUMN effect_time DATETIME NULL DEFAULT NULL;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } } mif_update_shoudong_table(); // 每次加载插件时检查一次 // 短代码处理函数 function manual_input_form_shortcode() { global $wpdb; $message = ''; $show_data = false; $show_zhuli = false; $table_name = $wpdb->prefix . 'shoudong'; $zhuli_table = 'zhuli'; // 不使用 wp_ 前缀 // 处理单行删除 if (isset($_POST['delete_id'])) { check_admin_referer('delete_shoudong_row'); // 安全验证 $delete_id = intval($_POST['delete_id']); $result = $wpdb->delete($table_name, array('id' => $delete_id), array('%d')); if ($result) { $message = '数据删除成功!'; } else { $message = '删除失败:未找到该记录。'; } } // 数据提交处理 if (isset($_POST['submit_data'])) { $var = sanitize_text_field($_POST['var']); // 品种 $cangwei_input = sanitize_text_field($_POST['cangwei']); // 仓位输入 $fangxiang = intval($_POST['fangxiang']); // 方向 $effect_time = !empty($_POST['effect_time']) ? sanitize_text_field($_POST['effect_time']) : null; // 生效时间 // 校验时间格式 (Y-m-d\TH:i) if ($effect_time && !DateTime::createFromFormat('Y-m-d\TH:i', $effect_time)) { $message = '生效时间格式不正确,请选择有效的时间。'; } else { // 处理仓位输入:允许百分比或小数 $cangwei = 0.0; if (strpos($cangwei_input, '%') !== false) { $cangwei = floatval(str_replace('%', '', $cangwei_input)) / 100; } else { $cangwei = floatval($cangwei_input); } if (empty($var)) { $message = '品种不能为空'; } elseif ($cangwei <= 0) { $message = '仓位必须大于0'; } else { // 检查 zhuli 表是否存在 $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$zhuli_table'") == $zhuli_table; if (!$table_exists) { $message = "错误:数据库中不存在名为 '$zhuli_table' 的表,请联系管理员。"; $show_zhuli = true; } else { // 品种校对:检查 pinzhong_E 是否存在(不区分大小写) $exists = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $zhuli_table WHERE UPPER(pinzhong_E) = UPPER(%s)", $var )); if (!$exists) { $message = "品种校验失败:'$var' 不存在于可交易品种列表中,请参考下方完整品种列表。"; $show_zhuli = true; } else { // === 新增:防止品种重复提交(不区分大小写)=== $existing_row = $wpdb->get_row($wpdb->prepare( "SELECT * FROM $table_name WHERE UPPER(var) = UPPER(%s)", $var ), ARRAY_A); if ($existing_row) { $existing_cangwei = number_format($existing_row['cangwei'] * 100, 2) . '%'; $existing_fangxiang = esc_html($existing_row['fangxiang']); $existing_time = $existing_row['effect_time'] ? esc_html(date_i18n('Y-m-d H:i', strtotime($existing_row['effect_time']))) : '未设置'; $message = "提交被驳回:品种 '$var' 已存在!" . " 当前仓位:$existing_cangwei," . " 方向:$existing_fangxiang," . " 生效时间:$existing_time。" . " 请先删除旧记录再提交新数据。"; $show_data = true; // 自动显示现有数据便于查看 } else { // 获取当前总仓位(只有非重复才检查总和) $total_cangwei = $wpdb->get_var("SELECT SUM(cangwei) FROM $table_name"); $total_cangwei = floatval($total_cangwei); if ($total_cangwei + $cangwei > 1.0) { $message = '总仓位不能超过100%!当前总仓位为:' . number_format($total_cangwei * 100, 2) . '%'; $show_data = true; } else { // 插入数据(含生效时间) $result = $wpdb->insert( $table_name, array( 'var' => $var, 'cangwei' => $cangwei, 'fangxiang' => $fangxiang, 'effect_time' => $effect_time ? $effect_time . ':00' : null // 添加秒 ), array('%s', '%f', '%d', '%s') ); if ($result) { $message = '数据提交成功!'; } else { $message = '数据提交失败,请重试。'; } } } } } } } } // 显示/隐藏数据操作 if (isset($_POST['toggle_data'])) { $show_data = isset($_POST['show_data']) && $_POST['show_data'] == '1'; } ob_start(); ?> <style> .manual-form { max-width: 800px; margin: 0 auto; font-family: Arial, sans-serif; } .manual-form p { margin-bottom: 15px; } .manual-form label { display: block; margin-bottom: 5px; font-weight: bold; } .manual-form input[type="text"], .manual-form input[type="number"], .manual-form input[type="datetime-local"] { width: 100%; padding: 8px; box-sizing: border-box; font-size: 14px; border: 1px solid #ccc; border-radius: 4px; } .manual-form input[type="submit"][name="submit_data"] { padding: 10px 20px; font-size: 18px; background-color: #4CAF50; color: white; border: none; cursor: pointer; border-radius: 4px; width: 100%; } .manual-form input[type="submit"][name="submit_data"]:hover { background-color: #45a049; } .manual-form form { background-color: #f9f9f9; padding: 20px; border-radius: 6px; } .manual-form table { width: 100%; border-collapse: collapse; margin-top: 10px; } .manual-form table th, .manual-form table td { border: 1px solid #ccc; padding: 8px; text-align: left; } .manual-form .action-buttons { margin-top: 10px; } /* 统一按钮样式:显示/隐藏、删除 */ .manual-form input[type="submit"][name="toggle_data"], .manual-form .delete-row-button { padding: 6px 10px; font-size: 14px; border: none; border-radius: 4px; cursor: pointer; margin-right: 5px; } .manual-form input[type="submit"][name="toggle_data"] { background-color: #2196F3; color: white; } .manual-form .delete-row-button { background-color: #f44336; color: white; } .manual-form .delete-row-button:hover, .manual-form input[type="submit"][name="toggle_data"]:hover { opacity: 0.9; } .manual-form .delete-form { display: inline-block; } </style> <div class="manual-form"> <?php if ($message): ?> <p style="color: red;"><?php echo esc_html($message); ?></p> <?php endif; ?> <!-- 提交表单 --> <form method="post" action=""> <p> <label>品种: <input type="text" name="var" required> </label> </p> <p> <label>仓位 (支持百分比,如 30%): <input type="text" name="cangwei" placeholder="例如:30%" required> </label> </p> <p> <label>方向 (1为买,-1为卖): <input type="number" name="fangxiang" min="-1" max="1" required> </label> </p> <p> <label>生效时间: <input type="datetime-local" name="effect_time" value=""> </label> </p> <p> <input type="submit" name="submit_data" value="提交"> </p> </form> <!-- 控制按钮:仅保留显示/隐藏 --> <form method="post" action="" style="margin-top: 20px;" class="action-buttons"> <input type="hidden" name="show_data" id="show_data_input" value="<?php echo $show_data ? '1' : '0'; ?>"> <input type="submit" name="toggle_data" value="<?php echo $show_data ? '隐藏数据' : '显示全部'; ?>"> </form> <!-- 可交易品种参考表 --> <?php if ($show_zhuli): ?> <div id="zhuli-table" style="margin-top: 20px;"> <h3>可交易品种列表(请参考)</h3> <?php $zhuli_results = $wpdb->get_results("SELECT * FROM $zhuli_table", ARRAY_A); if ($zhuli_results && is_array($zhuli_results) && count($zhuli_results) > 0): ?> <table> <thead> <tr> <?php foreach (array_keys($zhuli_results[0]) as $field): ?> <th><?php echo esc_html($field); ?></th> <?php endforeach; ?> </tr> </thead> <tbody> <?php foreach ($zhuli_results as $row): ?> <tr> <?php foreach ($row as $value): ?> <td><?php echo esc_html($value); ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p>可交易品种表为空,请确认表中已有数据。</p> <?php endif; ?> </div> <?php endif; ?> <!-- 当前数据列表 --> <?php if ($show_data): ?> <div id="data-table" style="margin-top: 20px;"> <h3>当前数据列表</h3> <?php $results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY id DESC", ARRAY_A); if ($results && is_array($results) && count($results) > 0): ?> <table> <thead> <tr> <th>ID</th> <th>品种</th> <th>仓位 (%)</th> <th>方向</th> <th>生效时间</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($results as $row): ?> <tr> <td><?php echo esc_html($row['id']); ?></td> <td><?php echo esc_html($row['var']); ?></td> <td><?php echo esc_html(number_format($row['cangwei'] * 100, 2) . '%'); ?></td> <td><?php echo esc_html($row['fangxiang']); ?></td> <td> <?php echo $row['effect_time'] ? esc_html(date_i18n('Y-m-d H:i', strtotime($row['effect_time']))) : '-'; ?> </td> <td> <form method="post" class="delete-form" onsubmit="return confirm('确定要删除这条数据吗?');"> <input type="hidden" name="delete_id" value="<?php echo esc_attr($row['id']); ?>"> <?php wp_nonce_field('delete_shoudong_row'); ?> <input type="submit" value="删除" class="delete-row-button"> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p>当前没有数据。</p> <?php endif; ?> </div> <?php endif; ?> </div> <!-- JavaScript:设置默认生效时间为当前时间 --> <script> document.addEventListener('DOMContentLoaded', function () { const dateTimeInput = document.querySelector('input[name="effect_time"]'); const toggleButton = document.querySelector('[name="toggle_data"]'); const showDataInput = document.getElementById('show_data_input'); // 设置默认时间为当前时间 if (dateTimeInput) { const now = new Date(); const pad = (n) => n.toString().padStart(2, '0'); const year = now.getFullYear(); const month = pad(now.getMonth() + 1); const day = pad(now.getDate()); const hours = pad(now.getHours()); const minutes = pad(now.getMinutes()); dateTimeInput.value = `${year}-${month}-${day}T${hours}:${minutes}`; } // 切换数据显示状态 if (toggleButton && showDataInput) { toggleButton.addEventListener('click', function () { showDataInput.value = showDataInput.value === '0' ? '1' : '0'; }); } }); </script> <?php return ob_get_clean(); } add_shortcode('manual_input_form', 'manual_input_form_shortcode'); 参考以上的wordpress插件短代码,重新生成一个短代码插件。后台数据库重新生成一个对应新的,代码也换一个新的。其余的核查是否重复品种输入、仓位超标等都不变。
最新发布
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值