简体   繁体   English

按下链接时自动填写表格

[英]Autofill form when link is pressed

I have this webpage. 我有这个网页。 It has a page called "services.php". 它有一个名为“ services.php”的页面。 I have several buttons (made of classes), that belong to different "package" prices i offer. 我有几个按钮(由类组成),属于我提供的不同“包装”价格。

I want the links that say "Select" to autofill a form in another page, or alternativly in a popup form in the page.. I don't really know how to explain it, but as short as possible: 我希望显示“选择”的链接自动在另一个页面中填充表单,或者在页面的弹出窗口中替代表单。.我真的不知道如何解释它,但是要尽可能简短:
When link is pressed autofill form (in this or other page) with the type of package they chose. 当按下链接时,自动填充表格(在此页面或其他页面)及其选择的包装类型。 Only text autofill 仅文字自动填充

What you seem to be asking is 'loading' a page pre-filled with specific information, you can do this a number of ways, either by utilizing javascript (like jQuery for instance). 您似乎要问的是“加载”预先填充有特定信息的页面,您可以通过多种方式来实现此目的,或者利用javascript (例如jQuery)。 Or using your PHP, make links that pass variables (say a flag or a reference to pre-fill the fields -- if you want a popup or next page, etc). 或使用您的PHP,建立传递变量的链接(例如,如果要弹出窗口或下一页等,请使用标志或引用来预填充字段)。

Your url would like like the following for the button that a user presses (button would be a simple http link): 您的网址想要用户按下的按钮的以下内容(按钮将是一个简单的http链接):

http://mywebsite.com/prefill.php?user=bob&package=2

This would have the values bob as the user that requests it (you can reference an id for user info here as well), and package=2 to designate your package options. 请求的用户将具有bob值(您也可以在此处引用用户信息的ID),以及package=2来指定您的软件包选项。

Then on the prefill.php page, you would have something that checks for: 然后在prefill.php页面上,您将检查以下内容:

$user = $_GET['user'];
$package = $_GET['package'];

Hope that helps 希望能有所帮助

This will populate form fields with whatever you pass to the autoFill() function. 这将使用传递给autoFill()函数的任何内容填充表单字段。 This would be a same page example. 这将是同一个页面示例。

<html>
<body>

    <form>
        <input type="text" id="packageDescription">
        <input type="text" id="packagePrice">
    </form>

    <script>

        function autoFill(packageDescription, packagePrice) {

            document.getElementById('packageDescription').value = packageDescription;
            document.getElementById('packagePrice').value = packagePrice;

        }

    </script>

    <a href="#" onClick="autoFill('Premium Package', 150); return false;">Premium Package</a><br>
    <a href="#" onClick="autoFill('Platinum Package', 350); return false;">Platinum Package</a>

</body>
</html>

You could do something like this: 您可以执行以下操作:

<select id="packages">
  <option value="package1">Package 1</option>
  <option value="package2">Package 2</option>
</select>

<a href="javascript:submitPackage();">Submit</a>

When the link is clicked, the following javascript will fire off: 单击链接后,将触发以下javascript:

function submitPackage()
{
  var package = $("#package").val();
   window.open("http://your-site.com/some-script.php?package=" + package);
}

The above will open a pop up window to a page such as this: 上面的操作将打开一个弹出窗口,例如以下页面:

http://your-site.com/some-script.php?package=package1

In some-script.php you will do something like this: 在some-script.php中,您将执行以下操作:

You selected the package: <b><?php echo $_GET['package'];?></b>.

Or: 要么:

<?php
//Put the packages in an array:
$packages = array();
$packages['package1'] = 'Package 1';
$packages['package2'] = 'Package 2';
//...
?>   

<select id="package">
 <?php foreach ($packages as $name => $text):?>
   <? $selected = ($name == $_GET['package']) ? 'selected' : '';?>
    <option value="<? php echo $name;?>" <?php echo $selected;?>> 
       <?php echo $text;?>
   </option>
 <? endforeach;?>
</select>

The above will auto select the package they selected in a dropdown box. 上面的内容将在下拉框中自动选择他们选择的软件包。

if i understood your problem, you want to fill some input fields with information when the user clicks on some links 如果我了解您的问题,则当用户单击某些链接时,您想在某些输入字段中填充信息

i can think of 2 ways of doing this : either have the links point to a page like services.php?req=package1 (or any other url you want) and on that page generate the input fields with the information you need (set the default values in the fields with the ones you want), or, use javascript to change the values of the forms without changing the actual page (either via ajax or predefined values) 我可以想到2种方法:要么使链接指向诸如services.php?req = package1之类的页面(或您想要的任何其他url),然后在该页面上生成包含所需信息的输入字段(设置字段中包含您想要的默认值),或者使用javascript更改表单的值而无需更改实际页面(通过ajax或预定义值)

for javascript you can use the jQuery framework, it has a pretty extensive community of enthusiasts and plenty of examples to get you started with it. 对于javascript,您可以使用jQuery框架,它有一个相当广泛的发烧友社区,并提供了许多示例来帮助您开始使用它。

an example for your case would be 您的情况的一个例子是

$('#btn1').bind('click', function() {
  $('#input1').val("value");
  $('#input2').val("value2");
});

replace btn1 with the id of the first button or link you have, input1 with the id of the first input in your form, and value with the value you want 将btn1替换为您拥有的第一个按钮或链接的ID,将input1替换为表单中第一个输入的ID,并将value替换为所需的值

I've ran several time into the same issue, so I had to write my own script doing this. 我已经多次遇到相同的问题,因此我必须编写自己的脚本来执行此操作。 It's called Autofiller and its pretty simple but does great job. 它被称为自动填充程序 ,非常简单,但是却做得很好。

Here is an example 这是一个例子

http://example.com/?autofiller=1&af=1&pof=package&package=package1

So basically it takes several parameters to init the script: 因此,基本上,需要几个参数来初始化脚本:

autofiller=1 - init AutoFiller autofiller=1初始化AutoFiller

af=1 - Autofill after page is loaded af=1页面加载后自动填充

pof=package - Find the parent form element of the select with name attribute package . pof=package查找带有名称属性packageselect的父form元素。 Works also with input form elements. 也可以与input表单元素一起使用。

package=package1 - Will set the select element's value to package1 package=package1select元素的值设置为package1

Hope it helps you! 希望对您有帮助! :) :)

I just did this myself. 我只是自己做的。 My solution was with jQuery. 我的解决方案是使用jQuery。 Just assign an id to your link. 只需为您的链接分配一个ID。 The first ID in the code is the link id and the second is the id for the input element you want to populate. 代码中的第一个ID是链接ID,第二个是您要填充的输入元素的ID。

Here is the script: 这是脚本:

 <script>

   $(document).ready(function() {

    $('#link_id').click(function() {
        $('#input_id').val( $(this).text() ).keyup();
         return false;
     });

   });

 </script>

Hope it works! 希望它能起作用!

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

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