unable to post json data , response message is SyntaxError: unexpected token < - php

also i did not get any console error and i check network tab i get 200 ok message that means data is posted.. please help
Here is jquery code
$(document).ready(function() {
$('#mc_subscribe').submit(function() {
$('#response').html('form submitting....');
var form = $(this);
$.ajax({
url: $(this).prop('action'),
type: 'POST',
dataType: 'json',
data: form.serialize()
}).done(function(data){
if (data.success == true){
$('#mc_subscribe')[0].reset();
$('#response').addClass('alert-success').removeClass('alert-danger').html(data.message);
window.setTimeout(function() {
window.location.href = data.redirectURL;
}, 2000);
} else{
$('#response').addClass('alert-danger').removeClass('alert-success').html(data);
}
}).fail(function( jqXHR, textStatus, data ) {
alert( "Request failed: " + data );
});;
return false;
});
});
Here is my codignitor controller code
class Subscribe extends CI_Controller {
private $data;
public function index() {
if ($this->input->is_ajax_request()){
$result = $this->_subscribe();
echo json_encode($result);
exit;
}
$this->load->view('header');
$this->load->view('pages/subscribe', $this->data);
$this->load->view('footer');
}
private function _subscribe(){
$success = true;
$redirectURL = base_url() + '/pricing';
if(!$this->input->post('FNAME')){
$message = "No Full Name provided";
$success = false;
}
if( empty($this->input->post('EMAIL')) || !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_POST['EMAIL']) ){
$message = "No Email address provided";
$success = false;
}
if($success && !$this->input->post('MOBILE')){
$message = "No Phone Number provided";
$success = false;
}
if($success && !$this->input->post('DAYTIME')){
$message = "No Day & Time provided";
$success = false;
}
if ($success){
$this->load->library('Mcapi', array(
'apikey' => $this->config->item('mailchimp_key')
));
$api = new Mcapi($this->config->item('mailchimp_key'));
$list_id = $this->config->item('mailchimp_list_id');
$merge_vars = Array(
'FNAME' => $this->input->post('FNAME'),
'MOBILE' => $this->input->post('MOBILE'),
'DAYTIME' => $this->input->post('DAYTIME')
);
if($api->listSubscribe($list_id, $_POST['EMAIL'], $merge_vars, '') === true) {
// It worked!
$message = 'Success!';
} else {
$success = false;
// An error ocurred, return error message
$message = 'Error: ' . $api->errorMessage;
}
}
return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL);
}
}
and here is HTML form
<div class="container">
<div class="col-md-12">
<form id="mc_subscribe" class="form-horizontal" action="http://localhost/CI-2/Subscribe/" method="post">
<fieldset>
<!-- Form Name -->
<legend>SUBSCRIBE</legend>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="FNAME" type="text" placeholder="Full Name" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="EMAIL" type="email" placeholder="Email" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="MOBILE" type="text" placeholder="Phone Number" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="DAYTIME" type="text" placeholder="Day & Time" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-8">
<input type="submit" name="submit" class="btn btn-success" value="SUBSCRIBE">
</div>
</div>
</fieldset>
</form>
<p id="response" class="alert text-center" align="center"></p>
</div>
</div>

change this below line
return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL);
TO
$data = array();
$data['message'] = $message ;
$data['success'] = $succcess;
$data['redirectURL'] = $redirectURL;
return json_encode($data);
in your controller.
it seems you are not returning the data in the json format,

Related

PHP Contact form not working with bootstrap html

I'm fairly new to web development and PHP and i've been having trouble trying to get my contact form to work with the Bootstrap template
I am getting an error:
Error 405(Method Not Allowed):
send # jquery.min.js:2 ajax # jquery.min.js:2
(anonymous) # contact.js:18 dispatch # jquery.min.js:2
y.handle # jquery.min.js:2
contact.php
<?php
$from = "#example.com";
$sendTo = "#example.com";
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
index.html
<div class="main-form">
<div class="container">
<div class="row">
<!-- contact form -->
<div class="col-md-offset-2 col-md-8">
<form class="form" id="contact-form" method="POST" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_name" type="text" name="name" placeholder="Name *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_email" type="email" name="email" placeholder="Email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="subject" name="subject" placeholder="Subject">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" placeholder="Message *" rows="4" required="required" data-error="Message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<input type="submit" class="button" value="Send message">
</div>
</form>
</div>
</div>
</div>
</div>
Thank you for any help
javascript
$(window).on("load",function (){
$(".loading").fadeOut(500);
// contact form
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
});
});

Angular ajax submit form

