dongxie45083 2014-04-24 09:08
浏览 50
已采纳

JQuery数量的单元格,用PHP计算总和

I'm trying to send, the numbers of every cell, and calculate the sum of them in php. Then show the result in text input But it did not work.How Can I do it?

This is my php code:

<?php

    if(isset($_POST['keyup'])){
    $sum1 = $_POST['sum1'];
    $sum2 = $_POST['sum2'];
    $sum3 = $_POST['sum3'];

    $resultado = $sum1+ $sum2 + $sum3;

     echo "Resultado: " .'<input type="text" name="resultado" value="'.$resultado.'">';

 }


?>

FULL FIDDLE CODE

Thank You in Advance

  • 写回答

1条回答 默认 最新

  • doutang3077 2014-04-24 09:15
    关注

    I made a quick look in your full code and it seem the form tags are missing. you need to embody your code in between form tags. which will run a script.

    <form action="yourphpfile" method="post"></form>
    

    if you dont want to load a new page you need to use Ajax which is a bit more complicated. more info about form can be found here. http://www.w3schools.com/php/php_forms.asp

    edit the action script will run when a submit input button of some sort is clicked.

    second edit. I looked into some old project to see how I instead refreshed a page with ajax which goes as follows.

    //This function will need to be called on a button click for example
    function login(){
        var sum1 = document.getElementById("sum1");
        var sum2 = document.getElementById("sum2");
        var sum3 = document.getElementById("sum3"); 
        //Do some extra refining here if needed. Depends on how the data is obtained.
        sum1 = sum1.value;
        sum2 = sum2.value;
        sum3 = sum3.value;
        //Calls the functions below
        ajax(sum1, sum2, sum3);
    }
    
    //Javascript call this function on a button click 
    //or create an extra function that calls this and loads the sums from the page.
    function Ajax(sum1, sum2, sum3){
        var xml;
        xml = setXML(xml);
        xml.onreadystatechange=function(){
            if (xml.readyState==4 && xml.status==200){
                document.getElementById(DIV THAT WILL NEED TO BE UDATED HERE ).innerHTML=xml.responseText;
            }
        };
        xml.open("POST", URLTOYOURSCRIPT.php ,true);
        xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xml.send("sum1="+sum1+"&sum2="+sum2+"&sum3="+sum3+);
    }
    function setXML(xml){
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xml = new XMLHttpRequest();
        return xml;
        }
        else{// code for IE6, IE5
        xml = new ActiveXObject("Microsoft.XMLHTTP");
        return xml;
        }
    }
    

    and then your php script will need the following.

    //Obtains the sums from the post data send by ajax.
    $sum1 = $_POST["sum1"];
    $sum2 = $_POST["sum2"];
    $sum3 = $_POST["sum3"];
    //Perform some extra checks here like isnumeric() to see if they are actual numbers etc.
    if(is_numeric($sum1) && is_numeric($sum2) && is_numeric($sum3){
        //Calculates the sum and echoes it on your existing page.
        $totalsum = $sum1+$sum2+$sum3;
        echo $totalsum;
    }else{
        echo "you did not insert numbers"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据