通过按下按钮调用php函数

通过按下按钮调用php函数,php,Php,我用php写了下面的代码,在这段代码中我想计算水费 但当我按下按钮时,结果不显示 问题在哪里。知道我想使用用户输入的值 <!DOCTYPE html> <html> <body> <form> القيمة: <input type="text" name="cal"><br> <button onclick="myFunction("cal")">إحسب التكلفة</button> &

我用php写了下面的代码,在这段代码中我想计算水费

但当我按下按钮时,结果不显示 问题在哪里。知道我想使用用户输入的值

<!DOCTYPE html>
<html>
<body>

<form>

القيمة: <input type="text" name="cal"><br>

<button onclick="myFunction("cal")">إحسب التكلفة</button>
</form>

<script>

function myFunction($x) {
$x=$x%1000;

if($x<=15){
$x=$x*0.10;
 return $x;}

elseif($x>=16&&$x<=30){
$x=$x*1.00;
 return $x;}

elseif($x>=31&&$x<=45){
$x=$x*3.00;
 return $x;}

elseif($x>=46&&$x<=60){
$x=$x*4.00;
 return $x;}

else{
$x=$x*6.00;
 return $x;}

}
</script>
</body>
</html>

القمة:
إحسب التكلفة 函数myFunction($x){ $x=$x%1000;
如果($x=16&&$x=31&&$x=46&&$x另一种实现方法是将onClick事件添加到按钮和调用函数中

例如

// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("myBtn").onclick = function() {myFunction()};

/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
//获取按钮,当用户单击它时,执行myFunction
document.getElementById(“myBtn”).onclick=function(){myFunction()};
/*myFunction在添加和删除show类之间切换,show类用于隐藏和显示下拉列表内容*/
函数myFunction(){
document.getElementById(“myDropdown”).classList.toggle(“show”);
}
//如果用户在下拉列表之外单击,请关闭下拉列表
window.onclick=函数(事件){
如果(!event.target.matches('.dropbtn')){
var dropdowns=document.getElementsByClassName(“下拉内容”);
var i;
对于(i=0;i
首先,javascript中没有
elseif
操作符。如果
,你应该使用
else。你还需要从输入中获取值并将其传递给
myFunction
。我修改了你的示例,请检查这个请尝试下面的脚本。问题是你使用elseif而不是else if 而且您调用的函数错误。我在输入字段中添加了一个id。请检查

    <!DOCTYPE html>
    <html>
    <body>

    <form>

        <input type="text" name="cal" id="cal"><br>

        <button type=button onclick="document.getElementById('data').innerHTML = myFunction(document.getElementById('cal').value)">إحسب التكلفة</button>
<hr>
Result:<span id="data"></span>
    </form>

    <script>

        function myFunction($x) {
            $x = $x % 1000;

            if ($x <= 15) {
                $x = $x * 0.10;
                return $x;
            }

            else if ($x >= 16 && $x <= 30) {
                $x = $x * 1.00;
                return $x;
            }

            else if ($x >= 31 && $x <= 45) {
                $x = $x * 3.00;
                return $x;
            }

            else if ($x >= 46 && $x <= 60) {
                $x = $x * 4.00;
                return $x;
            }

            else {
                $x = $x * 6.00;
                return $x;
            }

        }
    </script>


إحسب التكلفة
结果: 函数myFunction($x){ $x=$x%1000;
如果($x=16&&$x=31&&$x=46&&$x可能与1重复。您正在对硬编码字符串2执行操作。不使用值将结果返回到div中。但是如何在按钮栏下的行中引入值,对于恼人的问题,您是否可以编写您要编写的代码?在Word Press程序中编写代码,但结果未显示:(你知道WORDPRESS程序吗?