AngularJS | How to Send the Data of Json to database in Codeigniter - php

I am trying to use Angular.
I am trying to Post Form of Angular, which have been done successfully, it is posting in JSON, But Problem is it only Post the input fields which have values, if i have NOT filled a textbox then it will NOT post that textbox value and fieldname, so how to know in controller which input fields have been posted.
Also how to send it to the model??
Here what i have done so far.
View.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<link href="<?php echo base_url(); ?>/styles/formcss.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo base_url(); ?>/scripts/angular.min.js"></script>
</head>
<body ng-app>
<div ng-controller="controller">
<form>
<label for="name">UserID:</label> <input type="text" name="UserID" ng-model="user.UserID" value=""><br>
<label for="UserName">Name:</label> <input type="text" name="UserName" ng-model="user.UserName" value=""><br>
<label for="rollno">In Game Name:</label> <input type="text" name="GameName" ng-model="user.InGameName" value="" ><br>
<label for="father">CNIC Number:</label> <input type="number" name="Cnic" ng-model="user.Cnic" value="" ><br>
<label for="age">Age:</label> <input name="Age" ng-model="user.Age" type="number" value="" ><br>
<label for="email1">Email:</label> <input name="Email" ng-model="user.Email" type="email" value="" ><br>
<label for="email2">Res enter Email:</label> <input id="email2" type="email" name="email2" value="" ><br>
<label for="pass1">Password:</label> <input id="pass1" type="password" name="Pass" ng-model="user.Password" value=""><br>
<label for="pass2">Re enter Password:</label> <input id="pass2" type="password" name="pass2" value="" ><br>
<label style="margin-bottom:-10px; " for="male">Male</label><input class="gen" ng-model="user.Gender" id="male" type="radio" name="g" value="Male"><br>
<label style="margin-bottom:15px;" for="female">Female</label><input class="gen" ng-model="user.Gender" id="female" type="radio" name="g" value="Female"><br>
<label class="dis" for="about" >About:</label> <textarea rows="5" cols="40" id="about" type="text" value=""></textarea><br>
<button style="margin-left: 225px;" ng-click="save()" >Save</button>
</form>
</div>
<script>
function controller ($scope,$http)
{
var angularform="<?php echo base_url(); ?>index.php/datacon/angform"
$scope.save=function()
{
$http.post(angularform, $scope.user);
}
}
</script>
</body>
</html>
Controller:
public function angform()
{
$get=file_get_contents('php://input');
$json_get=json_decode($get);
foreach($json_get as $key=>$value){
// i think something it has to do here.
}
$this->load->model('datamodel');
$this->datamodel->insert($data);
}
here is the screenshot, i have filled three fields so it posted the values of three fields only.

public function angform()
{
$get=file_get_contents('php://input');
$json_get=json_decode($get);
foreach($json_get as $key=>$value){
$data[$key]=$value; //Worked For Me.
}
$this->load->model('datamodel');
$this->datamodel->insert($data);
}

i check like this :-
if($this->input->get_post('male'))
{
// rest code goes here
}

Change your input name to match exactly like the database attribute(column) name. In that way you will not have to check anything:
$data = $this->input->post(null); #get the post array
$this->db->insert('table', $ata);

Related

How can I replace a from value using a cookie?

I want to use cookies to remember the usernames I add and replace the default value with the last used username.
I have the following html code:
<link href = "login.css" rel = "stylesheet">
<html>
<head>
</head>
<body>
<form action="login.php" method="post">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" value ="?" name="uname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</form>
</body>
</html>
this is login.php:
<?php
echo("Success!");
if($_SERVER["REQUEST_METHOD"] == "POST"){
setcookie("username", $_POST["uname"], time()+30*24*60*60);
}
?>
I want to replace the '?' from
<input type="text" placeholder="Enter Username" value ="?" name="uname" required>
with the value held by the cookie.
This is what I do in value.php:
<?php
if(isset($_COOKIE["username"]))
echo $_COOKIE["username"];
?>
Can I do something like:
<input type="text" placeholder="Enter Username" value ="value.php" name="uname" required>
so I don't have to write the php code there? This doesn't work, the value I get in the form is "value.php"
I'm not too familiar with PHP, but can't you just do:
<input type="text" placeholder="Enter Username" value="<?php include 'value.php'?>" name="uname" required>
or something like that?

html form-- need help having new input area populate if radio button is clicked

