Codeigniter --- checkbox array issue - codeigniter

stuck in codeigniter code, checkbox array,
view code :
<input type="checkbox" name="company_type[]" value="Exporter" class="get_value"> Exporter
<input type="checkbox" name="company_type[]" value="Importer" class="get_value"> Importer
<input type="checkbox" name="company_type[]" value="Distributor" class="get_value"> Distributor
<?php
foreach($form as $company_type)
{
echo $company_type = implode(",",$company_type);
}
?>
=================================
model method:
public function save_registration_form($post)
{
return $this->db->insert('members', [ 'company_type' =>$post['company_type']]);
}
===========================================
controller code :
if ($this->form_validation->run() ) {
$this->model->form ($this->input->post() ) ;
==========================
code showing problem, please help to solve this

You have to chagne your code like this
<input type="checkbox" name="company_type[]" value="Exporter" class="get_value"> Exporter
<input type="checkbox" name="company_type[]" value="Importer" class="get_value"> Importer
<input type="checkbox" name="company_type[]" value="Distributor" class="get_value"> Distributor
Model Code
public function save_registration_form($post){
$company_type = implode(",",$post['company_type']);
return $this->db->insert('members', ['company_type' =>$company_type]);
}

Related

foreach error laravel multiple checkbox store

I store a few checkboxes as array in Laravel, but I get an error on Foreach. Can someone help me?
in model
public function setHandServiceAttribute($value)
{
$this->attributes['handservice'] = json_encode($value);
}
public function getHandServiceAttribute($value)
{
return $this->attributes['handservice'] = json_decode($value);
}
in checkbox
<div class="form-check mx-3">
<input class="form-check-input" type="radio" id="1" value="کاشت" name="handservice[]">
<label class="form-check-label" for="1">کاشت</label>
</div>
<div class="form-check mx-3">
<input class="form-check-input" type="radio" id="2" value="کاور" name="handservice[]">
<label class="form-check-label" for="2">کاور</label>
</div>
handservice is column of reservation table in database with value like this after store
["\u062a\u0631"]
and in view
#forelse($reservation->handservice as $hand)
{{ $hand }},
#empty
nothing
#endforlse
error
json_decode(): Argument #1 ($json) must be of type string, array given
I used this article but I get an error when one of the columns handservie is empty handservice in nullable
https://hackthestuff.com/article/how-to-store-multiple-checkbox-value-in-database-in-laravel8
It finally worked
in model
public function getHandServiceAttribute($value) {
return json_decode($value);
}
in view
#if($reservation->handservice != null)
#forelse($reservation->handservice as $hand)
{{$hand}},
#empty
نا مشخص
#endforelse
#else
<p class="text-danger">انتخاب نشده</p>
#endif
thanks all
You shouldn't try to assign something in a getter.
public function getHandServiceAttribute()
{
return json_decode($this->attributes['handservice']);
}

Submit POST data from controller

I am trying to implement Payhere payment gateway (Local gateway based in Sri Lanka). As per their documentation we could submit data as below.
<html>
<body>
<form method="post" action="https://sandbox.payhere.lk/pay/checkout">
<input type="hidden" name="merchant_id" value="121XXXX"> <!-- Replace your Merchant ID -->
<input type="hidden" name="return_url" value="http://sample.com/return">
<input type="hidden" name="cancel_url" value="http://sample.com/cancel">
<input type="hidden" name="notify_url" value="http://sample.com/notify">
<br><br>Item Details<br>
<input type="text" name="order_id" value="ItemNo12345">
<input type="text" name="items" value="Door bell wireless"><br>
<input type="text" name="currency" value="LKR">
<input type="text" name="amount" value="1000">
<br><br>Customer Details<br>
<input type="text" name="first_name" value="Saman">
<input type="text" name="last_name" value="Perera"><br>
<input type="text" name="email" value="samanp#gmail.com">
<input type="text" name="phone" value="0771234567"><br>
<input type="text" name="address" value="No.1, Galle Road">
<input type="text" name="city" value="Colombo">
<input type="hidden" name="country" value="Sri Lanka"><br><br>
<input type="submit" value="Buy Now">
</form>
</body>
</html>
This will redirect to url "https://sandbox.payhere.lk/pay/checkout" and opens a widow to submit card details.
But what I want to do is, send related POST data from a controller without a view (Same function as above, but through controller when after an database insertion done)
Any suggestion how to do this. I have tried using HTTP client, but since above gateway redirect to external url and popsup a window that's collect data.
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://sandbox.payhere.lk/pay/checkout', [
'form_params' => [
"merchant_id" => '1218XXX',
"return_url" => route('admin.dashboard'),
"cancel_url" => 'key-app.test',
"notify_url" => route('notify'),
"order_id" => '1',
"items" => 'Test',
"currency" => 'LKR',
"amount" => '50.00',
"first_name" => 'Arafath',
"last_name" => 'a',
"email" => 'mhmaarafath#gmail.com',
"phone" => '0765245237',
"address" => 'Mount Lavinia',
"city" => 'Colombo',
"country" => 'Sri Lanka',
]
]);
return redirect('https://sandbox.payhere.lk/pay/checkout');
First point you can't make a Guzzle request here since you need to open a new window here. (instead of Guzzle you can use HTTP client )
Yo can do something like below to accomplish your requirement
Get all related data from db inside controller and then return a view with that data where that view has the payhere form and you can hide the form with css. payhereCheckout ()
Now submit the form via JQuery or JS . What you have to do is just click form submit button via JS or JQuery. Just put a small loader or something which may show the user to that the server is processing the request.
I'll provide some codes how you need to achieve this. I have not used this process to PayHere but all the payment gateways in SL use a form submission .So that process should same.Here is the example code.
Controller
class PayHereChekoutControler extends Controller
{
public function payhereCheckout () {
$userData = UserDataModel::find(Auth::id());
$email = $userData->email;
$firstName = $userData->first_name;
//Other required paramas here or just pass the collection to view and get the required feilds there.
return view('payhere_hidden_checkout', compact(
'first_name',
'email',
));
}
}
web.php
Route::get('payhere-payment,'[PayHereChekoutControler::class, 'payhereCheckout']);
payhere_hidden_checkout.blade.php
<form id="payhere" style="display:none" method="post" action="https://sandbox.payhere.lk/pay/checkout">
<input type="text" name="first_name" value="{{$first_name}}">
<input type="text" name="email" value="{{$email}}">
<input type="submit" value="Buy Now">
</form>
<script>
$(document).ready(function () { // Now the DOM is ready
//Start loader or something here. You don't need to hide the loader because once form submitted it will redirect to payhere
setTimeout(() => {
$('form#payhere').submit();
}, 500)
});
</script>
I have not used this for Payhere but I have implemented this to WEBXPAY. Both the gateways use same process.
Hope this will make a scene to your requirement.

