如何从我的JS调用php函数?

时间:2016-06-13 06:05:10

标签: javascript php

这是我的PHP函数:

  <?php 
             function input_bill()
            {
        $servername = "localhost";
        $conusername = "root";
        $conpassword = "";
        $dbname = "annapoorna";
        $dsn='mysql:dbname='.$dbname.';host='.$servername;
        $conn = new PDO($dsn, $conusername, $conpassword);
            $date="";
            $category_value="";
            $item_value="";
            $vendor_value="";
            $unit_value="";
            $price_rate_value=0;
            $quantity_value=0;
            $vat_value=0;
            $freight_value=0;
            $other_charges_value=0;
            $total_value=0;  

           if(!empty($_POST['datepicker'])&& !empty($_POST['category']) && !empty($_POST['name'])&& 
              !empty($_POST['vendor_name']) && !empty($_POST['Unit']) && !empty($_POST['Price_per_Unit'])
                   && !empty($_POST['Quantity']) && !empty($_POST['Freight_charges']) && !empty($_POST['Other_charges'])
                   && !empty($_POST['VAT']))
           {
            $date = date("Y-m-d",strtotime($_POST['datepicker']));

            $category_value=$_POST['category'];

            $item_value=$_POST['name'];

            $vendor_value=$_POST['vendor_name'];

            $unit_value=$_POST['Unit'];

            $price_rate_value=$_POST['Price_per_Unit'];

            $quantity_value=$_POST['Quantity'];

            $freight_value=$_POST['Freight_charges'];

            $other_charges_value=$_POST['Other_charges'];

            $vat_value=$_POST['VAT'];
            } 
            else
            {
                echo '<script type="text/javascript">alert("Please enter all values!");</script> ';
            }
            $total_value= ($price_rate_value * $quantity_value)+$freight_value+$other_charges_value+$vat_value;

            $sql='INSERT INTO bill(date, category, item, vendor, unit, price_per_unit, quantity, vat, freight_charges, other_charges, total) '
        . 'values (:date,:category, :item,:vendor, :unit, :price_per_unit, :quantity, :vat, :freight_charges, :other_charges, :total)';
                 $sth=$conn->prepare($sql); 
                $sth->execute(array(':date'=>$date,':category'=>$category_value,':item'=>$item_value, ':vendor'=>$vendor_value,
                    ':unit'=>$unit_value,':price_per_unit'=>$price_rate_value,':quantity'=>$quantity_value,':vat'=>$vat_value,
                    ':freight_charges'=>$freight_value,':other_charges'=>$other_charges_value,':total'=>$total_value));              
            }
            ?>

这是我的JS。

        <script type="text/javascript">
            function input_bill_js() {
            $.ajax ({
            url: "Accounts.php",
            success: function( result ) {

    }
}   
        </script>

我只想调用此函数,但它没有发生。我该怎么做?根据我的条件,插入查询应该执行,或者应该显示警告消息。

2 个答案:

答案 0 :(得分:0)

您只能调用.php文件。你还没有打电话给这个功能 Plz像这样调用文件底部的函数

input_bill();

当这个函数调用时,你的代码应该执行

答案 1 :(得分:0)

它没有回答它在你的剧本中显示出一些错误

 <script type="text/javascript">
        function input_bill_js() 
       {
          $.ajax ({
           url: "Accounts.php",
          success: function( result ) 
          {

          }
        }); //missing here  ajax close 
      }   
    </script>