【问题标题】:How to populate text fields when radio button is clicked?单击单选按钮时如何填充文本字段?
【发布时间】:2013-04-01 21:29:24
【问题描述】:

我正在创建一个网页,其中有两个 div(帐单详细信息和运输详细信息)。加载页面后,会自动显示帐单详细信息,并且运输详细信息保持为空。我包含了两个单选按钮,允许用户选择运输细节是否与账单细节相同。如果用户从单选按钮中选择是,那么相同的详细信息应显示在运输详细信息中。 注意:详细信息存储在数据库中是使用php来获取显示的数据

目前,我只尝试过使用

<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>

在名字字段上只是为了看看它是否工作,但我似乎没有工作。

<div id="leftprofile">
    <form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
    <fieldset class="billing">
        <legend>Billing Details</legend><br />
        <label for="fname" class="reglabel">First Name:</label>
        <input required name="fname" type="text" id="fname" value="<?php echo $fname ?>"/><br />
        <label for="lname" class="reglabel">Last Name:</label>
        <input required name="lname" type="text" id="lname" value="<?php echo $lname ?>"/><br />
        <label for="address" class="reglabel">Address:</label>
        <input required name="address" id="address" type="text" value="<?php echo $address ?>"/><br />
        <label for="town" class="reglabel">Town:</label>
        <input required name="town" id="town" type="text" value="<?php echo $town ?>"/><br />
        <label for="postcode" class="reglabel">Post Code:</label>
        <input required name="postcode" id="postcode" type="text" value="<?php echo $postcode ?>"/><br />
        <label for="phone" class="reglabel">Phone:</label>
        <input required name="phone" id="phone" type="text" value="<?php echo $phone ?>"/><br />
        <label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
        <input required name="email" type="email" id="email" value="<?php echo $email ?>"/><br />
    </fieldset>
    </form>
</div>

<div id="rightprofile">
    <form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
    <fieldset class="billing">
        <legend>Shipping Details</legend><br />
        <form>
            Same as billing address?
            <input type="radio" name="shipping" id="yes" value="yes">Yes
            <input type="radio" name="shipping" id="no" value="no">No<br/>
        </form>
        <label for="fname" class="reglabel">First Name:</label>
        <input required name="fname" type="text" id="fname" value="<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>"/><br />
        <label for="lname" class="reglabel">Last Name:</label>
        <input required name="lname" type="text" id="lname" /><br />
        <label for="address" class="reglabel">Address:</label>
        <input required name="address" id="address" type="text" /><br />
        <label for="town" class="reglabel">Town:</label>
        <input required name="town" id="town" type="text" /><br />
        <label for="postcode" class="reglabel">Post Code:</label>
        <input required name="postcode" id="postcode" type="text" /><br />
        <label for="phone" class="reglabel">Phone:</label>
        <input required name="phone" id="phone" type="text" /><br />
        <label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
        <input required name="email" type="email" id="email" /><br />
    </fieldset>
    </form>
</div>

【问题讨论】:

  • 好吧,账单和运输上的输入名称或相同,所以当您最终提交表单时会遇到问题。此外,这需要在 Javascript 中完成,因为它需要在提交表单之前进行处理。 (还应该注意,一次只能提交一个表单,而现在您有两个单独的表单可以提交到同一个位置。)
  • 它们是不同的 div 但文本字段是相同的
  • 正是我的观点。如果都将提交到同一个action,则需要区分帐单和运费字段name。
  • 一方面,Jon 是对的,您需要唯一的名称。二,天哪,这是谷歌的第一个结果。 stackoverflow.com/questions/8192836/…
  • 据我了解您要制作的内容的概念,我相信它最适合仅使用一种形式,而不是两种形式。我还看到action URL 是相同的,因此将这两个表单合并为一个表单似乎很自然。正如 Jon 所指出的,&lt;input&gt; 字段中的所有 name 属性都必须是唯一的,以便您可以在 PHP 脚本中获取适当的数据。

标签: php text radio-button echo


【解决方案1】:

我已经为您编写了一些简化的示例代码。该站点包含一个表单,具有两个输入字段(帐单和运输),以及一个用于检查运输信息是否与帐单信息相同的复选框。如果选中该复选框,代码将简单地忽略输入到“运输”中的任何内容。

至少从 PHP 的角度来看,这将实现您的要求。如果您正在寻找更多在浏览器中实时“将这些输入字段中的数据复制到这些输入字段”,那么这是 Javascript 的任务,而不是 PHP。

<?php
/* Check if anything was submitted */
if(isset($_POST))
{
    /* Retrieve billing information */
    $billing_name = $_POST['billing_name'];
    $billing_addr = $_POST['billing_addr'];

    /* Check if shipping same as billing */
    if(isset($_POST['same']))
    {
        /* Shipping is the same as billing */
        $shipping_name = $billing_name;
        $shipping_addr = $billing_addr;
    }
    /* If not, set shipping to the posted value */
    else
    {
        $shipping_name = $_POST['shipping_name'];
        $shipping_addr = $_POST['shipping_addr'];
    }

    $insert = mysql_query(...);
}
?>

<form method="post" action="#" />

Billing information
<label for="billing_name">Name</label>
<input type="text" id="billing_name" name="billing_name" />
<label for="billing_addr">Addr</label>
<input type="text" id="billing_addr" name="billing_addr" />

<label for="same" />Is the shipping information the same as billing information?</label>
<input type="checkbox" id="same" name="same" />

Shipping information
<label for="shipping_name">Name</label>
<input type="text" id="shipping_name" name="shipping_name" />
<label for="shipping_addr">Addr</label>
<input type="text" id="shipping_addr" name="shipping_addr" />

<input type="submit" value="Register" />

</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多