I am having problem with ajax submit form with angular, when i click submit my entire page is reloaded?
Where can be the problem, here is my code
/*
Create the controller and inject Angular's $scope
*/
// resource2Controller
(function () {
'use strict';
angular.module("aluPlanetApp").controller('resource2Controller', resource2Controller);
resource2Controller.$inject = ['$scope'];
resource2Controller.$inject = ['$http'];
function resource2Controller($scope, $http) {
activate();
function activate() {
$scope.title = 'Contact';
$scope.id = '2';
$scope.result = 'hidden'
$scope.resultMessage;
$scope.formData; //formData is an object holding the name, email, subject, and message
$scope.submitButtonDisabled = false;
$scope.submitted = false; //used so that form errors are shown only after the form has been submitted
$scope.submit = function (contactform) {
$scope.submitted = true;
$scope.submitButtonDisabled = true;
if (contactform.$valid) {
$http({
method: 'POST',
url: 'contact-form.php',
data: $.param($scope.formData), //param method from jQuery
headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //set the headers so angular passing info as form data (not request payload)
}).success(function (data) {
console.log(data);
if (data.success) { //success comes from the return json object
$scope.submitButtonDisabled = true;
$scope.resultMessage = data.message;
$scope.result = 'bg-success';
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = data.message;
$scope.result = 'bg-danger';
}
});
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley"> Please fill out all the fields.';
$scope.result = 'bg-danger';
}
}
}
}
})();
And contact-form.php
<?php
require_once 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['inputName']) && isset($_POST['inputEmail']) && isset($_POST['inputSubject']) && isset($_POST['inputMessage'])) {
//check if any of the inputs are empty
if (empty($_POST['inputName']) || empty($_POST['inputEmail']) || empty($_POST['inputSubject']) || empty($_POST['inputMessage'])) {
$data = array('success' => false, 'message' => 'Please fill out the form completely.');
echo json_encode($data);
exit;
}
//create an instance of PHPMailer
$mail = new PHPMailer();
$mail->From = $_POST['inputEmail'];
$mail->FromName = $_POST['inputName'];
$mail->AddAddress('something#test.com'); //recipient
$mail->Subject = $_POST['inputSubject'];
$mail->Body = "Name: " . $_POST['inputName'] . "\r\n\r\nMessage: " . stripslashes($_POST['inputMessage']);
if (isset($_POST['ref'])) {
$mail->Body .= "\r\n\r\nRef: " . $_POST['ref'];
}
if(!$mail->send()) {
$data = array('success' => false, 'message' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo);
echo json_encode($data);
exit;
}
$data = array('success' => true, 'message' => 'Thanks! We have received your message.');
echo json_encode($data);
} else {
$data = array('success' => false, 'message' => 'Please fill out the form completely.');
echo json_encode($data);
}
HTML
<div ng-controller="resource2Controller" class="panel-body">
<form ng-submit="submit(contactform)" name="contactform" method="post" action="" class="form-horizontal" role="form">
<div class="form-group" ng-class="{ 'has-error': contactform.inputName.$invalid && submitted }">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input ng-model="formData.inputName" type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name" required>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': contactform.inputEmail.$invalid && submitted }">
<label for="inputEmail" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input ng-model="formData.inputEmail" type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email" >
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': contactform.inputSubject.$invalid && submitted }">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input ng-model="formData.inputSubject" type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message" required>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': contactform.inputMessage.$invalid && submitted }">
<label for="inputMessage" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea ng-model="formData.inputMessage" class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..." required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default" ng-disabled="submitButtonDisabled">
Send Message
</button>
</div>
</div>
</form>
<p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>
</div>
Mhh I belive that maybe your problem is in the HTML which I can't see right now so make sure that you are not using the submit button. I mean Instead of <input type="submit"... use <input type="button"...Here is a reference How do I make an HTML button not reload the page
On your HTML, you call your controller ContactController while at the Angular side you call it resource2Controller. Those two should match or Angular doesn't know what to do.

Can we subscribe using MailChimp through localhost?

