简体   繁体   English

通过URL参数自动填充输入表单字段

[英]Auto-populate input form field from URL parameters

I have a button on a woocommerce page ( http://.../product/abcd/ ) that has a button "Book Appointments Now" that links to another page with form fields to submit ( http://.../booking-appointments/?id=1238&sku=1 ) which has ProductID and SKU on the URL of that product from the previous page. 我在woocommerce页面( http://.../product/abcd/ )上有一个按钮,该按钮上有一个“立即预订书”按钮,该按钮链接到包含表单字段以提交的另一页面( http://.../booking-appointments/?id=1238&sku=1 ),该产品在上一页的产品URL上具有ProductIDSKU

I want it to auto-populate the ProductID field automatically each time this page loads from the URL parameters. 我希望它每次从URL参数加载此页面时自动自动填充ProductID字段。

Any help is appreciated. 任何帮助表示赞赏。

You can pass the product ID and SKU in the URL and get it via $_GET . 您可以在URL中传递产品ID和SKU,并通过$_GET Then load it into the input field. 然后将其加载到输入字段中。 Here's an example: 这是一个例子:

http://www.example.com/?product-id=123&sku=abcd456

Then use $_GET to get the URL parameters: 然后使用$_GET获取URL参数:

$productId = $_GET['product-id'];
$sku = $_GET['sku'];

Your input field would be like this: 您的输入字段如下所示:

<label for="product-id">Product ID:</label> <input type="text" name="product-id" id="product-id" value="<?php echo $productId; ?>" disabled>

I even added a disabled attribute to prevent the user/customer from editing the product ID. 我什至添加了disabled属性,以防止用户/客户编辑产品ID。 You can use PHP to secure it from the backend when the form gets submitted. 提交表单后,您可以使用PHP从后端保护它。

To auto populate the product ID and the product SKU in your booking form (page), you should try this (without any guaranty as it can be tested for real on your page form) : 要在您的预订表格(页面)中自动填充产品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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的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 产品ID和产品sku需要在url中传递,例如: http//www.example.com/booking-page/?id=124&&sku=AH1584

And your <imput> text fields should be something like: 并且您的<imput>文本字段应类似于:

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

So for the product sku is something similar, that you will be able to add with ease… 因此,对于产品sku来说,它是相似的,您可以轻松添加…

But as javascript is already very active in you "Easy Appointments" booking form, I don't know if this will work. 但是,由于javascript在您“轻松约会”预订表中已经非常活跃,我不知道这是否行得通。


This answer is related to: Add custom "Booking" button to WooCommerce Product single pages 该答案与以下内容有关: 向WooCommerce产品单页添加自定义“预订”按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM