请帮帮我。提前致谢。其实我试图从html表单插入到mysql数据库。我的表单包含用户组合框,其中包含数据库中的数据。当我从组合框中选择用户时,将显示分配有文本框和价格文本框以输入数据的相应课程,这是我使用ajax完成的。 现在的情况是我无法将表单中的数据插入到mysql数据库中。我会一步一步地解释一切。
Inside UsersController.php页面 从add_price.ctp视图页面单击用户组合框时,以下功能将显示课程文本框和价格文本框
<?php
public function showprice()
{
$this->loadModel('Exam');
$id = $_POST['userid'];
if($id!=0)
{
$allPrice = $this->Exam->query("select * from exams where username = $id");
foreach($allPrice as $allPrices)
{
$total_price = $allPrices['exams']['exam'];
$price = explode(',',$total_price);
echo "<table class='tblrow' width='350px'>";
for($i=0;$i<count($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> </td></tr>";
}
}
echo "</table>";
}
else
{
echo "No results found";
}
die();
}
The below function is for inserting into Mysql database from the form.
public function price_submit()
{
// print_r($_POST);
if(isset($_POST['data']))
{
$this->loadModel('Price');
$priceData = $_POST['data'];
//$this->set(array('priceData'=>$priceData,'_serialize'=>'priceData'));
$this->Price->create();
$this->Price->save($priceData);
/*
if($this->Price->save($priceData))
{
echo "Data added successfully";
}
*/
if($this->Price->save($priceData))
{
//$this->Session->setFlash('<p style="width:220px;background-color:#34A853;color:white;text-align:center;font-weight:bold">User is Created Successfully</p>');
$this->redirect('index');
}
}
$this->loadModel('User');
//$userData = $this->User->find('all');
$userData = $this->User->query("select * from users where id!= 15 and id!= 8");
$this->set('users',$userData);
}
?>
My add_price.ctp page is
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->Html->css('jquery-ui.min.css'); ?>
<?php echo $this->Html->script('jquery.js'); ?>
<?php echo $this->Html->script('jquery-ui.min.js'); ?>
<?php echo $this->Html->script('jquery.validate.min'); ?>
<?php echo $this->Html->css('bootstrap.min');?>
<?php echo $this->Html->script('bootstrap');?>
<?php echo $this->Html->script('bootstrap.min'); ?>
<style type="text/css">
#formadd{ width:550px;height:500px;border:1px solid black;margin: 0 auto;background-color:white;box-shadow: 1px 1px 1px }
#content{ padding:10px 0px 0px 84px;}
#heading{ font-size:25px;padding:10px 0px 0px 0px;text-align:center; }
.col-md-3{ width:400px;background-color:white;margin:10px 0px 20px 10px; }
.clearall{clear:both;}
.error{color:#D06578;}
</style>
<script>
This code is for when selecting users from the combobox it will call the UsersController showprice function for displaying the courses and price textboxes.
$(document).ready(function(){
$('#user').change(function(){
var nameIdVal = $(this).val();
$.ajax({
type: "POST",
url: '/invl_exams/showprice',
cache: "false",
data: {userid:nameIdVal},
success : function(result){
//alert(result);
$('#divprice').html(result);
}
});
});
});
This code will call price_submit from the UsersController.php to insert into database
$(document).ready(function(){
$('.btn').click(function(event){
event.preventDefault();
$.ajax({
url: "/invl_exams/submitprice",
method: "POST",
data: $('#ExamAddExamForm').serialize(),
dataType: "text",
success: function(strMessage){
consloe.log(strMessage);
//$('#msg').text(strMessage);
},
error: function(strMessage){
console.log('error');
//$('#msg').text(strMessage);
}
});
clearData();
});
});
function clearData()
{
$('#ExamAddExamForm :input').each(function(){
$(this).val('');
});
}
</script>
</head>
<body>
<div id="formadd">
<p> </p>
<p id="heading">Assign Price</p>
<div id="content">
<div class="col-md-3">
<div id="msg"></div>
<form name="priceForm" class="form-horizontal" role="form" accept-charset="utf-8" method="post" id="ExamAddExamForm" action="/invl_exams/users/priceSubmit">
<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>
</div><br class="clearall">
</div>
</div>
</body>
</html>
-------------- MY FULL CODE AS BELOW ------------------------------------
My controller page is UsersController.php
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{
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");
//echo "<pre>";
//print_r($allPrice);
//echo "</pre>";
//echo count($allPrice);
//print_r($allPrice[0]['exams']['exam']);
// die();
foreach($allPrice as $allPrices)
{
$total_price = $allPrices['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> </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'=>'priceData'));
$this->Price->create();
$this->Price->save($priceData);
/*
if($this->Price->save($priceData))
{
echo "Data added successfully";
}
*/
if($this->Price->save($priceData))
{
//$this->Session->setFlash('<p style="width:220px;background-color:#34A853;color:white;text-align:center;font-weight:bold">User is Created Successfully</p>');
$this->redirect('index');
}
}
}
}
?>
My routes.php page is
<?php
Router::connect('/submitprice', array('controller' => 'users', 'action' => 'price_submit'));
Router::connect('/course', array('controller' => 'users', 'action' => 'priceshow'));
?>
My add_price.ctp page is
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->Html->css('jquery-ui.min.css'); ?>
<?php echo $this->Html->script('jquery.js'); ?>
<?php echo $this->Html->script('jquery-ui.min.js'); ?>
<?php echo $this->Html->script('jquery.validate.min'); ?>
<?php echo $this->Html->css('bootstrap.min');?>
<?php echo $this->Html->script('bootstrap');?>
<?php echo $this->Html->script('bootstrap.min'); ?>
<style type="text/css">
/*body{background-color:#D4D6D3;}
#formadd{ width:600px;height:700px;margin: 0 auto;background-color:white;}
#heading{ background-color:#668CAF;width:600px;height:60px;font-size:25px;text-align:center;padding:10px 0px 0px 0px;}
#content{ padding:10px 0px 0px 84px;}
.col-md-3{ width:400px;background-color:white;margin:10px 0px 20px 10px; }
.clearall{clear:both;} */
#formadd{ width:550px;height:500px;border:1px solid black;margin: 0 auto;background-color:white;box-shadow: 1px 1px 1px }
#content{ padding:10px 0px 0px 84px;}
#heading{ font-size:25px;padding:10px 0px 0px 0px;text-align:center; }
.col-md-3{ width:400px;background-color:white;margin:10px 0px 20px 10px; }
.clearall{clear:both;}
.error{color:#D06578;}
</style>
<script>
$(document).ready(function() {
$("#ExamDateMonth").datepicker( { dateFormat: " yy-mm-dd " } );
});
$(function() {
// Setup form validation on the #register-form element
$("#ExamAddExamForm").validate({
// Specify the validation rules
rules: {
ExamUsername: { required: true },
ExamName: { required: true }
},
// Specify the validation error messages
messages: {
ExamUsername: "Please enter the Username",
ExamName: "Please enter the Examname",
},
submitHandler: function(form) {
form.submit();
}
});
});
$(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();
//console.log($('#ExamAddExamForm').serialize());
$.ajax({
url: "/invl_exams/submitprice",
method: "POST",
data: $('#ExamAddExamForm').serialize(),
dataType: "text",
success: function(strMessage){
consloe.log(strMessage);
//$('#msg').text(strMessage);
},
error: function(strMessage){
console.log('error');
//$('#msg').text(strMessage);
}
});
clearData();
});
});
function clearData()
{
$('#ExamAddExamForm :input').each(function(){
$(this).val('');
});
}
</script>
</head>
<body>
<div id="formadd">
<p> </p>
<p id="heading">Assign Price</p>
<div id="content">
<div class="col-md-3">
<div id="msg"></div>
<form name="priceForm" class="form-horizontal" role="form" accept-charset="utf-8" method="post" id="ExamAddExamForm" action="/invl_exams/users/priceSubmit">
<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>
</div><br class="clearall">
</div>
</div>
</body>
</html>
My Model page is User.php
<?php
//App::uses ('AppModel','Model');
class User extends AppModel{
public $validate = array(
'username' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Username is required'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken')
),
'password' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A password is required'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Password has already been taken')
),
'full_name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Full name is required'
)
),
/*'role' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Role is required'
)
) */
'email' => array(
array(
'rule' => array('email'),
'massage' => 'Please enter a valid email address',
),
),
'secondary_email' => array(
array(
'rule' => array('email'),
'massage' => 'Please enter a valid email address',
),
),
'phone' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Phone is required'
)
),
'secondary_phone' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Phone is required'
)
),
'location' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Loacation is required'
)
),
'business_name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Business Name is required'
)
),
'document' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Document is required'
)
),
'doc_file' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Document is required'
)
),
'pname' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Name is required'
)
),
'pemail' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Please enter a Valid Email Id'
)
),
'pOfc_phone' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Please enter a Phone Number'
)
),
'pdesignation' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'Designation is Required'
)
),
);
}
?>