• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening a new page w/ dynamic data

#11
(03-05-2017, 11:48 PM)Wouter60 Wrote: 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
c_tunesheet/edit/44  //means: find the edit method (like the first example) and pass the value 44 to the first parameter
c_tunesheet  //means: open the c_tunesheet controller and find the index method

The command
Code:
window.open("<?= site_url('c_tunesheet/');?>"+tuneID,'_blank');
will assume that tuneID is the name of the method!

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) {
 
 //your code here

to pass the parameter!
= NULL means that the variable $tuneID will be set to NULL if no parameter is passed.

Thanks for your patience. This is really helpful.

Re:
Code:
c_tunesheet/edit   //means: open then c_tunesheet controller and find the edit method inside it
c_tunesheet/edit/44  //means: find the edit method (like the first example) and pass the value 44 to the first parameter
c_tunesheet  //means: open the c_tunesheet controller and find the index method

Yes, I see now that of course using url segments to pass data I can't leave out the function name 'index' and  expect CI to know that I wanted the default index function. And therefore:

The command
Code:
window.open("<?= site_url('c_tunesheet/');?>"+tuneID,'_blank');
will assume that tuneID is the name of the method!  Obvious now.[Image: huh.gif]

You said, "This should work:"
Code:
window.open("<?= site_url('c_tunesheet/index');?>"+tuneID,'_blank');

Yes, but still I get that error:

Code:
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
[url=http://localhost/]localhost[/url]
Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9

However, if I use:

Code:
window.open('c_tunesheet/index/'+tuneID,'_blank');

The code works as you suggest it should. The new page url now appears as:

http://localhost/CID17/c_tunesheet/index/1632

. . . and the content appears correctly as:

Code:
c_tunesheet controller says: Tune ID: 1632

I don't know why using site_url() produces that error but I'll try to figure that out as a separate issue.

Thanks again for your help.[Image: cool.gif]
Reply

#12
Wouter60 made a little misstake Smile
It missing a /

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');
Reply

#13
Very confusing.
If I use base_url() . 'controller/method', the function base_url() itself ends with a /
Reply

#14
(03-06-2017, 11:34 AM)Paradinight Wrote: Wouter60 made a little misstake Smile
It missing a /

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');

When I try . .

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');
. . . I still get that [b]Access forbidden![/b] error.

I notice that the url that appears in the browser for that page that opened is now:

[b]http://localhost/CID17/+<?php site_url('c_tunesheet/index/');?>+1632[/b]

Is that possibly because this is inside a .js file and not a .php file?

i.e. The browser seems not to know what to do with the php insert before it executes the window.open().

Damn, for a while I couldn't figure out how to turn on Code formatting. Now I can't shut it off. [Image: confused.gif]
Reply

#15
(03-06-2017, 12:11 PM)codeguy Wrote:
(03-06-2017, 11:34 AM)Paradinight Wrote: Wouter60 made a little misstake Smile
It missing a /

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');

When I try . .

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');
. . . I still get that [b]Access forbidden![/b] error.

I notice that the url that appears in the browser for that page that opened is now:

[b]http://localhost/CID17/+<?php site_url('c_tunesheet/index/');?>+1632[/b]

Is that possibly because this is inside a .js file and not a .php file?

i.e. The browser seems not to know what to do with the php insert before it executes the window.open().

Damn, for a while I couldn't figure out how to turn on Code formatting. Now I can't shut it off. [Image: confused.gif]

php in js is a bad idea Big Grin

make in the <head></head> of you document a

Code:
<script>
var base_url = <?=base_url();?>
</script>

in you js fie

Code:
window.open(base_url+"c_tunesheet/index/"+tuneID,'_blank');
Reply

#16
(03-06-2017, 01:19 PM)Paradinight Wrote:
(03-06-2017, 12:11 PM)codeguy Wrote:
(03-06-2017, 11:34 AM)Paradinight Wrote: Wouter60 made a little misstake Smile
It missing a /

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');

When I try . .

Code:
window.open("<?= site_url('c_tunesheet/index/');?>"+tuneID,'_blank');
. . . I still get that [b]Access forbidden![/b] error.

I notice that the url that appears in the browser for that page that opened is now:

[b]http://localhost/CID17/+<?php site_url('c_tunesheet/index/');?>+1632[/b]

Is that possibly because this is inside a .js file and not a .php file?

i.e. The browser seems not to know what to do with the php insert before it executes the window.open().

Damn, for a while I couldn't figure out how to turn on Code formatting. Now I can't shut it off. [Image: confused.gif]

php in js is a bad idea Big Grin

make in the <head></head> of you document a

Code:
<script>
var base_url = <?=base_url();?>
</script>

in you js fie

Code:
window.open(base_url+"c_tunesheet/index/"+tuneID,'_blank');

OK I got it running now. I couldn't get the <script> inside the doc <head> to work for some reason so I created a hidden div at the top of the main page . .

    <div id="hiddenDiv"><?=base_url()?></div>

Then I used this in the js event function callback . . .

    var base_url=$('#hiddenDiv').text();
    window.open(base_url+'c_tunesheet/index/'+tuneID,'_blank');

Do you think this a less elegant (or bad practices) solution?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.