I am trying to add rating to a user and running in to error Call to a member function Review() on null. in laravel 5.8

i am trying to add rating and reviews to the user here is so far what i have done
i have made a separate controller for review
and in the store method of that controller my code is as follow
public function store(Request $request,User $user)
{
//
$up=User::find($user->id);
if($request->review)
{
$up->Review()->attach($request->review);
}
if($request->rating)
{
$up->Review()->attach($request->rating);
}
return redirect()->back();
}
i have created a seprate model for the review as well in which my code is looks like this
class Review extends Model
{
//
protected $fillable=['rating','review'];
Public function User()
{
return $this->belongsToMany(User::class);
}
}
where in my user model i have made this
Public function Review()
{
return $this->belongsToMany(Review::class);
}
where as my migrations looks like this
public function up()
{
Schema::create('review_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('rating');
$table->string('review');
$table->timestamps();
});
}
my forms looks like this
<form action="{{route('ReviewsUser',$user->id)}}" method="POST">
#csrf
#method('PUT')
<div class="feedback-yes-no">
<strong>Your Rating</strong>
<div class="leave-rating">
<input type="radio" name="rating" id="rating-1" value="1" checked/>
<label for="rating-1" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-2" value="2"/>
<label for="rating-2" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-3" value="3"/>
<label for="rating-3" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-4" value="4"/>
<label for="rating-4" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-5" value="5"/>
<label for="rating-5" class="icon-material-outline-star"></label>
</div><div class="clearfix"></div>
</div>
<textarea class="with-border" placeholder="Enter Your Comments" name="review" id="review" cols="7" required></textarea>
<!-- Button -->
<button class="button full-width button-sliding-icon ripple-effect" type="submit" >Your Review's <i class="icon-material-outline-arrow-right-alt"></i></button>
</form>
what i am trying to do is to store reviews that other users have given the user anyone who can help me overcome this error would be highly appreciated .
Call to a member function Review() on null
This error means that your variable $up calling the member function Review() has a null value. Which means it's not finding your user in
$up = User::find($user->id);
Your post route doesn't have a route parameter to pass the user ID to find the user in the first place.
Therefore, changing this
Route::post('Reviews','ReviewsController#store')->name('ReviewsUser');
to this
Route::post('Reviews/{user}','ReviewsController#store')->name('ReviewsUser');
will now capture the User ID in your controller via User $user

How to make Searching in Codeigniter where i have 3 categories of checkboxes?

