Opening a new page w/ dynamic data |
I'm trying to learn CI by converting an existing php app. In response to a click event I need to open a new page and pass an id to the controller. This allows the controller to populate the page with data, appropriate to the particular element of that class that was clicked. In the original version I just passed the ID to the controller along with the url (in the 'get' array) Here's the js/jq code in my new CI code version that registers the event and provides the initial response:
$('.tsEditButt').click(function() { var tuneID=$(this).closest('.tune').attr('id'); window.open('c_tunesheet','_blank'); $.post('c_tunesheet',{tuneID:tuneID},function(response) { alert('Response: '+response); // For testing the strategy }); }); The c_tunesheet controller to test this strategy: class C_tunesheet extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('url'); } public function index() { // index function loads new tunesheet editor page / with this tune's data $pageTitle='MZ TS Editor'; $tuneID=$this->input->post('tuneID'); echo('<div>C_tunesheet, tuneID:'.$tuneID.' PageTitle:'.$pageTitle.'</div>'); } } The problem: Firebug (and the alert message correctly appearing in the first page) shows the expected string returned to the js/jq calling function as: Code: <div>C_tunesheet, tuneID:1632 PageTitle:MZ TS Editor</div> But, the html that appears on the new page is: C_tunesheet, tuneID: PageTitle:MZ TS Editor i.e. the $tuneID value is not getting passed to the controller. I suspect I'm missing something very fundamental here re: what happens within the CI app when a new page is opened. Hopefully someone can point me in the right direction. Thanks in advance.
After examining this further I just realized that 'tuneID' is being passed to the controller in the post array and was successfully received. Otherwise it would not appear in the echoed response from the controller back to the jq $.post(), as it does.
But that still leaves the question: If tuneID is getting set in the post array and is converted to variable $tuneID and then gets echoed back to the .post() , why does it not appear on the 'c_tunesheet' page as well - as a result of the echo statement? Everything in the echo stmt except the value of $tuneID appears on the new page as expected. i.e. C_tunesheet: tuneID: PageTitle:MZ TS Editor
Your Javascript has this line of code before the AJAX call:
PHP Code: window.open('c_tunesheet','_blank'); The page title will always show up, because you hardcoded it. (03-03-2017, 03:14 PM)Wouter60 Wrote: Your Javascript has this line of code before the AJAX call: Thanks for your reply. This is the full js/jq function where I assume the problem lies: $('.tsEditButt').click(function() { //alert('tsEditButt clicked!'); var tuneID=$(this).closest('.tune').attr('id'); window.open('c_tunesheet','_blank'); $.post('c_tunesheet',{tuneID:tuneID},function(response) { alert('Response: '+response); // For testing }); }); Can you suggest what I need to do to make this code work? i.e. how do I open the new page and pass the ID variable to it?
(03-03-2017, 05:04 PM)codeguy Wrote:It does not work. Window.open open a new window for you with the data from controller c_tunesheet. You do not send any post data to the controller. With the $.post you send an ajax request and get back the value you wanted, but the other window does know about it .(03-03-2017, 03:14 PM)Wouter60 Wrote: Your Javascript has this line of code before the AJAX call: Question: - Do you need "POST" or can it be "GET"? - Do you need a new page? - what do you want?
03-04-2017, 10:39 AM
(This post was last modified: 03-04-2017, 10:46 AM by codeguy. Edit Reason: Clarity ) (03-03-2017, 11:49 PM)Paradinight Wrote:(03-03-2017, 05:04 PM)codeguy Wrote:It does not work. Window.open open a new window for you with the data from controller c_tunesheet. You do not send any post data to the controller. With the $.post you send an ajax request and get back the value you wanted, but the other window does know about it .(03-03-2017, 03:14 PM)Wouter60 Wrote: Your Javascript has this line of code before the AJAX call: PS: What you have told me is helpful and accurate. I agree with what you say is the reason my code does not work. I just can't figure out the right way to do this.
You can do this to open the new page with the right parameters:
Code: window.open('<?= base_url();?>c_tunesheet/index?tuneid='+tuneID,'_blank'); There's no need to set $config['enable_query_strings'] to TRUE. If you do, CI expects URL's like: Code: www.example.com/index.php?c=controllername&m=methodname[&extraparam=....]
(03-04-2017, 02:38 PM)Wouter60 Wrote: You can do this to open the new page with the right parameters: Use site_url() if it only one param e.g. tuneID Code: window.open('<?= site_url('c_tunesheet/edit/');?>' + tuneID ,'_blank');
Much thanks to both of you for the advice. I've spent time exploring your suggestions. Here's what I found:
With my uri_protocol set to either 'QUERY_STRINGS'; or 'REQUEST_URI'; in config.php and . . . using this window.open statement, 'window.open("<?= site_url('c_tunesheet/');?>"+tuneID,'_blank');' on the new page that appeared I got: Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the [email=postmaster@localhost]webmaster[/email]. Error 403 localhost Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9 I get a similar result using base_url() instead of 'site_url()' method. However I was able to pass data using the not-recommended method: window.open('c_tunesheet?tuneID='+tuneID,'_blank'); Note that I first tried: window.open('localhost/CID17/c_tunesheet?tuneID='+tuneID,'_blank'); In that case I got the good old "404 Page Not Found But using just: window.open('c_tunesheet?tuneID='+tuneID,'_blank'); It works with either uri-protocol above. I extracted the data and displayed it using: $tuneID=$this->input->get('tuneID'); echo "<div>c_tunesheet controller says: Tune ID: $tuneID"; . . . in the controller. I spent some trying to understand how I could modify my .htaccess or my php.ini file to allow access to wherever I was being denied but gave up after hitting the first few layers of confusion. Thanks to your hints, I now have a strategy that opens the new page and passes the tuneID. This is not data that could cause any security problems on a production site but I'm still hoping to find a better solution eventually. Another good thing is I can stick with $config['uri_protocol'] = 'REQUEST_URI'; since that's the default and I won't have to change anything elsewhere.
It's all because CI expects URL's to have at least 2 segments: controller and method. If the second segment is omitted, the controller will execute the index method by default.
Code: c_tunesheet/edit //means: open then c_tunesheet controller and find the edit method inside it The command Code: window.open("<?= site_url('c_tunesheet/');?>"+tuneID,'_blank'); This should work: Code: window.open("<?= site_url('c_tunesheet/index');?>"+tuneID,'_blank'); But then, your index method should look like this: PHP Code: public function index($tuneID = NULL) { = NULL means that the variable $tuneID will be set to NULL if no parameter is passed. |
Welcome Guest, Not a member yet? Register Sign In |