I am using CakePHP 2.3.6. In a page, I have a checkbox. I want, when I select the checkbox(click on it), an ajax post request will be sent to a page, some data will be saved in the db, and after that a successful notification will appear. And when I deselect it(click on it again), an ajax post request will be sent to that page again, some data will be saved in the db, and after that a successful notification will appear. So, my view file is like this :
.
.
.
<form>
<label><input type="checkbox" class="item_id" />Save the data</label>
</form>
.
.
.
<?php $this->Js->get('.item_id')->event('onclick',$this->Js->request(array('controller'=>'items','action'=>'saveData',$item['Item']['id'],$another_id),array('async'=>true)));?>
In the saveData() function :
if($this->request->is('post')){
//save the data in the db
}
But, when I click on the checkbox, nothing happens.
I put the php code for the ajax call at the end of that specific view file, not the default layout. And, when I see the source code of the page from the browser("view source" option), I don't see any jQuery code that was supposed to be generated by CakePHP.
What is the problem ? What should I do here ? I need to save some data in my db and show some notification.
Thanks.
Have you included the Js helper in the controller like
public $helpers = array('Js' => array('Jquery'));
replace jquery with your javascript file.
Also try including this line just before the ending tag:
echo $this->Js->writeBuffer(); // Write cached scripts
to print the cache.
Related
I have a {{!! Form::model!!}} where I am passing a variable $values from the Controller and displaying it inside of the Form select those values. In mine main blade i have 3 additional blades I am including through
#if($jobs->open)
#include('jobs.review.open')
#endif
I am trying to load the data $jobs from controller only when someone clicks , I figured to do some by create an ajax call with onclick event, so i have the data $jobs back. An only now trying to include jobs.review.open blade.
Any help will be appreciated
One common strategy is to put the rendering in the AJAX call. The server returns a ready-to-go chunk of HTML in its reply, and the JavaScript code simply inserts it into the DOM at the appropriate place with innerHTML. The server uses a blade to prepare the HTML that it then returns.
The initial page HTML that is displayed doesn't include the content, but does include dummy placeholder <div>s which mark the places where the HTML will be inserted.
I'm struggling with just getting data from an ajax created radio button to correctly post the data at the click of a submit button, and I'm getting frustrated trying to figure out where I've gone wrong.
Am I wrong in thinking that once the ajax radio buttons have been properly created in my HTML driven website from my ajax backend shown here
while(odbc_fetch_row($rs)) {
$rowCarDeets = odbc_result($rs, 'CarDescript');
$response = $response . "<input type='radio' name='CarDescription' value='" . $rowCarDeets . "'>". $rowCarDeets . "</input></br>";
}
That all I need to do is grab it with a $_POST['CarDescription']? I have very little experience working with this, so if there is something that is supposed to be very obvious I need to add, please don't hesitate to suggest it just in case I did forget.
Just as a note, everything else in the form (not provided via ajax, simply taking user input) that these radio buttons are in get posted perfectly.
in $_POST, you should be able to just get the checked value by using the element's name attribute.
so: $_POST['CarDescription']
In my controller I put this code to check if the shopping cart is empty:
if (!$this->cart->contents()){
$this->session->set_flashdata('message', 'Your cart is empty!');
}
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');
On my "bookings" view I set this code to display the message:
<div id="infoMessage"><?php echo $message;?></div>
But on the first page load the message "Your cart is empty!" is not showing. However, it will display when I press the F5 key or refresh the browser.
I have already read the manual from codeigniter that says "CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared."
However, I don't know where to set this message so it will be available on "next server requrest".
There is no need to pass the data to the view and assign it as flashdata.
The reason that it's not working the first time, is that flashdata is only available for the next server request. Loading the views isn't a server request. However, refreshing the page is.
There are a couple of solutions to this:
Pass the data directly and load the view. (Not using flash data.)
Use flashdata and redirect to the page.
Passing the data directly
You could just pass it directly (personally I'd go with this option - I'm not sure why you'd want to use flashdata in this case, I would've thought that you'd always like to inform the user if their cart is empty...):
Controller:
if (!$this->cart->contents())
{
$this->data['message'] = 'Your cart is empty!';
}
$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');
View:
<div id="infoMessage"><?php echo $message;?></div>
Using flashdata
Or, if you want to use flashdata, then:
Controller:
if (!$this->cart->contents())
{
$this->session->set_flashdata('message', 'Your cart is empty!');
}
redirect('/your_page');
View:
<div id="infoMessage"><?php echo $this->session->flashdata('message');?></div>
If you want to use this method, you can't simply just load the view - due to the server request issue that I explained above - you have to redirect the user, using redirect('/your_page');, and the URL Helper $this->load->helper('url');. You can also send the appropriate HTTP header using the native PHP header function. Loading the view the first time won't work.
You can use this $this->session->keep_flashdata('message'); to preserve the data for an additional request.
I'd also check that your either auto-loading the session library or loading it in the constructor of your controller: $this->load->library('session'); (I'm fairly sure you will be, but it's worth checking anyway.)
Finally, I know that some people who store session data in their database can have issues with flashdata, so that may be worth looking into, if none of the above helps.
On a side note, personally I'd have a check to ensure that a variable is set before echoing it.
I'm having a website http://1008designs.in/ There in the home page I have built a Enquiry Form, The form usual validation works fine, but I need Jquery & Ajax validation, the form submits to 'enquiry/add', Pls help me on how to use Jquery Ajax in cakephp 2.0, I gone through a video of Andrew Perkins, but It didn't work for me. If I submit the form, then the whole home page is displayed on that Enquiry Div. I'm trying it from a week, but not working, pls help me as soon as possible.
When you submit form with ajax to controller enquiry/add the html returend by ajax request is a view associated with this enquiry/add. If you want to customize this html you have to modify add action in controller:
At the end of the action try this:
if($this->request->is('ajax'))
{
$this->autoRender = false;
$this->render('/elements/enquiry_success');
}
And in app/view/elements/ add enquiry_success.ctp containing some html/php code which will be returned to #succces div (for example <p>Enquiry added!!!</p>).
This code detects if request is ajax and id it is it doesnt render default view for action but some element.
We have a site that needs to have several sections be secure. We have
our SSL certificate installed, and for the areas that are accessible
via menu item, it's no problem - we just use the SSL Enabled system
parameter in the menu item editor. But we have a few sections (i.e. a
shopping cart checkout screen) that are only accessible via a submit
button (they don't have their own URL, so to speak - they're just
submitted to themselves via the controller and the view changes based
on the form action.) Right now, the form action is set like this:
<form name="instantForm" action="/<?=$this->segment?>/" method="post" onsubmit="updateSubmitValue()">
where segment is passed via the view.html.php. The rendered form tag
looks like this:
<form id = "checkoutForm" name="checkoutForm" action="/checkout/" method="post" onsubmit="updateSubmit()">
When submitted, the controller grabs the value of a few submitted
fields and determines which view to display (logged in with saved
account info or anonymous transaction) and then displays the correct
form.
Here's a stripped-down version of the controller's display method:
if (JRequest::getVar('checkoutCodeSubmitBTN') != ""){
//user has clicked Checkout button; go to billing info page
JRequest::setVar('view','checkoutpay');
// JRequest::setVar('view','checkout_thankyou');
//reference view
$viewCode =& $this->getView('checkoutpay','html');
$viewCode->voucher =& $voucher;
} //close test for step 1 if
How can I make sure that the view that gets displayed gets switched
over to an https URL?
I've already posted this on the google joomla dev discussion group, and got a response telling me to use JRoute to generate a URL and use setRedirect instead of posting to the form, but then someone else responded that using JRoute produces a completely new request, so all your access to JRequest::getVar type stuff is gone. We need to be able to access the variables that are posted through the form, so that solution is out. Does anyone have any other ways of doing this? I'm pretty new to Joomla development and am not familiar with many of the objects and methods available.
I've heard from some people that JRoute would be better for this, but that only works if you know the URL you need; we have to build our URL dynamically based on the current request, so I used JURI.
In my view.html.php, I added this code:
$needSecure = $model->needSecure();
if($needSecure) {
$u =& JURI::getInstance( JURI::base() );
$u->setScheme( 'https' );
$tmpURL = $u->toString()."checkout";
}
else {
$tmpURL = "/checkout";
}
$this->assignRef("tmpURL", $tmpURL);
needSecure() is a function in my model that pulls a value from a database table and returns a boolean. So if needSecure returns true, we get the current request URI, set the first part to https, then append the bit that we're submitting to. If it returns false, we just set the bit to submit to.
In the default.php, we have this:
<form id = "checkoutForm" name="checkoutForm" action="<?=$this->tmpURL?>/" method="post" onsubmit="updateSubmit()">
If needSecure is true, the action renders to
<form id = "checkoutForm" name="checkoutForm" action="https://www.mysite.com/checkout" method="post" onsubmit="updateSubmit()">
otherwise it renders to
<form id = "checkoutForm" name="checkoutForm" action="/checkout" method="post" onsubmit="updateSubmit()">
It works perfectly, and because we're storing the boolean in a database, it means we don't ever have to change the code itself if we want to make a new form submission secure or insecure.