How to make a search and get from database in codeigniter where i used 3 different checkbox categories..
This is what i have in my HTML Page...
<form action="<?php echo base_url('regularSearch') ?>" method="POST">
<h1>Marital Status</h1>
<label>
<input type="checkbox" name="marital_status[]" value="Never Married">
Never Married
</label>
<label>
<input type="checkbox" name="marital_status[]" value="Widowed">
Widowed
</label>
<label>
<input type="checkbox" name="marital_status[]" value="Divorced">
Divorced
</label>
<label>
<input type="checkbox" name="marital_status[]" value="Awaiting divorce">
Awaiting divorce
</label>
<h1>Mother Tongues</h1>
<label>
<input type="checkbox" name="motherTongue[]" value="English" checked>
English
</label>
<label>
<input type="checkbox" name="motherTongue[]" value="Polish">
Polish
</label>
<label>
<input type="checkbox" name="motherTongue[]" value="French">
French
</label>
<h1>Show profile</h1>
<label>
<input type="checkbox" name="showProfiles[]" value="Imagesecure_No" checked>
With Photo
</label>
<label>
<input type="checkbox" name="showProfiles[]" value="Horosc_YES">
With Horoscope
</label>
<label>
<input type="checkbox" name="showProfiles[]" value="online">
Online right now
</label>
<label>
<input type="checkbox" name="showProfiles[]" value="Premium_YES">
Premium members
</label>
<input type="submit" name="submit">
</form>
My Search Query
public function searchRegular()
{
$maritalArray = $this->input->post('marital_status');
$motherArray = $this->input->post('motherTongue');
$showProfArray = $this->input->post('showProfiles');
if($this->input->post('marital_status'))
{
foreach($maritalArray as $maritalRow)
{
$this->db->or_having('Marital_status', $maritalRow);
}
}
if($this->input->post('motherTongue'))
{
foreach($motherArray as $motherRows)
{
$this->db->or_having('Mother_tongue', $motherRows);
}
}
if($this->input->post('showProfiles'))
{
foreach ($showProfArray as $showProfRow)
{
$this->db->or_having('Image_secure', $showProfRow);
$this->db->or_having('Horoscope', $showProfRow);
$this->db->or_having('Online', $showProfRow);
$this->db->or_having('Premium_Memebers', $showProfRow);
}
}
$query = $this->db->get('users');
return $query->result();
}
Please Help... I spend 4 days to figure out how to do this....
Thanks in advance.
Based on your comment you say everything gets messed up if anybody chooses from the 3rd category
its clear that this get messed up - because you iterate over showProfiles without handling the data - which is simply wrong in your case, try the following
(i'm assuming the last category is saved as a 0/1 state in your db)
public function searchRegular()
{
$maritalArray = $this->input->post('marital_status');
$motherArray = $this->input->post('motherTongue');
$showProfArray = $this->input->post('showProfiles');
if($this->input->post('marital_status'))
{
foreach($maritalArray as $maritalRow)
{
$this->db->or_having('Marital_status', $maritalRow);
}
}
if($this->input->post('motherTongue'))
{
foreach($motherArray as $motherRows)
{
$this->db->or_having('Mother_tongue', $motherRows);
}
}
if($this->input->post('showProfiles'))
{
foreach ($showProfArray as $showProfRow)
{
switch($showProfRow)
{
case "Imagesecure_No":
$this->db->or_having('Image_secure', 1);
break;
case "Horosc_YES":
$this->db->or_having('Horoscope', 1);
break;
case "online":
$this->db->or_having('Online', 1);
break;
case "Premium_YES":
$this->db->or_having('Premium_Memebers', 1);
break;
}
}
}
$query = $this->db->get('users');
return $query->result();
}
If this doesn't help, you should provide an excerpt of your db table structure and explain what exactly you want.

how to post form with yui3

When i use yui3`s io-form module to post a form, i found the field value that server reseived is null...
Any help is welcome.
<form name='testajax' id="testajax1" >
<input type="text" name="test1" id="test1" ></input>
<input type="text" name="test2" >
<input type="text" name="test3" id="result" >
<input type="submit" value="submit" id="submit">
</form>
Y.io('/ajax/test',{
method:'POST',
form: {
id:Y.one('#testajax1'),
useDisabled: true,
},
on:{
complete:function(id,response){
Y.log(Y.one('#test1').get('value'));
},
start:function(id,response){
Y.log(Y.one('#test1').value);
}
}
});
You are passing a Y.Node to form.id and the docs indicate that it takes either a string or a "formObject" which I'm assuming means a "form element". I don't believe a Y.Node is a valid (which is an unfortunate API choice if true). Try switching your code to:
form: {
id: "#testajax1"
}
http://yuilibrary.com/yui/docs/io/#serializing-html-form-as-data

Resources