php - 从 URL 参数自动填充输入表单字段

标签 php jquery wordpress woocommerce custom-fields

我在 woocommerce 页面 (http://.../product/abcd/) 上有一个按钮,其中有一个按钮“立即预订”,该按钮链接到另一个带有表单字段的页面提交 (http://.../booking-appointments/?id=1238&sku=1),其 URL 上包含 ProductIDSKU上一页中该产品的信息。

我希望每次从 URL 参数加载此页面时,它都能自动填充 ProductID 字段。

感谢任何帮助。

最佳答案

要在您的预订表单(页面)中自动填充产品 ID 和产品 SKU,您应该尝试此操作(没有任何保证,因为它可以在您的页面表单上进行真实测试):

add_action( 'wp_footer', 'autopopulate_product_id_script' );
function autopopulate_product_id_script() {
    if( isset( $_GET['id'] ) ):
        ?>
        <script type="text/javascript">
        (function($){
            $('input[name="productid"]').val("<?php echo $_GET['id']; ?>");
            $('input[name="productsku"]').val("<?php echo $_GET['sku']; ?>");
        })(jQuery);
        </script>
        <?php
    endif;
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

The product id and the product sku need to be passed in url something like: http//www.example.com/booking-page/?id=124&&sku=AH1584

还有你的<imput>文本字段应该类似于:

<input type="text" name="productid" val="">
<input type="text" name="productsku" val="">

因此,产品 sku 是类似的东西,您可以轻松添加......

But as javascript is already very active in you "Easy Appointments" booking form, I don't know if this will work.

<小时/>

此答案与:Add custom "Booking" button to WooCommerce Product single pages相关

关于php - 从 URL 参数自动填充输入表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146803/

相关文章:

php - 无法使用 PHPWord TemplateProcessor 下载 word docx

php - 使用 PHP 进行与环境无关的 301/302 重定向

php - 如果特定列中存在某个值则更新记录,否则插入新值

javascript - IE 中的 Ajax 请求 "Access is denied"

javascript - url字符串在jquery中被 chop ?

css - 固定 Visual Composer 行位置

php - Artisan 命令的自定义错误和异常处理程序

javascript - 如何修复数据表数据被更新或者破坏表的情况?

mysql - 我需要更改数百篇 wordpress 文章中的 url。帮助?

php - Wordpress:在表中搜索元素