I am doing this first time. I want to ask, can I subscribe using MailChimp through my local server because I am having problem in doing that every time. When I subscribe I do not receive any mail. Here is the code
<?php
// change this path if the class file isn't in the same directory!
include_once 'MailChimp.php';
$alertclass = 'alert-warning';
$msg = '';
$name = '';
$email = '';
if (isset($POST['Submit'])) {
if (empty($_POST['name']) || empty($_POST['email'])) {
$msg = 'Please enter a name and email address.';
} else {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
/*
* Place here your validation and other code you're using to process your contact form.
*/
$mc = new \Drewm\MailChimp('xxxxxxxxx-us11');
$mvars = array('optin_ip'=> $_SERVER['REMOTE_ADDR'], 'FNAME' => $name);
$result = $mc->call('lists/subscribe', array(
'id' => 'f660c6ba5f',
'email' => array('email'=>$email),
'merge_vars' => $mvars,
'double_optin' => true,
'update_existing' => false,
'replace_interests' => false,
'send_welcome' => false
)
);
if (!empty($result['euid'])) {
$msg = 'Thanks, please check your mailbox and confirm the subscription.';
$alertclass = 'alert-success';
} else {
if (isset($result['status'])) {
switch ($result['code']) {
case 214:
$msg = 'You\'re already a member of this list.';
break;
// check the MailChimp API if you like to add more options
default:
$msg = 'An unknown error occurred.';
$alertclass = 'alert-error';
break;
}
}$msg="asdasdad";
}
}
}
?>
I am very new to this.
html code
<form class="form-horizontal"action="contactform-mailchimp.php" method="post">
<?php
//if ($msg != '')
echo '
<div class="alert '.$alertclass.'" role="alert">'.$msg.'</div>';
?>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">First name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="inputName">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" id="inputEmail">
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" name="message" id="inputMessage"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" name="newsletter"> Subscribe to newsletter
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="Submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>

Contactform.php not sending all inputs

I have a contact form that is working fine, it sends the email except that it doesn't post the email address of the person that sends it. I have no idea why, I tried changing the $from variable and nothing changes. Is there something obvious I am missing?
It is the input with the id c_email which is put into the $from variable that I do not receive in the emails sent by this form.
My contactform.php:
<?php
// Contact
$to = 'myemail#gmail.com';
$subject = 'Portfolio ContactForm';
if(isset($_POST['c_name']) && isset($_POST['c_email']) && isset($_POST['c_message'])){
$name = $_POST['c_name'];
$from = $_POST['c_email'];
$message = $_POST['c_message'];
if (mail($to, $subject, $from, $name, $message)) {
$result = array(
'message' => 'Sent, thanks!',
'sendstatus' => 1
);
echo json_encode($result);
} else {
$result = array(
'message' => 'Ooops, problem..',
'sendstatus' => 1
);
echo json_encode($result);
}
}?>
On my html page:
<form id="contact-form" role="form">
<div class="form-group">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Nom">
</div>
<div class="form-group">
<label class="sr-only" for="c_email">Email address</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Votre message"></textarea>
</div>
<button type="submit" class="btn btn-custom-1">
<i class="fa fa-bullhorn icon-before"></i> Envoyer
</button>
</form>
Your values is not posting because in your form you not mention method="POST".Just try like this.it should work
<form id="contact-form" role="form" action="" method="POST">
<div class="form-group">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Nom">
</div>
<div class="form-group">
<label class="sr-only" for="c_email">Email address</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Votre message"></textarea>
</div>
<button type="submit" class="btn btn-custom-1">
<i class="fa fa-bullhorn icon-before"></i> Envoyer
</button>
</form>
<script>
$('button').click(function() {
var c_name = $("#c_name").val();
var c_email = $("#c_email").val();
var c_message = $("#c_message").val();
$.ajax({//create an ajax request to load_page.php
type: "POST",
url: "assets/php/contactForm.php",
data:{"c_name":c_name,"c_email":c_email,"c_message":c_message},
success: function(data) {
if (data) {
alert(data);
}
else {
alert('Successfully not posted.');
}
}
});
});
</script>

Uploading image from one function

I have a edit function on my users codeigniter project, I would like to be able to use that one function for uploading images as well but user guide says to create a new function.
How am I able to make it with same edit() function.
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
// User userfile
if(!empty($this->input->post('userfile'))) {
$data['userfile'] = $this->input->post('userfile');
} else {
$data['userfile'] = $this->model_user->getUserImage($user_id);
}
$config['upload_path'] = base_url('image/catalog/'); // base_url('image/catalog/');
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
} else {
$data['upload_data'] = $this->upload->data();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View File
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $error ;?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $userfile;?>">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="userfile" value="<?php echo $userfile;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>
I have got it to work with out using codeigniter upload image helper easier this way.
controller function
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
if(!empty($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} else {
$data['image'] = $this->model_user->getUserImage($user_id);
}
$data['image_path'] = config_item('HTTP_CATALOG') . '/image/catalog/';
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $image_path . $image; ?>" style=" width: 100%; height: 300px;">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="image" value="<?php echo $image;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>

Categories