I am a newbie in Codeigniter. I cannot insert data into my table. I have used bootstrap files too.
These are lines from View - groups.php
<input type="text" name="group" id="group" placeholder="Group">
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
Model - groups_model.php
function insert_groups($dbdata) {
$this->db->insert('groups', $dbdata);
}
Controller - groups_controller.php
public function group()
{
$this->load->view('groups');
$this->load->model('groups_model');
if(isset($_POST['addgroup']))
{
$userID= '2';
$groupname= $this->input->post('group');
//$this->load->model('groups_model');
$data=array(
'userID'=>$userID,
'groupname'=>$groupname
);
$this->groups_model->insert_groups($data);
//var_dump($this->db->last_query());
}
}
}
My full html form is pasted below. There are a lot of comments in there.
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Content</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="<?php echo base_url(); ?>stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<SCRIPT language="javascript">
// var person;
// var element;
//
// function add() {
// var e1 = document.getElementById('group').value;
// var e2 = document.getElementById('select');
// var o = document.createElement('option');
// o.value = "group_body.html";
// o.text = e1;
// e2.options.add(o);
//
// }
// function goto() {
// var index = document.getElementById('select').selectedIndex;
// if (index.value != "select") {
// location = index.value;
// }
// }
// function add()
// {
// //Create an input type dynamically.
// element = document.createElement("button");
// element.className='btn btn-default';
// var person = document.getElementById('group').value;
// var t = document.createTextNode(person);
// element.appendChild(t);
// element.id=person;
// //Assign different attributes to the element.
//// element.type = type;
//// element.value = type; // Really? You want the default value to be the type string?
//// element.name = type; // And the name too?
//
// element.onclick = function() { // Note this is a function
// //alert(element.id);
//// $(document).ready(function(){
//// $("#element").click(function(){
// $('body').load('group_body.html');
//// });
//// });
// };
//
// var foo = document.getElementById("fooBar");
// //Append the element in page (in span).
// foo.appendChild(element);
//// var d = document.getElementById('fooBar');
//// d.appendChild(i);
// }
//
// function copy()
// {
// var n1 = document.getElementById('addKeyword');
// var n2 = document.getElementById('getKeyword');
// n2.innerText = n1.value;
// }
</SCRIPT>
</head>
<body>
<form method="post" name="form1">
<div class="btn-group" id="fooBar">
<!-- <button type="button" class="btn btn-default">Marketing</button>
<button type="button" class="btn btn-default">Internet</button>
<button type="button" class="btn btn-default">Politics</button>-->
select a group:
<select class="form-control" id="select" onchange="javascript:location.href = this.value;">
<option selected value="select">select</option>
<!-- <option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>-->
</select>
<button type="button" class="btn btn-default" id="btnAdd" data-toggle="modal" data-target="#myModal">Add Group</button>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Group Name</h4>
</div>
<div class="modal-body">
<h5>Please enter Group Name:</h5>
<input type="text" name="group" id="group" placeholder="Group">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="boxed">
<h4>Group:<span class="label label-default" id="span"><script>document.getElementById('group').innerText = document.getElementById('span').innerText</script></span>
<button type="submit" class="btn btn-default">Edit Name</button>
<button type="submit" class="btn btn-default">Disable</button>
<button type="submit" class="btn btn-default">Delete Group</button></h4>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter your keyword" id="addKeyword">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="copy()">Add</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div>
<h4>Keywords:</h4>
<!-- <div class="keyword" id="getKeyword"></div>-->
</div>
</form>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="<?php echo base_url(); ?>javascripts/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>javascripts/bootstrap.js"></script>
</body>
</html>
try
public function group()
{
$this->load->model('groups_model');
if($this->input->post('addgroup'))
{
$userID= '2';
$groupname= $this->input->post('group');
$data=array(
'userID'=>$userID,
'groupname'=>$groupname
);
$this->groups_model->insert_groups($data);
}
$this->load->view('groups');
}
}
And add action to your form :
Change
<form method="post" name="form1">
To
<?= form_open('yourcontroller/action')?>
//anything HTML inside your form
<?= form_close()?>
And you must load form helper to do this. In autoload.php
$autoload['helper'] = array('form');
Hope it helps !
Why are you using button when it should be submit.
change this
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
to
<button type="submit" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
You have to submit the data, to get it in post
Basically you don't have action attribute in your form tag.
<form method="post" name="form1">
You can use this expressions:
<form action="yourcontroller/action" method="post" name="form1">
or use the Codeigniter form helper
$this->load->helper('form');
echo form_open("yourcontroller/action");
Hope it helps you.
Related
I have a sample code below which consists of a simple bootstrap modal for delete confirmation message. The code is working perfectly,while i give input type= button , but once i give type=submit it does not submit form and dialog modal dont open. Here is my code.
<?php
if(isset($_POST['submit'])){
echo $_POST['first_name'];
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script type="text/javasript">
$('.submit').click(function(e){
e.preventDefault();
});
</script>
<title>JS Bin</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="first_name" id="first_name">
<!--<button type="button" class="btn btn-default" data-toggle="modal" data-target="#confirm-submit">Submit</button>-->
<button type="submit" name="submit" class="btn btn-default" data-toggle="modal" data-target="#confirm-submit">Submit</button>
<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Confirm</h2>
</div>
<div class="modal-body">
<p>Are you sure you want to submit?</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Yes, Submit</button>
<button type="submit" name="" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
Please help me..
In the modal footer [SOLVED]
<div class="modal-footer">
<button type="submit" class="btn btn-success">Yes, Submit</button>
<button type="submit" name="submit" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
As your first button on the form,
<button type="submit" name="submit" class="btn btn-default" data-toggle="modal" data-target="#confirm-submit">Submit</button>
opens the modal but doesn't submit the form as you have used e.preventDefault(); on that button click,
After modal opens once you click on 'Yes, Submit' it will submit the form again but this time isset($_POST['submit']) will be blank as the 'Yes, Submit' button doesn't have name.
Just add name to that button and it will work.
Final code will look like as above [SOLVED]
<script type="text/javascript">
$(document).on('submit', 'form', function(e){
e.preventDefault();
});
</script>
You (first) have a typo in the script tag (should be javascript).
Then you have to listen to the submit of the form to prevent it. You listen to a click of an element with the class submit which doesn't exist.
I have a search bar for users to enter a query. Upon clicking 'Search', a modal should appear with the query results.
My output from index.php is still not showing in the modal. When I click 'Search', the modal pops up with an empty body. How do I get my output from index.php to show in the modal's body?
index.php
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="#">
<input type="text" name="q" placeholder="Enter query"/>
<input type="button" name="search" value="Search" data-toggle="modal" data-target="#mymodal">
</form>
</body>
<script>
$('form').submit(function(e){
e.preventDefault() // do not submit form
// do get request
$.get( 'search.php', { q : },function(e){
// then show the modal first
$('#mymodal').modal('show');
// then put the results there
$('#mymodal:visible .modal-container .modal-body').html(e);
});
});
</script>
<!-- The Modal -->
<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
search.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];
//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= '<td><audio src="audio/'.$audio_name.'" controls="control">'.$audio_name.'</audio></td>';
}
}
echo $output;
}else{
"Please add search parameter";
}
mysqli_close($conn);
?>
Edit: Here is a screenshot: Modal Screenshot
Edit: I edited the code and I am no longer redirected to a new page when clicking the 'Search" button, but my output from index.php is still not showing in the modal. When I click 'Search', the modal pops up with an empty body. How do I get my output from index.php to show in the modal's body?
I have a search bar for users to enter a query. Upon clicking 'Search', a modal should appear with the query results.
My code isn't displaying anything in the modal window. When I click the 'Search' button, the modal window opens but immediately afterwards a new page is loaded with the query results.
How do change it so that the query results display in the modal window rather than a new page?
index.php
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form method="POST" action="#">
<input type="text" name="q" placeholder="Enter query"/>
<input type="button" name="search" value="Search" data-toggle="modal" data-target="#mymodal">
</form>
</body>
<script>
$('form').submit(function(e){
e.preventDefault() // do not submit form
// do get request
$.get( 'search.php', { q : },function(e){
// then show the modal first
$('#mymodal').modal('show');
// then put the results there
$('#mymodal:visible .modal-container .modal-body').html(e);
});
});
</script>
<!-- The Modal -->
<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
search.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once('db.php'); //Connect to database
if(isset($_POST['q'])){
$q = $_POST['q'];
//get required columns
$query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%' OR `yupikWord` LIKE '%$q%'") or die(mysqli_error($conn)); //check for query error
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<h2>No result found</h2>';
}else{
while($row = mysqli_fetch_assoc($query)){
$output .= '<h2>'.$row['yupikWord'].'</h2><br>';
$output .= '<h2>'.$row['englishWord'].'</h2><br>';
$output .= '<h2>'.$row['audio'].'</h2><br>';
$audio_name = $row['audio'];
$output .= '<td><audio src="audio/'.$audio_name.'" controls="control">'.$audio_name.'</audio></td>';
}
}
echo $output;
}else{
"Please add search parameter";
}
mysqli_close($conn);
?>
You can try this possible solution by following this changes
- Remove Form tag
- Add Onclick Listner on Search Button
- First put content in Modal and then show Modal
index.php
$(document).ready(function () {
$('.search').click(function () {
var q= $('#q').val();
// AJAX request
$.ajax({
url: 'search.php',
type: 'post',
data: {
q: q
},
success: function (response) {
// Add response in Modal body
$('.modal-body').html(response);
$('#mymodal').modal('show');
}
});
});
});
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='bootstrap/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
<!-- Script -->
<script src='jquery-3.1.1.min.js' type='text/javascript'></script>
<script src='bootstrap/js/bootstrap.min.js' type='text/javascript'></script>
</head>
<body>
<input type="text" id="q" name="q" placeholder="Enter query"/>
<input type="button" class="search" name="search" value="Search">
</body>
<!-- The Modal -->
<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#t').on('click','#btns',function() {
var id = $(this).attr('data-id');
var image = $(this).attr('data-image');
$('#dats').val(id);
$('#Mimage').val(image);
});
$(document).ready(function() {
$.ajax({
type: "POST",
url: "query1.php",
success: function(data) {
var parsed = $.parseJSON(data);
$.each(parsed, function(i,parsed) {
$('#t').append('<tr><td>'+parsed.id+'</td><td id="edi"><img src="upload/'+parsed.image+'" height = "50px" width = "50px"></td><td>'+'<button class="button info" name="btn" data-toggle="modal" data-target="#Mpn" id="btns" data-id="'+parsed.id+'" data-image="'+parsed.image+'">Edit</button>'+'<button class="button info1" name="btn" data-toggle="modal" data-target="#Mpd" id="btns" data-id="'+parsed.id+'" data-image="'+parsed.image+'">Delete</button>'+'</td></tr>');
});
}
});
$('body').on('click','#update',function(e) {
alert("hi");
var id = $('#dats').val();
alert(id);
var images = $('#Limage').val();
alert(images);
$.ajax({
type:"POST",
url:"upd.php",
data:{mode:'upd',id:id,images:images},
success:function(data){
alert(data);
alert("successfully updated");
}
});
});
});
</script>
<!Doctype html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="modal fade" id="Mpn" tabindex="-1" role="dialog" aria-labelledby="edit-modal-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form class="form-horizontal" id="edit-form">
<input type="hidden" id="dats" name="id" >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 align="center" class="modal-title" id="edit-modal-label" >Save Changes</h4>
</div>
<div class="modal-body">
<div class="form-group" enctype="multipart/form-data">
<label for="username" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="Mimage" name="image" />
</br>
<input type="file" class="form-control" id="Limage" name="image" placeholder=""required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="update" id="update">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
if(($_POST['mode'])=='upd') {
$id = $_POST['id'];
echo $id;
$images = $_POST['images'];
echo $images;
$query = mysqli_query($con,"UPDATE image SET image = '$images' WHERE id = '".$_POST["id"]."'");
<img class="img-responsive" src= "upload/3.jpg" alt="image" id="Mimage" name="image" />
if($query == true) {
echo "File Uploaded";
}
}
?>
Context:
This code is to update the image in the database.This is not working properly when i uploading the image the image enters into the database but as C:fakepathimg.jpg.It is not entering into the actual folder.If it is get rid of fake path the image will be updated.The image displays as fakepath.If the path is correct i think the the image will be updated.
[EDIT]just put the post data in a var and delete $scope.
$scope.deleteData = function(email) {
if(confirm("Are you sure you want to delete this record?")){
var email1= email; $http.post('<?php echo site_url("index.php/user/delete_user");?>', {'email':email1}).error(function(){ console.log("error"); });
}
}
whenever i press delete button nothing happens.
please check the code and please reply with the solution. network payload pass nothing {}.
view/user_view.php
<!DOCTYPE html >
<html lang="en" ng-app="userApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script type="text/javascript" src="<?php echo base_url('assets/jquery/jquery-3.1.1.min.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script>
<link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>">
<script type="text/javascript">
/*function userController($scope,$http) {
$scope.users = [];
$http.get('<?php echo site_url('index.php/user/user_list'); ?>').success(function($data){ $scope.users=$data; });
}*/
var app = angular.module('userApp', []);
app.controller('userController', function($scope, $http) {
//$scope.buttonName = "Add";
$scope.deleteData = function() {
/*$http.post("<?php echo base_url(); ?>index.php/User/delete_user/",{'id':$scope.id})
.success(function(data,status,headers,config){
$scope.message = "Deleted Successfully!";
});
*/
$http.post('<?php echo site_url('index.php/user/delete_user');?>',
{'email':$scope.email}).error(function(){
console.log("error");
})
}
$scope.displayUsers = function() {
$scope.users = [];
$http.get('<?php echo site_url("index.php/user/user_list"); ?>').success(function($data){ $scope.users=$data; });
}
$scope.insertData = function(){
$http.post('<?php echo site_url('index.php/user/insert_user');?>',
{
'name':$scope.name,
'gender':$scope.gender,
'email':$scope.email,
'address':$scope.address,
'phone':$scope.phone
}).error(function() {
console.log("Error");
});
}
});
</script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body ng-controller="userController">
<!-- ng-app : which tells the Angular framework to parse data from this div -->
<div class="container" ng-init="displayUsers()">
<button class="btn" data-toggle="modal" data-target="#myModal">Add Users</button>
<div class="row">
<div class="col-lg-12 col-md-12">
<input type="search" class="form-control" ng-model="searchUser" placeholder="Live Search">
<table class="table table-bordered table-condensed table-responsive" ng-controller="userController">
<thead>
<tr>
<td>Select</td>
<td>Name</td>
<td>Gender</td>
<td>Email</td>
<td>Address</td>
<td>Phone Number</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users | filter:searchUser">
<td><input type="checkbox"></td>
<td class="hidden">{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.gender}}</td>
<td>{{user.email}}</td>
<td>{{user.address}}</td>
<td>{{user.phone}}</td>
<td>
<button class="btn btn-primary">Edit</button>
<button class="btn" name="btnDelete" ng-click="deleteData(user.email)" value="delete">Delete</button></td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="col-md-12">
<form class="form form-horizontal">
<div class="form-group hidden">
<label for="name">id</label>
<input type="hidden" class="form-control" ng-model="id" id="name">
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" ng-model="name" id="name">
</div>
<div class="form-group">
<label for="gender">Gender</label>
<input type="radio" id="gender" ng-model="gender" value="male">Male
<input type="radio" id="gender" ng-model="gender" value="female">Female
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" ng-model="email" id="email">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" ng-model="address" id="address">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="text" class="form-control" ng-model="phone" id="phone">
</div>
<button type="button" class="btn btn-primary" ng-click="insertData()" type="submit">Insert</button>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
controller/User.php
<?php
defined('BASEPATH') OR EXIT ('DIRECT ACCESS IS NOT ALLOWED');
class User extends CI_Controller{
public function index(){
$this->load->view('user_view');
}
public function user_list(){
$data = $this->user_model->get_user_list();
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
public function insert_user(){
//$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$_POST = json_decode(file_get_contents('php://input'),true);
$data = $_POST;
$this->user_model->insert_user_data($data);
}
public function delete_user(){
$_POST = json_decode(file_get_contents('php://input'),true);
$email= $_POST['email'];
$this->user_model->delete_user_data($email);
}
}
?>
model/user_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model{
protected $table_name;
public function __construct(){
parent::__construct();
$this->table_name = 'user';
}
public function get_user_list(){
return $this->db->from($this->table_name)->get()->result_array();
}
public function insert_user_data($data){
$this->db->insert($this->table_name, $data);
}
public function delete_user_data($email){
$this->db->where('email',$email);
$this->db->delete('user');
}
}
?>
Network after click on delete.
The thing you are doing a wrong here is, passing email as an argument in function, you are not utilizing it:
$scope.deleteData = function(email) {
$http.post('<?php echo site_url('index.php/user/delete_user');?>',
{'email':email}).error(function(){
console.log("error");
})
}
use double quotes inside single quotes
$http.post('<?php echo site_url("index.php/user/delete_user");?>',
{'email':$scope.email}).error(function(){
console.log("error");
})