用Ajax显示结果

时间:2017-02-17 06:14:09

标签: php jquery html ajax continuous-integration

我正在尝试使用CI和Ajax进行一些计算程序。 计算结果是成功但是,当我想用​​带有if条件的变量显示结果时,变量将不会显示但结果仍然有效。所以我真的很困惑这个并且已经处于边缘。 如果有任何参考资料,请告诉我。抱歉我的英语不好。

这是控制器:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Kalkulator extends CI_Controller{

    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $hasil ['hasil']= 0;
        $hasil ['laba'] = '';
        $this->load->view('Hitungan', $hasil);
    }
    public function hitung()
    {
        $v1 = $this->input->post('v1');
        $v2 = $this->input->post('v2');
        $v3 = $this->input->post('v3');
        $v4 = $this->input->post('v4');
        $v5 = $this->input->post('v5');
        $v6 = $this->input->post('v6');
        $v1 = $v1;
        $v2 = $v2;
        $v3 = $v3;
        $v4 = $v4;
        $v5 = $v5;
        $v6 = $v6;
        $laba = '';
        $hasil = 0;
        $hasil = ((($v2*$v3*$v4)+($v1*$v5))/$v6);
        $nilai['hasil'] = round($hasil*50)."%" .$laba;
        echo json_encode($nilai);
        if ($nilai['hasil'] < 30) {
            $laba = "Budidaya Anda akan gagal";
        }
    }
} 

剧本:

<script type="text/javascript">
    $(document).ready(function() {
        $(".submit").click(function(event) {
            event.preventDefault();
            var bilangan1 = $("#v1").val();
            var bilangan2 = $("#v2").val();
            var bilangan3 = $("#v3").val();
            var bilangan4 = $("#v4").val();
            var bilangan5 = $("#v5").val();
            var bilangan6 = $("#v6").val();
            jQuery.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>"+"index.php/kalkulator/hitung",
                dataType: 'json',
                data: {v1: bilangan1, v2: bilangan2, v3: bilangan3, v4: bilangan4, v5: bilangan5, v6: bilangan6},
                success: function(res) {
                    jQuery("#value").html(res.hasil);
                }
            });
        });
    });
</script>

HTML:

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Kalkulator</title>
    <link href="<?php echo base_url();?>css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h1 class="form-header"></h1>
      <form class="form-horizontal">
            <?php echo form_open(); ?>
        <h2>Kalkulator Hitungan</h2>
      <br>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Padat Tebar Ikan</label>
          <div class="col-sm-6">
            <?php 
                        $v1 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v1',
                        'id'        => 'v1'
                        );
                        echo form_input($v1);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/ekor</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Panjang Kolam</label>
          <div class="col-sm-6">
            <?php 
                        $v2 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v2',
                        'id'        => 'v2'
                        );
                        echo form_input($v2);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/m2</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Lebar</label>
          <div class="col-sm-6">
            <?php 
                        $v3 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v3',
                        'id'        => 'v3'
                        );
                        echo form_input($v3);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/m2</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Tinggi</label>
          <div class="col-sm-6">
            <?php 
                        $v4 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v4',
                        'id'        => 'v4'
                        );
                        echo form_input($v4);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/m2</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Harga Benih per ekor</label>
          <div class="col-sm-6">
            <?php 
                        $v5 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v5',
                        'id'        => 'v5'
                        );
                        echo form_input($v5);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/ekor</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3">Harga Konsumsi per kg</label>
          <div class="col-sm-6">
            <?php 
                        $v6 = array(
                        'class'     => 'form-control',
                        'type'      => 'number',
                        'name'      => 'v6',
                        'id'        => 'v6'
                        );
                        echo form_input($v6);
                        ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label ">/kg</label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3"></label>
          <div class="col-sm-6">
            <?php echo form_submit('submit', 'Hitung Sekarang', "class='btn btn-default submit'"); ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label "></label>
          </div>
        </div>
        <div class="form-group col-sm-12">
          <label class="control-label col-sm-3"></label>
          <div class="col-sm-6">
            <output id="value"></output>
            <?php echo form_close(); ?>
          </div>
          <div class="col-sm-3">
          <label class="control-label "></label>
          </div>
        </div>


      </form>
      <footer class="form-footer">&copy beagle company</footer>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

3 个答案:

答案 0 :(得分:1)

你写了太多我们不需要的代码。您还尝试将字符串与int进行比较。 Php是一种松散耦合的语言,我们不需要在使用前初始化变量。我希望这能解决你的问题。

public function hitung()
{
    $v1 = $this->input->post('v1');
    $v2 = $this->input->post('v2');
    $v3 = $this->input->post('v3');
    $v4 = $this->input->post('v4');
    $v5 = $this->input->post('v5');
    $v6 = $this->input->post('v6');

    $hasil = ((($v2*$v3*$v4)+($v1*$v5))/$v6);
    $nilai = round($hasil*50);

    if ($nilai < 30) {
         $nilai = $nilai."%";
         $result = array(
         'laba' => "Budidaya Anda akan gagal",
         'nilai' => $nilai
         );
        echo json_encode($result);
    }else{
         $nilai = $nilai."%";
         $result = array(
         'nilai' => $nilai
         );
        echo json_encode($result);
   }
}
<script type="text/javascript">
    $(document).ready(function() {
        $(".submit").click(function(event) {
            event.preventDefault();
            var bilangan1 = $("#v1").val();
            var bilangan2 = $("#v2").val();
            var bilangan3 = $("#v3").val();
            var bilangan4 = $("#v4").val();
            var bilangan5 = $("#v5").val();
            var bilangan6 = $("#v6").val();
            jQuery.ajax({
                dataType: "json",
                type: "POST",
                url: "<?php echo base_url(); ?>"+"index.php/kalkulator/hitung",
                data: {v1: bilangan1, v2: bilangan2, v3: bilangan3, v4: bilangan4, v5: bilangan5, v6: bilangan6},
                success: function(res) {
                   alert(res.laba);
                   alert(res.nilai);
                }
            });
        });
    });
</script>

答案 1 :(得分:0)

你应该返回Json代码如果你的jquery中有dataType:json

在您的Controller 类中,Kalkulator扩展了CI_Controller { 和功能公共功能hitung() 你这样做

echo json_encode($nilai);

这很好,因为您使用 DataType:Json 发送Ajax调用 因此,如果您想要返回删除该行并添加其他条件条件,那么您应该尝试这样

if ($nilai['hasil'] < 30) {
        $laba = "Budidaya Anda akan gagal";
        echo json_encode(array('msg'=>$laba,'data'=>$nilai));
}
else{
    echo json_encode($nilai);
}

因此,如果条件也在jquery中,你可以获得内部值 的 RES

答案 2 :(得分:0)

你的if条件不对。您要比较的是

$nilai['hasil'] = round($hasil*50)."%" .$laba;

所以这里$nilai['hasil']将有价值&#39; 90%&#39;例如

你正在比较

if ($nilai['hasil'] < 30) 

表示

if ('90%' < 30)

这不会满足

首先存储在某个变量中然后比较然后追加&#39;%&#39;

    $hasil = round($hasil*50);
$nilai = array();
    if($hasil < 30) {
    $nilai['hasil'] = $hasil . "%" . $laba;
    $laba = "Budidaya Anda akan gagal";
    }
echo json_encode($nilai);