使用JavaScript计算值并发送到PHP
目前我正在使用3个不同的页面,分别是HTML、JavaScript和PHP。我已经输入了,我可以使用JavaScript计算使用JavaScript计算值并发送到PHP,javascript,php,Javascript,Php,目前我正在使用3个不同的页面,分别是HTML、JavaScript和PHP。我已经输入了,我可以使用JavaScript计算价格*数量 但我有一个问题:如何将计算出的值发送到PHP页面 HTML: PHP: 我无法得到这个值。请帮忙。谢谢。尝试使用隐藏的表单和输入类型(从myPhp.php更改php文件名): 测试 人数: 价格: 计算 函数和(){ var quantity=parseInt(document.getElementById('quantity').value); var
价格
*数量
但我有一个问题:如何将计算出的值发送到PHP页面
HTML:
PHP:
我无法得到这个值。请帮忙。谢谢。尝试使用隐藏的表单和输入类型(从myPhp.php更改php文件名):
测试
人数:
价格:
计算
函数和(){
var quantity=parseInt(document.getElementById('quantity').value);
var price=parseInt(document.getElementById('price').value);
var总额=价格*数量;
document.getElementById('result').innerHTML=total;
document.getElementById('result')。value=total;
}
尝试使用隐藏的表单和输入类型(从myPhp.php更改php文件名):
测试
人数:
价格:
计算
函数和(){
var quantity=parseInt(document.getElementById('quantity').value);
var price=parseInt(document.getElementById('price').value);
var总额=价格*数量;
document.getElementById('result').innerHTML=total;
document.getElementById('result')。value=total;
}
检查KhorneHoly和Kevin Kloet提供的链接
根据您的代码,您忘记了在HTML中使用元素,这将允许您将数据发送到PHP页面
编辑:我同意Javier Elices的观点,您必须在表单元素中添加一个输入,才能将该值发送到帖子页面。该输入可以隐藏,您可以从javascript函数中指定值检查KhorneHoly和Kevin Kloet提供的链接 根据您的代码,您忘记了在HTML中使用元素,这将允许您将数据发送到PHP页面
编辑:我同意Javier Elices的观点,您必须在表单元素中添加一个输入,才能将该值发送到帖子页面。该输入可以隐藏,并且您可以从javascript函数中指定值您可以将结果存储在相同形式的隐藏字段中。这样,当表单发送时,它将携带您的计算结果。隐藏字段的外观如下所示:
<input type='hidden' id='result' name='result'></input>
名称部分将设置PHP程序要检索的字段的名称
确保您的HTML具有指向PHP的有效表单定义。您可以将结果存储在同一表单的隐藏字段中。这样,当表单发送时,它将携带您的计算结果。隐藏字段的外观如下所示:
<input type='hidden' id='result' name='result'></input>
名称部分将设置PHP程序要检索的字段的名称
确保您的HTML具有指向PHP的有效表单定义。尝试使用表单或Ajax发送请求。Hier就是一个例子:
<?php
if($_POST['result']){
$total = $_POST['result'];
echo "price :".$total."<br/>";
}
?>
number of people: <input id="quantity" name="quantity" size='5'><br><br>
Price: <input type='hidden' id='price' name='price' value='755'>
<button type="button" onclick="sum()">calculate</button>
<p id='result' name='result'></p>
<script>
function sum() {
var quantity = parseInt(document.getElementById('quantity').value);
var price = parseInt(document.getElementById('price').value);
var total = price * quantity;
document.getElementById('result').innerHTML=total;
document.getElementById('result').value=total;
//Ajax
var http = new XMLHttpRequest();
var url ="test2.php" ;
var params = "result="+total;
http.open("POST",url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
</script>
人数:
价格:
算计
函数和(){
var quantity=parseInt(document.getElementById('quantity').value);
var price=parseInt(document.getElementById('price').value);
var总额=价格*数量;
document.getElementById('result').innerHTML=total;
document.getElementById('result')。value=total;
//阿贾克斯
var http=new XMLHttpRequest();
var url=“test2.php”;
var params=“result=”+总计;
http.open(“POST”,url,true);
//随请求一起发送正确的标头信息
http.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
http.send(params);
}
尝试使用表单或Ajax发送请求。Hier就是一个例子:
<?php
if($_POST['result']){
$total = $_POST['result'];
echo "price :".$total."<br/>";
}
?>
number of people: <input id="quantity" name="quantity" size='5'><br><br>
Price: <input type='hidden' id='price' name='price' value='755'>
<button type="button" onclick="sum()">calculate</button>
<p id='result' name='result'></p>
<script>
function sum() {
var quantity = parseInt(document.getElementById('quantity').value);
var price = parseInt(document.getElementById('price').value);
var total = price * quantity;
document.getElementById('result').innerHTML=total;
document.getElementById('result').value=total;
//Ajax
var http = new XMLHttpRequest();
var url ="test2.php" ;
var params = "result="+total;
http.open("POST",url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
</script>
人数:
价格:
算计
函数和(){
var quantity=parseInt(document.getElementById('quantity').value);
var price=parseInt(document.getElementById('price').value);
var总额=价格*数量;
document.getElementById('result').innerHTML=total;
document.getElementById('result')。value=total;
//阿贾克斯
var http=new XMLHttpRequest();
var url=“test2.php”;
var params=“result=”+总计;
http.open(“POST”,url,true);
//随请求一起发送正确的标头信息
http.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
http.send(params);
}
我忘了我在这里写的。实际上我的代码已经有了形式哈哈..哦。。在这种情况下,我编辑了我的答案,我忘了把答案写在这里了。实际上我的代码已经有了形式哈哈..哦。。那么,,我编辑了我的回答我使用了form action='php.php'method='post'已经哈哈,但不起作用..事实上我提供的示例在我的电脑上正常工作。你能告诉我运行时出现了什么错误吗?我使用了form action='php.php'method='post'已经哈哈,但不起作用..实际上我提供的示例在我的电脑上正常工作。你能告诉我运行它时有什么错误吗?段落不是表单元素。因此,给段落赋值对形式没有影响。段落不是形式元素。因此,就形式而言,给段落赋值没有效果。
<input type='hidden' id='result' name='result'></input>
document.getElementById('result').value=total;
<?php
if($_POST['result']){
$total = $_POST['result'];
echo "price :".$total."<br/>";
}
?>
number of people: <input id="quantity" name="quantity" size='5'><br><br>
Price: <input type='hidden' id='price' name='price' value='755'>
<button type="button" onclick="sum()">calculate</button>
<p id='result' name='result'></p>
<script>
function sum() {
var quantity = parseInt(document.getElementById('quantity').value);
var price = parseInt(document.getElementById('price').value);
var total = price * quantity;
document.getElementById('result').innerHTML=total;
document.getElementById('result').value=total;
//Ajax
var http = new XMLHttpRequest();
var url ="test2.php" ;
var params = "result="+total;
http.open("POST",url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
</script>