【问题标题】:retreive html data into form input using submit button使用提交按钮将 html 数据检索到表单输入中
【发布时间】:2018-06-04 09:41:45
【问题描述】:

我创建了一个支付表单,并将数据从数据库检索到输入字段。我的html如下:

<table>
	<form name="postForm" action="form_process.php" method="POST" >
	<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
	<tr><td>amount</td><td><input type="text" name="amount" value="<?php echo $_GET['id'];?>" /></td></tr>



	<tr><td>Name</td><td><input type="text" name="firstname" value="<?php echo $user_name; ?>" /></td></tr>
	<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
	<tr><td>phone</td><td><input type="text" name="mobile" value="<?php echo $user_mobile; ?>" /></td></tr>
	<tr><td>productinfo</td><td><input type="text" name="productinfo" value="" /></td></tr>
	<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
	<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
	<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
	</form>
</table>

我的网站有以下设计供客户选择计划。当用户单击 paynow 按钮时,他们将被重定向到此表单。

当用户点击按钮时,相应的金额应该自动添加到输入字段中。如果它的 400 输入应该是 400 或 300 如果 300。我如何在提交按钮中做到这一点?

【问题讨论】:

  • 哪个输入字段需要值?
  • 试试this
  • @melvin 金额字段
  • 代码中的金额字段在哪里?
  • 第三行。在顶部

标签: javascript php html css


【解决方案1】:

所以你需要在按钮上执行点击功能。您的 3 个按钮可能有一个 data 属性 price 提及值,例如

<input type="button" data-price="300">

通过访问数据属性来填充输入字段。

$('.button').click(function(){
    $('input[name="amount"]').val($(this).data(price));
});

【讨论】:

  • 我应该在哪里添加这个
  • 在你的 js 文件中。
  • 让我检查并告诉
  • 我在他的标签列表中没有看到 jQuery?!
  • 所以?他不知道该怎么做,所以他寻求帮助。还有一个标签javascript。我不知道没有js是否可以@RezaSaadati
【解决方案2】:

您可以将输入类型指定为 Button,例如 plan1、plan2、plan3,并为每个按钮创建 onClick 函数,例如

<button onclick="plan1()">Plan1</button>

<p id="plan1">Plan1 contents</p>

<script>
function plan1() {
  document.getElementById("plan1").innerHTML = "Document Data";
}

明智的

【讨论】:

    【解决方案3】:
    1. 将以下类添加到您的 3 个部分: class="month"

    2. 为该输入字段指定一个您希望输出结果的 ID:例如:&lt;input type="text" name="productinfo" id="productinfo"&gt;

    3. 在您的 JavaScript 文件中添加以下脚本: const month = document.querySelectorAll('.month'); const productInfo = document.getElementById('productinfo'); month.forEach((m) => m.addEventListener('click', (e) => productInfo.value = m.getAttribute('data-month')));

    data-month 约束值。在本例中为 100、200、300。

    const month = document.querySelectorAll('.month');
    const productInfo = document.getElementById('productinfo');
    
    month.forEach((m) => m.addEventListener('click', (e) => productInfo.value = m.getAttribute('data-month')));
    <input class="month" type="button" value="PLAY NOW" data-month="100">
    <input class="month" type="button" value="PLAY NOW" data-month="200">
    <input class="month" type="button" value="PLAY NOW" data-month="300">
    
    <input type="text" id="productinfo">

    【讨论】:

    • 我应该在哪里提供 id=month
    • @kingkhan 对不起,我的错。这不是身份证。应该是一堂课。所以添加到你所有的按钮:class="month".
    • 我已经按照你说的做了,但是价值没有添加到盒子里
    • @kingkhan 您是否将 ID productinfo 添加到输入文本字段中,应该接收该值?
    • @kingkhan 你的 JavaScript 在哪里加载?在&lt;head&gt; 或页脚中?如果不在页脚中,请将其放在页脚中。另外请告诉我你的控制台的错误。
    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多