如何在没有ajax的情况下将javascript动态数据发送到php变量

how to send javascript dynamic data to php variable without ajax

本文关键字:数据 动态 变量 php javascript ajax 情况下      更新时间:2023-09-26

我有一个表单,有3个输入类型的文本。

<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" name="two">
<input type="text" id="three" name="three">
<script>
function multiply(){
    one = document.getElementById('one').value;
    two = document.getElementById('two').value;
    document.getElementById('three').value = one * two
}
</script>

现在我没有三个值,但它是动态的,当我将论坛提交到(forumSubmit.php)时,我会得到的错误

undefiend index three

我搜索了&发现这可以用ajax完成,但我不想使用ajax,我想做一个页面刷新

您可以这样做:

标记

<!-- Use onkeyup on both inputs -->
<input type="text" id="one" onkeyup="multiply()" name="one">
<input type="text" id="two" onkeyup="multiply()" name="two">
<input type="text" id="three" name="three">
​

JavaScript

function multiply() {
   // Parse the values, and count it as 0 if the input is not a number
   // I also made the variables private to this function using the var keyword
   // There is no need to have them in the global namespace
   var one = parseInt(document.getElementById('one').value, 10) || 0;  
   var two = parseInt(document.getElementById('two').value, 10) || 0;
   document.getElementById('three').value= one * two;
}​

工作示例

制作一个演示:http://jsfiddle.net/DjQNx/

<input type="text" id="one" onkeyup="multiply()" name="one" />
<input type="text" id="two" onkeyup="multiply()" name="two" />
<input type="text" id="three" name="three" />
<script type="text/javascript">
function multiply() {
    one = document.getElementById('one').value;
    two = document.getElementById('two').value;
    document.getElementById('three').value = one * two;
}
</script>