Inserting photo and get photo - php

Hi i want to make it possible when user sign up default photo upload in my DB right now i'm upload it but i don't know if its correct or not here its the code:
<?php
require_once 'includes/header.php';
$profile_pic = basename('files/img/default.jpg');
if (Input::exists()) {
//also here we check our token to see if input
if (Token::check(Input::get('token'))) {
//new instance
$validate = new Validate();
//now here we check our validation with check method
//fist of all we check if its POST then:
$validation = $validate->check($_POST,array(
//contain all rulles we want
//first of all in username as we set in our DB
'username' => array(
//its required
'required' => true,
//minimum character 2
'min' => 2,
//maximum character 20
'max' => 20,
//and we want it unique on users table
'unique' => 'users'
),
'name' => array(
'required' => true,
//minimum character 2
'min' => 2,
//maximum character 20
'max' => 50,
),
'email' => array(
'required' => true,
//minimum character 2
'min' => 10,
//maximum character 20
'max' => 50,
),
'password' => array(
'required' => true,
//minimum character 2
'min' => 6,
),
'SecondPassword' => array(
'required' => true,
//check for be match with password1
'matches' => 'password'
),
));
//is passed
if($validation->passed()){
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'username' => Input::get('username') ,
'name' => Input::get('name') ,
'email' => Input::get('email') ,
'password' => Hash::make(Input::get('password'),$salt) ,
'salt' => $salt,
'joined' => date('Y-m-d H:i:s') ,
'lastlogin' => date('Y-m-d H:i:s'),
'avatar' => $profile_pic
));
Session::flash('home','<span class="success-sign">Thank you for your
regsiteration Please log in</span> ');
Redirect::to('index.php');
} catch (Exception $e) {
die($e->getMessage());
}
}else {
//if error
foreach ($validation->errors() as $error) {
echo "<span class='error-sign'>$error</span><br>";
}
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="username" class='field-name'>Username</label><br>
<!--here in value we declare it to use get method from Input class to get
username for when we see error we dont miss this value and also for name -->
<input type="text" name="username" id="username"
value="<?php echo Input::get('username');?>" autocomplete="off"
onBlur ='checkUser(this)' class='signup'>
<span id='info'></span>
<br>
<label for="name" class='field-name'>Your name</label><br>
<input type="text" name="name"
value="<?php echo Input::get('name');?>" id="name" class='signup' >
<br>
<label for="email" class='field-name'>Your Email</label><br>
<input type='email' name='email' id="email" value="<?php echo
Input::get('email'); ?>"
class='signup'><br>
<label for="password1" class='field-name'>Password</label><br>
<input type="password" name="password" id="password" class='signup'>
<br>
<label for="secondpassword" class='field-name-re'>Retype Password</label>
<br>
<input type="password" name="SecondPassword" id="SecondPassword"
class='signup'>
<br>
<!--this is for token value-->
<input type="hidden" name="token" value="<?php echo Token::generate();?>">
<br>
<span class='fieldname'> </span>
<input type="submit" value="SignUp" class='info-signup'>
</form>
<br>
now as i said i see that in my DB the default.jpg is exists when user sign up but first i don't know if its photo or just name of photo and second i cant show this photo in user profile page

You Never want to actually save an Image's actual data into the database.
With that being said, your code stores the Path name into the database like how it should have been done.
Double check if you are using the correct path.
use
<img src="<?php echo $your_avatar_url; ?> />
to output your image.
look into How to have user upload the image here
http://www.w3schools.com/php/php_file_upload.asp

You need to actually save POSTed photo. Check out move_uploaded_file function.

Related

Validation is not working in codeigniter

Validation is not working. All the form fields are coming dynamically. It depends on the user how many fields he chooses.If he chooses 2 and it will display 2 fields in the view. If select 3 then it will display 3 fields and so on.I have more than 30 fields
I set 3 arrays(for testing purpose I set only 3. It will be total no of fields ) in my form validation page. If I remove the last array than validation is working because I am getting only 2 fields in the view. I am not able to use the more than 2 array in my form validation page.
Is it mandatory to require the number of fields in view is equal to a number of sets of rules array in form validation?
View
This is my dynamic view page
<?php
echo form_open('formbuilder_control/enxample_from_view');
foreach ($data as $key) {// I am getting output
$exp_fields_name=$key->fields_name;
$exp_fields_type=$key->fields_type;
$exp_form_elements=$key->form_elements;
$abc=explode(',',$exp_form_elements);
foreach ($abc as $value) {
if ($exp_fields_name == $value) {?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $value;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}}}?>
<div class="form-buttons-w btn_strip">
<input type="submit" name="submit" value="Save" class="btn btn-primary margin-10">
</div>
<?php echo form_close(); ?>
Form_validation.php
$config = array(
'test'=>array(
array('field' =>'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
),
);
Controller
public function test()
{
if($this->form_validation->run('test') == TRUE)
{
echo "working";
}
$this->load->view('test1');
}
Of course it will fail. Your validation rules define a 'middlename' field as required, and that field doesn't even exist in the form.
A missing field cannot satisfy a required rule.
It is possible to have flexible sets of rules with a minimum of code. Consider this form_validation.php example
<?php
$first_last = array(
array('field' => 'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
);
$middle_name = array(
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
);
$config = array(
'fullname' => array_merge($first_last, $middle_name), //first, middle and last name fields
'shortname' => $first_last, //only first and last name fields
);
This provides two different sets of fields for use with form_validation->run().
For example: On a form using first, middle and last name fields
if($this->form_validation->run('fullname'))
{
...
Or, when the form contains only the first and last name fields
if($this->form_validation->run('shortname'))
{
...
As others have mentioned, you are requiring a field that does not exist. You either need to:
Add a field in your view with the name middlename
Remove the validation rule that requires a field middlename to exist
Not contributing to your issue, but in a usability aspect, you likely want to change the label in your lastname and middlename rules to make them more user friendly.
$config = array('test'=>array(
array('field' =>'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array('field' => 'lastname',
'label' => 'lastname',
'rules' => 'required'
),
array('field' => 'middlename',
'label' => 'middlename',
'rules' => 'required'
)
),
);
Also, the documentation has other helpful tips for different custom rules if you're not trying to require the middlename but want to sanitize or validate its format prior to insertion into a database.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#form-validation-tutorial
Well, as you want to have one set of validation rules and you have two different forms to validate, you can do it using very nice option with Codeigniter's validation callback function.
I am going to post a simple example here:
Your view file:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo form_open('formbuilder_control/test');?>
<input type="text" name="firstname">
<input type="hidden" name="which_form" value="first_form">
<?php echo form_error('firstname'); ?>
<input type="text" name="lastname">
<?php echo form_error('lastname'); ?>
<input type="submit" name="submit" value="submit">
<?php echo form_close(); ?>
<?php echo form_open('formbuilder_control/test');?>
<input type="text" name="firstname">
<input type="hidden" name="which_form" value="second_form">
<?php echo form_error('firstname'); ?>
<input type="text" name="lastname">
<?php echo form_error('lastname'); ?>
<input type="text" name="middlename">
<?php echo form_error('middlename'); ?>
<input type="submit" name="submit" value="submit">
<?php echo form_close(); ?>
</body>
</html>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class YourController extends CI_Controller {
public function save()
{
//.... Your controller method called on submit
$this->load->library('form_validation');
// Build validation rules array
$validation_rules = array(
array(
'field' => 'A',
'label' => 'Field A',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'B',
'label' => 'Field B',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'C',
'label' => 'Field C',
'rules' => 'trim|xss_clean|callback_required_inputs'
)
);
$this->form_validation->set_rules($validation_rules);
$valid = $this->form_validation->run();
// Handle $valid success (true) or failure (false)
}
public function required_inputs()
{
if( $this->input->post('which_form') == "second_form" && !$this->input->post('middlename'))
{
$this->form_validation->set_message('middlename', 'Middle name is required!');
return FALSE;
}
return TRUE;
}
}
From above example you can see required_inputs function is just like a normal function, where you can write any php code.
So what I would advice is, have one hidden field in both of the forms, jus to check which form got submitted, and then set callback function for middlename validation rule and in that function, check which form is submitted by user, and based on it, return TRUE or FALSE.
I hope this will get you the whole idea what you can do.
You just need to add a hidden field with different value in each the forms and check the value in callback function to decide whether you should apply the third validation rule or not.

How to print a string into a array

I tried to print a string into a array.
The form will be submitted trough a array. The amount of forms is unknow because user is allowed to add extra inputs if needed (Order system).
To run my validation of the form it needs to be like this:
$toxValidate = new toxValidate();
$toxValidation = $toxValidate->check('$_POST', array(
'part1' => array('required' => TRUE),
'part2' => array('required' => TRUE),
'part3' => array('required' => TRUE),
'part4' => array('required' => TRUE),
'part5' => array('required' => TRUE)
));
But because we dont know how many inputs it will be, there needs for every input one line of: 'part1' => array('required' => TRUE),.
This is what i tried:
for($i=0; $i<count($part); $i++){
$parts .= "part".$i." => array('required' => true)";
$t0x = $t0x + 1;
if($t0x < count($part)){
$parts .= ', ';
}
}
This would be the var_dump output of $parts when submitting 3 forms
''part0' => array('required' => true), 'part1' => array('required' => true), 'part2' => array('required' => true)'
That looks exactly what needs to be printed in:
$toxValidation = $toxValidate->check($_POST, array(
#HERE#
));
i tried with print and echo which i allready tought wouldt work.
How can i print $parts into the array.
Update
HTML Form:
<div id="form">
<p class="order">
<label style="margin-left:20px;">Partnumber:</label><br />
<input type="text" style="margin-left:20px;" class="text medium" id="part" name="part[]" placeholder="SP partnumber" />
</p>
<p class="order" >
<label style="margin-left:20px">Qty</label><br>
<input type="text" id="qty" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />
<p class="order">
<label style="margin-left:20px">Price</label><br>
<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly /><br>
</p>
</div><p>
jQuery to add a other line of inputs:
$('#addpart').click(function(){
var loop = $('#loop').val();
var html;
html = '<p>';
html += '<input type="text" name="part[]" style="margin-left:20px;" class="text medium" id="part" placeholder="SP partnumber" />';
html += '<input type="text" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />';
html += '<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly />';
html += '</p>';
for (i = 0; i < loop; i++) {
$("#form").append(html);
}
});
Update 2
But there also needs to be other 'inputs' to be check. Like Purchase order, backorder, date. See below:
$toxValidation = $toxValidate->check('$_POST', array(
'po' => array('required' => TRUE),
'date' => array('required' => TRUE),
'bo' => array('required' => TRUE),
'remarks' => array('required' => TRUE),
#PARTS HERE#
));

Cannot catch value from form_hidden on codeigniter?

I try to create Update Form with form helper CI. everything Work if on form_input. But, when id on form_hidden, it return NULL. this the script
on View
$hidden = array('name'=>'id_hidden','value'=>$datacompany[0]->id);
echo form_hidden($hidden); //I have edited
On Controller
function edit_company()
{
if(isset($_POST['EDIT']))
{
print_r($_POST);//return All value
$isi = array(
'id' =>$this->input->post('id_hidden'),//return null
'nip' =>$this->input->post('nip'),//return value
'nama' =>$this->input->post('nama'), //return value
'golongan' =>$this->input->post('golongan') //return value
);
echo $isi['id']; //the result id is null
}//end if
}//end Function
That Id I need for using on model. How Can I Fix That?
How to Catch ID from form_hidden?
I'm very appreciated Your Answer
thanks
While using my first comment will let you debug it in 3 seconds, Here's the answer :)
You're using the form_hidden in the wrong way.
An array in the form_hidden turns to this (from the docs)
$data = array(
'name' => 'John Doe',
'email' => 'john#example.com',
'url' => 'http://example.com'
);
echo form_hidden($data);
// Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john#example.com" />
<input type="hidden" name="url" value="http://example.com" />
As you see, the 'keys' of the array turn to the names of the fields.
The value is the 'value' of the array. so in your example, you're making two hidden fields.
<input type="hidden" name="name" value="id_hidden">
<input type="hidden" name="value" value="$datacompany[0]->id">
You need to define like this a hidden field in CI:
$hidden = array('id_hidden',$datacompany[0]->id); // a name and value pair for a single instance.
echo form_hidden($hidden);
$hidden = array('id_hidden' => $datacompany[0]->id);
echo form_hidden($hidden);
I think this will fulfill your need.
Or if you want other attributes... Try with this..
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'type' => 'hidden',
'size' => '50',
'style' => 'width:50%',
);
echo form_input($data);
Depend on your need.

To add a class in codeigniter form

How to add a class in this codeigniter Pls any one help me to solve issue
I want to add this class in this line class="form-control"
<?php echo form_input('username', 'Username'); ?>
Please try this
echo form_input('username', 'Username',"class='class_name'");
Please read the docs for more info because the answer's there. (http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html).
You can pass form_input with an associative array with the attributes you want in it.
$attrs = array(
'name'=>'username',
'value'=>'Username',
'class'=>'form-control',
);
echo form_input($attrs);
try something like this
<?php echo form_input('username', 'Username','class="class_name"'); ?>
OR
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%',
'class' => 'class_name',
);
echo form_input($data);
// Would produce:
<input type="text" name="username" id="username" class="class_name" value="johndoe" maxlength="100" size="50" style="width:50%" />

cakephp generates wrong label for radio input id

When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)

Categories