简体   繁体   English

如何使用 PHP 将 Javascript 变量传递到 MYSQL 中?

[英]How Do I Pass Javascript Variables Into MYSQL Using PHP?

I'm tring to send my variable to mysql database, but the database only show the variable by using the HTML input tag.我想将我的变量发送到 mysql 数据库,但数据库仅使用 HTML 输入标签显示变量。

The result showed " Undefined index: rate & amount"结果显示“未定义的指数:比率和金额”

Requesting the resolution.请求解决。

Thanks谢谢

 <?php if(isset($_POST['send'])){ $dayhour = $_POST['dayhour']; $wagetype = $_POST['wagetype']; $rate = $_POST['rate']; $amount = $_POST['amount']; $query = "INSERT INTO `result` ( `DaynHour`, `Wagetype`, `Rate`, `Amount`) VALUES ('$dayhour','$wagetype', '$rate', '$amount')"; }?>
 <form action="connection.php" method="post" class="form-signin"> <label for="hour">hour</label><input name="dayhour" type="text" id="dayhour" ><br> <label for="wagetype">type</label> <select name="wagetype" id="wagetype" onchange="calc()"> <option value="PublicHolidays" id="PHOT">PublicHolidays</option> <option value="Normaldays" id="NDOT">Normaldays</option> <option value="MorningShift" id="NDOT">MorningShift</option> </select><br><br> <script type="text/javascript"> function calc(){ var a = parseFloat(document.querySelector("#dayhour").value); var op = document.querySelector("#wagetype").value; var amount; if (op == "PublicHolidays") { rate = 37.5; amount = rate *a;} else if (op == "Normaldays") { rate = 2; amount = rate *a;} else if (op == "MorningShift") { rate = 6; amount = rate *a;} document.querySelector("#amount").innerHTML = amount; document.querySelector("#rate").innerHTML = rate; } </script> <label for="rate">rate:</label><output name="rate" id="rate" ></output><br> <label for="amount">amount:</label><output name="amount" id="amount" ></output><br> <button name="send" type="submit">Sign up</button>

Yes you can pass any value using your form.是的,您可以使用表单传递任何值。 In a situation like this, make a hidden input filed and assign its value to the value of any tag you want.在这种情况下,创建一个隐藏的输入字段并将其值分配给您想要的任何标签的值。 This can be done with javascript or easily JQuery by listening on change envents.这可以通过 javascript 或通过监听更改事件轻松 JQuery 来完成。 And that's all.就这样。 Submit the form and you have values.提交表格,你就有了价值。

Try to change <output name="rate" id="rate" ></output> to <input name="rate" id="rate" type="hidden"/> and <output name="amount" id="amount" ></output> to <input name="amount" id="amount" type="hidden"/>尝试将<output name="rate" id="rate" ></output>更改为<input name="rate" id="rate" type="hidden"/><output name="amount" id="amount" ></output><input name="amount" id="amount" type="hidden"/>

And you may change your calc() function to this codes, I think it is a good idea to add default value of amount and rate just in case they don't meet IF conditional您可以将您的 calc() function 更改为此代码,我认为添加金额和费率的默认值是一个好主意,以防万一它们不符合 IF 条件

function calc() {
    var a = parseFloat(document.querySelector("#dayhour").value);
    var op = document.querySelector("#wagetype").value; 
    var amount;
    var rate;
    if (op == "PublicHolidays") { 
        rate = 37.5; amount = rate *a;
    } else if (op == "Normaldays") { 
        rate = 2; amount = rate *a;
    } else if (op == "MorningShift") { 
rate = 6; amount = rate *a;
    }
    document.getElementById("amount").value = amount;
    document.getElementById("rate").value = rate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM