Cant access image data in cakephp 2.0 through custom form - html

I have created one function to add users. The parameters are username,email,password and profile image.
<form name="User" method="post" action="http://192.168.1.100/filmtastic/api/users/adduser" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td></tr></table>
</form>
Now in my UserController i have:
public function api_adduser() {
$this->layout = false;
$this->request->data['User']= $this->request->data;
if($this->request->data['User'] != array()) {
pr($this->request->data); die();
}
}
here i debug the data which is passed by that HTML form and optput is like
Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
[User] => Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
)
)
The problem is that it will not show me the image array. can you tell me how i will get that image array which suppose to look like
Array
(
[image] => Array
(
[name] => 06_01_2009_0692878001231258160_nanzig.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\php368F.tmp
[error] => 0
[size] => 81167
)
)

Maybe you simply forgot to set the form mime type to multipart/form-data. Here is my demo view, called add.ctp
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->password('password');
echo $this->Form->input('email');
echo $this->Form->file('image');
echo $this->Form->submit();
echo $this->Form->end();
Here is the UserController
class UserController extends AppController {
public $helpers = array('Html', 'Form');
public function add() {
pr($this->request->data);
}
}
And here is how the result looks like when you submit the form
Array
(
[User] => Array
(
[name] => admin
[password] => admin
[email] => asdf#qwerty.com
[image] => Array
(
[name] => cakephp-book.odt
[type] => application/vnd.oasis.opendocument.text
[tmp_name] => /tmp/phpKniokr
[error] => 0
[size] => 170247
)
)
)
Here is a dump of the generated HTML which you may want to use on your external page (may I ask why???). Note how many things the FormHelper adds... I wonder why you build an external view if your application is still accessing your server directly...
<form action="/test/cakedemo/user/add" enctype="multipart/form-data" id="UserAddForm"
method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<div class="input text">
<label for="UserName">Name</label><input name="data[User][name]"
id="UserName" />
</div><input name="data[User][password]" type="password" id="UserPassword" />
<div class="input text">
<label for="UserEmail">Email</label><input name="data[User][email]" type="text"
id="UserEmail" />
</div><input type="file" name="data[User][image]" id="UserImage" />
<div class="submit">
<input type="submit" value="Submit" />
</div>
</form>

Related

Failed to store data in laravel 8

I'm making a form so that users can request to create a new account to the admin, but every time I try to send data, it always fails, even though I've checked every part of my code, can anyone help me?
the form code :
<form method="POST" action="{{ url('/request-akun/make-request') }}">
#csrf
<div class="form-group col-md-12">
<label for="name">Nama</label>
<input type="text" name="name" class="form-control" id="name">
</div>
<div class="form-group col-md-12">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email">
</div>
<div class="form-group col-md-12">
<label for="password">Password</label>
<input type="text" name="password" class="form-control"
id="password">
</div>
<div class="form-group col-md-12">
<label for="password_confirm">Konfirmasi Password</label>
<input name="password_confirm" type="text" class="form-control"
id="password">
</div>
<button type="submit" class="btn btn-primary pull-right"
style="margin-right: 15px;">Request</button>
</form>
this is the controller :
public function requestAccount(Request $request){
$request->validate([
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required',
'password_confirm' => 'required'
]);
if ($request->get('password') == $request->get('password_confirm')){
$data = new requestAkun();
$data->name = $request->get('name');
$data->ra_user_id = Auth::id();
$data->ra_instansi_id = Auth::user()->instansiID;
$data->email = $request->get('email');
$data->password = $request->get('password');
$data->status = "pending";
$data->save();
return Redirect::to('/request-akun/request-lists')->with('success','Request Berhasil dikirim');
} else {
return Redirect::to('/request-akun/request-lists')->with('error','Password tidak sama');
}
}
this is the model code:
class requestAkun extends Model
{
use HasFactory, Uuids;
protected $table = 'request_akun';
public $timestamps = true;
protected $fillable = [
'name',
'ra_instansi_id',
'ra_user_id',
'email',
'password',
'status'
];
}
this is the route :
Route::prefix('request-akun')->group(function(){
Route::post('/make-request',[userController::class,'requestAccount'])->name('make-request');
});
Error is you don't have ra_instansi_id input in your form.
Not showing any validation error message in your form so better show errors so you check which validation failed.
3.Instead of checking password match in if condition you can change input name password_confirm to password_confirmation so you can change like below
<input name="password_confirmation" type="text" class="form-control"
id="password_confirmation">
and in your validation
$validator=Validator::make($request->all(),[
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required|confirmed',
'password_confirmation' => 'required'
]);
dd($validator->errors());

Wordpress. Insert form values to custom table

How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')

Image doesn't load after encoding blob from database(php,mysql,laravel)

I'm trying to create a picture for every contact.
this is the store() method in my controller:
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
Kontakt::create($attributes);
return redirect('/imenikk');
}
this is the create.blade.php:
<form method="POST" action="/imenikk">
{{ csrf_field() }}
<div class="container text-center">
<label for="ime">Ime</label>
<input type="text" class="form-control text-center" name="ime"
placeholder="Ime">
<label for="prezime">Prezime</label>
<input type="text" class="form-control text-center" name="prezime"
placeholder="Prezime">
<label for="broj">Broj</label>
<input type="text" class="form-control text-center" name="broj"
placeholder="Broj">
<br>
<input type="file" name="avatar" accept="image/*">
<br>
<input type="hidden" name="user_id" value='{{$user_id}}'>
<button type="submit" class="btn btn-primary">Dodaj
</div>
</form>
And this is on show.blade.php:
<img src="data:image/jpg;base64,{{ chunk_split(base64_encode($imenikk->avatar)) }}" height="500" width="500">
<img src ="data:image/jpeg;base64,{{base64_encode($imenikk->avatar)}}" height="200" width="200">
Neither of these 2 are working.. I get this but the image doesn't load:
<img src="data:image/jpg;base64,SU1HXzYxODQuSlBH" height="500" width="500">
This is how my db looks when I create a contact with picture:
Also I have managed to decode this
img src="data:image/jpg;base64,SU1HXzYxODQuSlBH
and I get the image name when I decode it yet the page still doesn't display the image.
It seems your base64 code is wrong.
Try to get the real base64 code from image file, and store in database.
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
$file = request()->file('avatar');
$imagedata = file_get_contents($file->getRealPath());;
$base64 = base64_encode($imagedata);
$attributes['avatar'] = $base64;
Kontakt::create($attributes);
return redirect('/imenikk');
}
then store it in your field,
and display it like this:
<img src={{ "data:image/jpg;base64,".$imenikk->avatar }} height="500" width="500">

how to modify parameters like (style , class ..) in cakephp input?

in cakephp
echo $this->Form->input('email', array('type' => 'email'));
will render
<div class="input email">
<label for="UserEmail">Email</label>
<input type="email" name="data[User][email]" value="" id="UserEmail" />
how to make this like that
<input type="email" name="data[User][email]" value="" id="UserEmail" class="input_class" style="some:style;" />
Just add a "class" and/or "style" argument to your options array.
echo $this->Form->input('email', array('type' => 'email', 'class' => 'input_class', 'style' => 'some:style' ));
See the the FormHelper documentation for a list of all options.
if you need only input without lable you can also try in this way
echo $this->Form->input('email', array('type' => 'email','div'=>false, 'class' => 'input_class', 'style' => 'some:style' ));

http request on file input using post

What, precisely is submitted to the webserver in a file upload http request?
ie. If I have the following form,
<form action=%URL% method="POST" enctype="multipart/form-data">
<input name="fileinfo" type="file" /><br />
<input name="submit" type="submit">
</form>
What is the request that will be sent to the server?
will be something like below... just use print_r to see what is going into your script
echo "<pre>";
print_r($_GET);
print_r($_POST);
print_r($_FILES);
[fileinfo] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)