I am making a basic new customer database with mysql and php. I would like when people click on the radio button"different mailing address" for another couple of input fields to appear for the mailing address. Im not quite sure how to handle this with inputs and not variables. Is there a way to do an if statement here is my html form code below
<form method="POST" action="insert.php">
<fieldset>
<legend>New Customer data</legend>
<label>Complete Below</label>
<div class="controls controls-row">
<input class="span4" name="firstname" type="text" placeholder="First name">
<input class="span3" name="lastname" type="text" placeholder="Last Name">
<input class="span3" name="phone" type="text" placeholder="Phone">
</div>
<div class="controls controls-row">
<input class="span4" name="address" type="text" placeholder="Address">
<input class="span2" name="city" type="text" placeholder="City">
<input class="span1" name="state" type="text" placeholder="State">
</div>
<div class="controls controls-row">
<input class="span4" name="zip" type="text" placeholder="Zip Code">
<input class="span2" name="email" type="text" placeholder="Email">
<input class="span2" name="ccemail" type="text" placeholder="cc email">
</div>
<span class="help-block">When is the customers due date monthly</span>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios1" value="1" checked>
1st of the month</label>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios2" value="15">
15th of the month
</label>
<span class="help-block">Mailing address</span>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios3" value="1" checked>
Check if mailing address is the same as the service address
</label>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios4" value="0">
Check if mailing address is different
</label>
<button type="submit" class="btn btn-primary">Submit</button>
</fieldset>
</form>
Using jQuery, have two inputs that are initially hidden. Once the user checks the box, '$.show()' on those two inputs. Id have a div containing two inputs, and just show or hide the div depending on whether the box is checked or not
you can try this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" >
</script>
<script>
$(document).ready(function(){
$("#checkme").on('change',function(){
if($("#checkme").is(":checked")){
$("input#myemail").fadeIn();
}else{
$("input#myemail").fadeOut();
}
});
});
</script>
</head>
<body>
show: <input type="checkbox" id="checkme" /><br />
<input type="text" id="myemail" hidden />
</body>
</html>

How to get the page title into a input's value?

I want my hidden input field to get the page title as its value.
This is my testmail.php code.
<p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>
and this is my form code
<form action="testmail.php" method="post" class="cf">
<label for="name">* Name:</label>
<input type="text" name="name" placeholder="Your Name">
<input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">
<label for="email">* Email:</label>
<input type="text" name="email" placeholder="Enter a valid Email Address">
<label for="phone"> Phone:</label>
<input type="text" name="phone" placeholder="Enter areacode and number">
<label for="message">* Message:</label>
<textarea name="message"></textarea>
<button type="input">Send</button>
</form>
Is there a way to get the title automatically?
Here's a pure javascript way using the onsubmit event.
<form onsubmit="this.subject.value=document.title;">
<input type="text" name="subject" value="" />
<input type="submit" />
</form>
To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.
Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):
<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">
<label for="name">* Name:</label>
<input type="text" id="name" name="name" placeholder="Your Name" />
<input type="hidden" name="subject" value="" />
<label for="email">* Email:</label>
<input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />
<label for="phone"> Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />
<label for="message">* Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
This will set the value of your hidden input field once the page has finished loading.
<script>
$(function() {
$('input[name="subject"]').val($('title').text());
});
</script>
You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form
$('your_form_selector').on('submit', function(){
$('<input />').attr('type', 'hidden')
.attr('name', 'title')
.attr('value', document.title)
.appendTo($(this));
});

AngularJS, Databind Multiple Form Fields

