根据 $_POST 值填充字段 [英] Populate field depending on $_POST value

查看:29
本文介绍了根据 $_POST 值填充字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求有关我尝试填充的表单的帮助.

I am looking for some help with a form I am trying to populate.

我通常会使用 onChange() 函数,但我不认为这是一个选项.

I would usually use an onChange() function, but I don't think that is an option here.

基本上我有一个带有几个隐藏值的

和一个提交按钮.当提交被点击时,它会定向到预订页面,其中 $_POST 值填充了几个字段.

Basically I have a <form> with a couple of hidden values, and a submit button. When submit is hit, it directs to the booking page, where the $_POST values populate a couple of the fields.

<form method="post" action="book.php">
    <input type="hidden" name="title" value="Cardio Fitness">
    <input type="hidden" name="courseID" value="CF">
    <input type="hidden" name="day" value="Wednesday">
    <input type="hidden" name="time" value="9:00pm">
    <input type="submit" Value="Wednesday - 9:00pm">
</form>

book.php 看起来像:

The book.php looks like:

<h2><?php echo $_POST['title'];?></h2>

<form id="booking" onSubmit="return check_book()" method="post" action="#">
    <!-- Course Name -->
    <input type="hidden" name="courseID" id="cID" value="<?php echo $_POST['courseID'];?>"/>
    <!-- Course Day -->
    <input type="text" name="day" id="day" value="<?php echo $_POST['day'];?>" readonly/>
    <!-- Course Time -->
    <input type="text" name="time" id="time" value="<?php echo $_POST['time'];?>" readonly/>

    <!-- Adult Spots -->
    <select name="as" class="1-10">
        <option value="" disabled selected>Adult</option>
    </select>
    <input type="text" class="as_price" readonly/>
    <!-- Child Spots -->
    <select name="cs" class="1-10">
        <option value="" disabled selected>Child</option>
    </select>
    <input type="text" class="cs_price" readonly/>

    <!--Submit Button -->
    <input type="submit" value="Add to Cart"/>

</form>

根据 $_POST 值填充 .as_price 和 .cs_price 字段的最佳方法是什么?JS?

What is the best way to populate the .as_price and .cs_price fields, depending on the $_POST values? JS?

我完全不合时宜吗?

干杯

推荐答案

你的意思是(提交表单之后,加载其他页面之前)?

Do you mean (after submitting the form and before loading the other page)?

然后使用 <?php 并花时间使用您的价格逻辑进行计算.(一次性计算).示例:

Then use <?php and take your time doing the calculations using your price logic. (One-time calculation). Example:

<!-- Adult Spots -->
<select name="as" class="1-10">
    <option value="" disabled selected>Adult</option>
</select>
<?php
// Do a db connection if you want to
// Calculate the price using your logic
$final_price = "$".(10*10); // just an example resulting $100
?>
<input type="text" class="as_price" value="<?php echo $final_price;?>" readonly/>

但是,在您将页面提供给客户端之后,忘记 PHP,它不是更改任何内容的选项.使用 AJAX 在选择更改时向 price-checking-file.php 发送请求,并相应地更新价格.

However, after you serve the page to the client, forget about PHP, it isn't an option to change anything. Use AJAX to send requests to a price-checking-file.php on selection change, and update the price accordingly.

这篇关于根据 $_POST 值填充字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