jquery表单提交不在cakephp中工作

时间:2016-11-15 06:31:50

标签: jquery cakephp

提前致谢。实际上我有一个price_submit.ctp页面包含一个用户下拉菜单。点击用户下拉菜单,下面会显示一个文本框列表,其中包含课程名称和价格文本框以输入价格值。这是我通过jquery完成的。我无法将表单数据提交到mysql数据库。

表单数据应该像这样插入mysql数据库:

 column 1  | column2                | column 3
 ragith    |  ITIL Foundation, CAPM | $200,$400    

查看页面是price_submit.ctp

<head>
// This function will display the course textboxes and price textboxes when a name from dropdown menu is selected

$(document).ready(function(){
    $('#user').change(function(){         
        var nameIdVal = $(this).val(); 
        $.ajax({
            type: "POST",
            url: '/invl_exams/userprice',
            cache: "false",
            data: {userid:nameIdVal}, 
            success : function(result){
                //alert(result);
                $('#divprice').html(result);                            
            } 
        }); 
    });
});  

此功能用于在单击提交按钮时提交表单。但我无法将表单数据提交到数据库中。

$(document).ready(function(){
    $('.btn').click(function(event){
        event.preventDefault();

        $.ajax({
            method: "POST", 
            url: "/invl_exams/submitprice",               
            cache: "false",                   
            data: $('#addPriceForm').serialize(),                  
            dataType: "text",   
            success: function(strMessage){
                //alert(strMessage); 
                //console.log(strMessage); 
                //$('#msg').html(strMessage);  
            },
            error: function(strMessage){
                //alert('error'); 
                console.log('error');    
            } 
        });     
        clearData(); 
    });
});

function clearData() {
    $('#addPriceForm :input').each(function(){
        $(this).val('');    
    });
} 
</head>
<body>
    <form name="priceForm" class="form-horizontal" role="form" accept-charset="utf-8" method="post" id="addPriceForm"  action="/invl_exams/users/price_submit">  
        <div style="display:none;"><input type="hidden" value="POST" name="_method"></div>
        <div class="form-group">
            <label for="ExamUsername">Users</label>  
            <select class="form-control" required="required" id="user" name="data[Price][userid][]">    
                <option value="">-- Select --</option>
                <?php foreach($users as $username): ?>   
                <option value="<?php echo $username['users']['id']?>"><?php echo $username['users']['username']?>
                </option>
                <?php endforeach; ?> 
            </select> 
            <label id="ExamUsername-error" class="error" for="ExamUsername"></label>       
            </div>

            <!--On selecting username showing the Courses with price textboxes in this id here -->  
            <div class="form-group" id="divprice"></div>

            <div class="form-group">  
                <button class="btn btn-default">Submit</button>  
            </div>
        </form>
    </body>

routes.php页面是

Router::connect('/userprice', array('controller' => 'users', 'action' => 'showprice')); 

Router::connect('/submitprice', array('controller' => 'users', 'action' => 'price_submit'));

控制器页面是usersController.php

<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{    

price_submit.ctp查看页面

中选择用户名时,将显示文本框
public function showprice()
{
    $this->loadModel('Exam');   
    $id = $_POST['userid'];
    //echo $id;
    //die();

    if($id != 0) {
        $allPrice = $this->Exam->query("select * from exams where username = $id");    
        //print_r($allPrice);          
        //print_r($allPrice[0]['exams']['exam']);  
        //echo $allPrice[0]['exams']['exam'];
        //die();

        $total_price = $allPrice[0]['exams']['exam'];                   
        $price = explode(',',$total_price);
        echo "<table class='tblrow' width='350px'>";  
        for($i=0; $i<count($price); $i++) {
            //echo $price[$i];
            echo "<tr>";
            echo "<td><label for='Course'>Course</label><input type='text' id='course' name='data[Price][course][]' value='$price[$i]'></td>";
            echo "<td><label for='Price'>Price</label><input type='text' id='price' name='data[Price][price][]' value=''></td>";
            echo "</tr>";
            echo "<tr><td>&nbsp;</td></tr>";    
        }
        echo "</table>";   
    } else  {
        echo "No results found";     
    }
    die();      
}

这是用于在用户点击提交按钮时提交表单

public function price_submit() {
    // print_r($_POST);
    if(isset($_POST['data'])) {
        $this->loadModel('Price'); 
        $priceData = $_POST['data'];

        //$this->set(array('priceData'=>$priceData,'_serialize'=>'pric‌​eData'));         
        $this->Price->create();
        $this->Price->save($priceData);  

        /*
        if($this->Price->save($priceData))  
        {
            echo "Data added successfully";                  
        } 
        */           
    }
}

0 个答案:

没有答案