Ok i have a Edit Form in AngularJS and i want show data in textboxes through angular databindings.
Here is the Form which i have done so far.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<link href="<?php echo base_url(); ?>/styles/formcss.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo base_url(); ?>/scripts/angular.min.js"></script>
</head>
<body ng-app>
<div ng-controller="controller">
<form>
<label for="name">UserID:</label> <input type="text" name="UserID" ng-model="user.UserID" value=""><br>
<label for="UserName">Name:</label> <input type="text" name="UserName" ng-model="user.UserName" value=""><br>
<label for="rollno">In Game Name:</label> <input type="text" name="GameName" ng-model="user.InGameName" value="" ><br>
<label for="father">CNIC Number:</label> <input type="number" name="Cnic" ng-model="user.Cnic" value="" ><br>
<label for="age">Age:</label> <input name="Age" ng-model="user.Age" type="number" value="" ><br>
<label for="email1">Email:</label> <input name="Email" ng-model="user.Email" type="email" value="" ><br>
<label for="email2">Res enter Email:</label> <input id="email2" type="email" name="email2" value="" ><br>
<label for="pass1">Password:</label> <input id="pass1" type="password" name="Pass" ng-model="user.Password" value=""><br>
<label for="pass2">Re enter Password:</label> <input id="pass2" type="password" name="pass2" value="" ><br>
<label style="margin-bottom:-10px; " for="male">Male</label><input class="gen" ng-model="user.Gender" id="male" type="radio" name="g" value="Male"><br>
<label style="margin-bottom:15px;" for="female">Female</label><input class="gen" ng-model="user.Gender" id="female" type="radio" name="g" value="Female"><br>
<label class="dis" for="about" >About:</label> <textarea rows="5" cols="40" id="about" type="text" value=""></textarea><br>
<button style="margin-left: 225px;" ng-click="save()" >Save</button>
</form>
</div>
<script>
function controller ($scope,$http)
{
var angularform="<?php echo base_url(); ?>index.php/datacon/angform"
$scope.save=function()
{
$http.post(angularform, $scope.user);
}
}
</script>
</body>
</html>
Here is the Json Array I am Getting From the Controller
JSON:
{"datas":[{"UserID":"168","UserName":"fdaa","Cnic":"23424","Email":"pak#gmail.com","Password":"asdad","Age":"12","Gender":"Male","Picture":null,"InGameName":"fds","ContactID":null,"GroupID":null,"ClanID":null}]}
But How to show these values in TextBoxes through Angular Data bindings?
Or if i am Doing Something Wrong Please Redirect me to my mistakes so i could solve my mistakes.
I am not sure whether I am getting you right or not. But I think If you are using $scope and ng-model, they are doing their job perfectly. You are using absolutely correct.
With the $http.post() you can save the data. At the same time data will be present in the text boxes.
What exactly you want to do. Please explain a bit more so that folks can try to explain you more.

formname.submit not working on IE8

I am currently developing an app and i am getting difficulties on IE8.
I have the following code:
<form class="fillClaimForm" name="fillClaimForm" method="POST" enctype="multipart/form-data" action="<?php print BASE_URL; ?>?action=insertBlogger" >
<label for="fblogName">Blog's name: </label>
<input type="text" name="fblogName" id="fblogName" class="cfInput" placeholder ="Complex" required />
<label for="fblogUrl">URL: </label>
<input type="text" name="fblogUrl" id="fblogUrl" class="cfInput" placeholder ="www.complex.com" required />
<label>Blog's logo: </label>
<div class="upload-file-container"><span>UPLOAD</span>
<input type="file" name="fblogLogo" id="fblogLogo"/>
<p class="ErrLoad error" style="display:none;">Please load your logo</p>
</div>
<label for="fEmail">Email adresse: </label>
<input type="email" name="fEmail" id="fEmail" class="cfInput" required />
<label for="frNum">Phone number: </label>
<input type="tel" name="frNum" id="frNum" class="cfInput" required />
<input type="hidden" name="idVid" id ="idVid" value=""/>
<input type="hidden" name="idRubrique" id ="idRubrique" value=""/>
<button id="sendClaim" class="sendClaim">SEND</button>
Consequently, I am doing a form submission using jquery:
......submit(function(){
validate=false;
formInfo = validateForm(validate,$thisLi);
if(formInfo.validate){
**fillClaimForm.submit();**
}
return false;
});
It is working on all browsers except IE8
Can someone kindly help me.
Thanks
Close your form.
Also give the same name as the id for your form and use
$("#formId").submit();
Code
<form class="fillClaimForm" name="fillClaimForm" method="POST" enctype="multipart/form-data" action="<?php print BASE_URL; ?>?action=insertBlogger" >
<label for="fblogName">Blog's name: </label>
<input type="text" name="fblogName" id="fblogName" class="cfInput" placeholder ="Complex" required />
<label for="fblogUrl">URL: </label>
<input type="text" name="fblogUrl" id="fblogUrl" class="cfInput" placeholder ="www.complex.com" required />
<label>Blog's logo: </label>
<div class="upload-file-container"><span>UPLOAD</span>
<input type="file" name="fblogLogo" id="fblogLogo"/>
<p class="ErrLoad error" style="display:none;">Please load your logo</p>
</div>
<label for="fEmail">Email adresse: </label>
<input type="email" name="fEmail" id="fEmail" class="cfInput" required />
<label for="frNum">Phone number: </label>
<input type="tel" name="frNum" id="frNum" class="cfInput" required />
<input type="hidden" name="idVid" id ="idVid" value=""/>
<input type="hidden" name="idRubrique" id ="idRubrique" value=""/>
<button id="sendClaim" class="sendClaim">SEND</button>
</form>
$("#sendClaim").click(function(){
validate=false;
formInfo = validateForm(validate,$thisLi);
if(formInfo.validate){
$("#fillClaimForm").submit();
}
return false;
});
Try this:
document.forms.fillClaimForm.submit();
Or if the ...... in your code represents creating of a submit handler on that same form you can probably just say this.submit().

Categories