javascript - php代码不获取表单输入值?

标签 javascript php html

我在 html 文档中有一个表单,我想做的是,当有人提交此表单时,它会转到文件“total.php”,并在我计算的total.php文件中(值的乘法和结果反射(reflect)在 html 页面上,但它不起作用。)

这是 html 和 javascript 代码

表单的工作:

当用户选择他的级别时,假设为 1,那么其他两个字段将使用此代码中提到的 JavaScript 自动填充,因此对于级别 1,其他两个字段将被填充为

没有。人 = 3,人的收入 = $5 。之后有一个名为计算收入的按钮,一旦用户单击该按钮,它就会转到文件total.php,作为“action of form =total.php”

<script>
             function getPerson(select) {
  var form = select.form;
  form.ref_person.value = select.options[select.selectedIndex].getAttribute('per');
  form.earn_person.value = select.value;}                            
            </script>
                        <form id="calc" method="post" action="total.php" target="total">
                          <fieldset>
                          <legend>Person and earning</legend>
                          <label class="fhead">CHOOSE YOUR LEVEL :</label> 
                          <select name="choose" id="personSelect" onchange="getPerson(this)">
                                    <option value="" selected>Choose</option>
                                  <option per="3" value="$5">1</option>
                                  <option per="9" value="$4">2</option>
                                  <option per="27" value="$4">3</option>
                          </select> 
                           <br>
                          <label  class="fhead" for="personNumber">No. Of person:</label>
                          //first  input field
                              <input type="text" name="ref_person"id="personNumber" class="field"  readonly="readonly">
                            <br />

                          <label  class="fhead" for="personEarning">Earning of person:</label>
                              //second  input field
                            <input type="text" name="earn_person" id="personEarning"  class="field"  readonly="readonly">
                          </fieldset>

                          <input style=" float:right; margin-right:90px;"  name="submit" value="Calculate Earnings" type="submit"> 

                        </form>

                                //This iframe tag helps in reflecting the result of the calculation I did in total.php .

                             <iframe src="total.php" name="total" width="100%"frameborder="0"></iframe>   

这是我的total.php代码

现在,在总共 php 中,我获取了表单的输入值来计算内容,其结果应该反射(reflect)在 html 页面上,但不幸的是它不是工作环。

我不知道我哪里错了。

//fetching input values from form
<?php
$ref_person=$_POST['ref_person'];
$earn_person=$_POST['earn_person'];

//required calcualtion    
$total =$ref_person * $earn_person;
$monthly = $total; 
$yearly = $monthly * 12;
?> 

<This is start tr tag>
    <this is td tag>
    <b style="color:#FFF">Monthly Income: $<?php if (empty($total) || $total == 0 || $total == 0.00) { echo "0"; } 
    else { echo "$monthly"; } ?>.00</b>
<This is end of td tag >

</This is end of tr tag>

<This is start  tr tag >

    <This is td tag >
    <b style="color:#FFF">Yearly Income: $<?php if (empty($total) || $total == 0 || $total == 0.00) { echo "0"; } 
    else { echo "$yearly"; } ?>.00</b>
<This is end of td tag >

</This is end of tr tag>

最佳答案

令人惊讶的是,javascript 函数似乎可以工作,所以请接受我对它的怀疑的歉意。我认为您可以将 total.php 代码更改为:

<?php
    $ref_person=intval( $_POST['ref_person'] );
    $earn_person=intval( $_POST['earn_person'] );

    $total = $ref_person * $earn_person;
    $monthly = $total; 
    $yearly = $monthly * 12;

    /* you didn't return or echo any value */
    echo $yearly > 0 && !empty( $yearly ) ? 'Total Yearly: $'.$yearly : '';
?>

我在测试中更改了如下所示的 js 函数:

        function getPerson(n) {
            var form = n.parentNode.parentNode;
            form.ref_person.value = n.options[ n.options.selectedIndex ].getAttribute('per');
            form.earn_person.value = n.value;
        }

关于javascript - php代码不获取表单输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465903/

相关文章:

javascript - 如何创建默认在新窗口打开的浏览器书签?

javascript - 通过href提交Form并传递隐藏变量

php - 需要 MySQL、PHP 帮助

PHP 将秒转换为 MM :DD:HH:MM:SS

html - 如何在移动版中更改对象的宽度

javascript - 如何使用 javascript/jquery 来防止将小数点输入到 HTML 文本字段中?

javascript - 为什么用 p.then(resolve) 比用 resolve(p) 更早地解决新的 promise ?

javascript - 附加任何元素时 Div 高度不会增加

php - opencart 2.0 结帐流程中缺少确认订单按钮

html - Flexbox:使用 Flexbox 将页眉和页脚移动到侧边栏