将javascript值传递给php并发布它(同一个文件)

时间:2013-09-27 11:25:40

标签: javascript php

我有一个名为sendform.php的文件,其中包含一个POST数据并通过邮件发送的表单。

现在我有一个javascript函数,它可以选择4个不同选择字段的选项。

        <script language="javascript">
        function Calculate()
        {
            var h = document.getElementById('bedrag-bol').value;
            var l = document.getElementById('aantal-bol').value;
            var h2 = document.getElementById('bedrag-gamma').value;
            var l2 = document.getElementById('aantal-gamma').value;
            var h3 = document.getElementById('bedrag-douglas').value;
            var l3 = document.getElementById('aantal-douglas').value;
            var h4 = document.getElementById('bedrag-bartsmit').value;
            var l4 = document.getElementById('aantal-bartsmit').value;
            var result = h * l * 10 + h2 *l2 * 10 + h3 * l3 * 10 + h4 * l4 * 10;
            document.getElementById('totaal').innerHTML = result;
        }
    </script>
                    <form name="aanvraag" method="post" action="index.php?p=<?php echo $_GET['p']; ?>&amp;action=checkform">
                    <table width="700" cellpadding="3" cellspacing="3">
    <tr>
        <td><img src="bestanden/spaaractie/bol.png"  /></td>
        <td><img src="bestanden/spaaractie/gamma.png"  /></td>
        <td><img src="bestanden/spaaractie/douglas.png"  /></td>
        <td><img src="bestanden/spaaractie/bartsmit.png"  /></td>
    </tr>
    <tr>
        <td><select id="bedrag-bol" name="bedrag-bol" onChange="Calculate();">
                <option value="0" selected="selected">Kies uw bedrag</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bol']) == "5") {echo "selected='selected'"; } ?>>&euro; 5,00</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bol']) == "10") {echo "selected='selected'"; } ?>>&euro; 10,00</option>
                <option value="25" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bol']) == "25") {echo "selected='selected'"; } ?>>&euro; 25,00</option>
                <option value="50" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bol']) == "50") {echo "selected='selected'"; } ?>>&euro; 50,00</option>
            </select>
        </td>
        <td><select id="bedrag-gamma" name="bedrag-gamma" onChange="Calculate();">
                <option value="0" selected="selected">Kies uw bedrag</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-gamma']) == "5") {echo "selected='selected'"; } ?>>&euro; 5,00</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-gamma']) == "10") {echo "selected='selected'"; } ?>>&euro; 10,00</option>
                <option value="25" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-gamma']) == "25") {echo "selected='selected'"; } ?>>&euro; 25,00</option>
                <option value="50" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-gamma']) == "50") {echo "selected='selected'"; } ?>>&euro; 50,00</option>
            </select>
        </td>
        <td><select id="bedrag-douglas" name="bedrag-douglas" onChange="Calculate();">
                <option value="0" selected="selected">Kies uw bedrag</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-douglas']) == "10") {echo "selected='selected'"; } ?>>&euro; 10,00</option>
                <option value="25" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-douglas']) === "25") {echo "selected='selected'"; } ?>>&euro; 25,00</option>
                <option value="50" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-douglas']) == "50") {echo "selected='selected'"; } ?>>&euro; 50,00</option>
            </select>
        </td>
        <td><select id="bedrag-bartsmit" name="bedrag-bartsmit" onChange="Calculate();">
                <option value="0" selected="selected">Kies uw bedrag</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bartsmit']) == "5") {echo "selected='selected'"; } ?>>&euro; 5,00</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bartsmit']) === "10") {echo "selected='selected'"; } ?>>&euro; 10,00</option>
                <option value="25" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bartsmit']) == "25") {echo "selected='selected'"; } ?>>&euro; 25,00</option>
                <option value="50" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['bedrag-bartsmit']) == "50") {echo "selected='selected'"; } ?>>&euro; 50,00</option>
            </select>
        </td>
    </tr>
    <tr>
        <td><select id="aantal-bol" name="aantal-bol" onChange="Calculate();">
                <option value="0" selected="selected">Kies het aantal</option>
                <option value="1" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "1") {echo "selected='selected'"; } ?>>1</option>
                <option value="2" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "2") {echo "selected='selected'"; } ?>>2</option>
                <option value="3" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "3") {echo "selected='selected'"; } ?>>3</option>
                <option value="4" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "4") {echo "selected='selected'"; } ?>>4</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "5") {echo "selected='selected'"; } ?>>5</option>
                <option value="6" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "6") {echo "selected='selected'"; } ?>>6</option>
                <option value="7" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "7") {echo "selected='selected'"; } ?>>7</option>
                <option value="8" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "8") {echo "selected='selected'"; } ?>>8</option>
                <option value="9" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "9") {echo "selected='selected'"; } ?>>9</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bol']) == "10") {echo "selected='selected'"; } ?>>10</option>
            </select>
        </td>
        <td><select id="aantal-gamma" name="aantal-gamma" onChange="Calculate();">
                <option value="0" selected="selected">Kies het aantal</option>
                <option value="1" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "1") {echo "selected='selected'"; } ?>>1</option>
                <option value="2" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "2") {echo "selected='selected'"; } ?>>2</option>
                <option value="3" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "3") {echo "selected='selected'"; } ?>>3</option>
                <option value="4" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "4") {echo "selected='selected'"; } ?>>4</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "5") {echo "selected='selected'"; } ?>>5</option>
                <option value="6" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "6") {echo "selected='selected'"; } ?>>6</option>
                <option value="7" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "7") {echo "selected='selected'"; } ?>>7</option>
                <option value="8" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "8") {echo "selected='selected'"; } ?>>8</option>
                <option value="9" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "9") {echo "selected='selected'"; } ?>>9</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-gamma']) == "10") {echo "selected='selected'"; } ?>>10</option>
            </select>
        </td>
        <td><select id="aantal-douglas" name="aantal-douglas" onChange="Calculate();">
                <option value="0" selected="selected">Kies het aantal</option>
                <option value="1" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "1") {echo "selected='selected'"; } ?>>1</option>
                <option value="2" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "2") {echo "selected='selected'"; } ?>>2</option>
                <option value="3" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "3") {echo "selected='selected'"; } ?>>3</option>
                <option value="4" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "4") {echo "selected='selected'"; } ?>>4</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "5") {echo "selected='selected'"; } ?>>5</option>
                <option value="6" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "6") {echo "selected='selected'"; } ?>>6</option>
                <option value="7" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "7") {echo "selected='selected'"; } ?>>7</option>
                <option value="8" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "8") {echo "selected='selected'"; } ?>>8</option>
                <option value="9" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "9") {echo "selected='selected'"; } ?>>9</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-douglas']) == "10") {echo "selected='selected'"; } ?>>10</option>
            </select>
        </td>
        <td><select id="aantal-bartsmit" name="aantal-bartsmit" onChange="Calculate();">
                <option value="0" selected="selected">Kies het aantal</option>
                <option value="1" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "1") {echo "selected='selected'"; } ?>>1</option>
                <option value="2" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "2") {echo "selected='selected'"; } ?>>2</option>
                <option value="3" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "3") {echo "selected='selected'"; } ?>>3</option>
                <option value="4" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "4") {echo "selected='selected'"; } ?>>4</option>
                <option value="5" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "5") {echo "selected='selected'"; } ?>>5</option>
                <option value="6" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "6") {echo "selected='selected'"; } ?>>6</option>
                <option value="7" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "7") {echo "selected='selected'"; } ?>>7</option>
                <option value="8" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "8") {echo "selected='selected'"; } ?>>8</option>
                <option value="9" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "9") {echo "selected='selected'"; } ?>>9</option>
                <option value="10" <?php if(isset($_POST['Submitform']) && strip_tags($_POST['aantal-bartsmit']) == "10") {echo "selected='selected'"; } ?>>10</option>
            </select>
        </td>
    </tr>
</table>
<div style="margin-left:15px; margin-top:20px;"><span id="totaal" style="color:#F00;">0</span></div>

                        <div id="fm-submit">
                            <input name="Submitform" value="Verstuur" type="submit" />
                        </div>


</form> 

是否可以使用php发布javascript变量?我找到了一些关于它的文章,我知道这是可能的,但我无法弄明白。

2 个答案:

答案 0 :(得分:0)

感谢所有回复

这就是诀窍:

使用Javascript:

document.aanvraag.totaal.value = result; //assign value to hidden input element.

HTML

<input type="hidden" name="totaal" id="totaal" value="" />

@Christoph谢谢!:)

答案 1 :(得分:0)

参见表格

 <form name="aanvraag" method="post" action="" onsubmit="Calculate()">

在此处写下您的所有HTML并包含隐藏字段

<input type="hidden" name="totaal" id="totaal" value="" />

<input name="Submitform" value="Verstuur" type="submit" />

</form>

当您提交此表单时,在提交表单之前,计算功能将调用并粘贴 隐藏字段中的总值,然后使用您的指定网址发布所有值。