Welcome Guest, Not a member yet? Register   Sign In
Onclick does nothing
#1

Just started using CI, and need to record downloads, but add_row() not getting called  Confused

<a href="http://downloadsite.com/file.doc" onclick="$this->table_model->add_row( 'file.doc' );">

Thanks, if can tell me the obvious thing I must be missing.
Reply
#2

Er, Javascript is executed client-side. Your PHP code is meant to run server-side.
You will need to rework your page so that the Javascript sends a request to your server-side PHP which would then udpate the model.
Reply
#3

(This post was last modified: 05-13-2019, 10:00 AM by albertleao.)

You're thinking about it wrong.

1. onclick is a js handler. php runs on the server.

2. It's good practice to never call a model from your html. In fact, you shouldn't be calling much at all from your html.

3. Try something like:


Code:
<a href="#" onclick="myJsFunctionToUploadDoc()">
   Upload
</a>

<script>
   function myJsFunctionToUploadDoc() {
         /// ajax logic to upload file
   }
</script>
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#4

Useful thanks, understand more now about server(php)/client(js). I tried below, which adds a row when the page loads, but not with onclick. Dunno how to use js to send a request to php, which should be simple even for novice.

<script>
function add_row() {
<?php $this->table_model->add_row( 'file.doc' ); ?>
}
</script>

<a href="http://downloadfiles.com/file.doc" onclick="add_row()">
Reply
#5

(05-13-2019, 10:58 AM)Deborah Wrote: Useful thanks, understand more now about server(php)/client(js).  I tried below, which adds a row when the page loads, but not with onclick.  Dunno how to use js to send a request to php, which should be simple even for novice.

<script>
function add_row() {
<?php $this->table_model->add_row( 'file.doc' ); ?>
}
</script>

<a href="http://downloadfiles.com/file.doc" onclick="add_row()">

To learn how to use JS to send a request to php google "jquery get and post". If you don't want to use JS you could create a form that just has a submit button with the action attribute specifying a codeigniter controller. Then in the controller you call your model function to add the row to your table.
Simpler is always better
Reply
#6

(This post was last modified: 05-13-2019, 03:34 PM by albertleao.)

(05-13-2019, 10:58 AM)Deborah Wrote: Useful thanks, understand more now about server(php)/client(js).  I tried below, which adds a row when the page loads, but not with onclick.  Dunno how to use js to send a request to php, which should be simple even for novice.

<script>
function add_row() {
<?php $this->table_model->add_row( 'file.doc' ); ?>
}
</script>

<a href="http://downloadfiles.com/file.doc" onclick="add_row()">

This is still wrong.

Anything in a <?php ?> tag will only run on your server. Anything inside your <script></script> tag will run on your client. Before diving into this anymore, I'd recommend reading up and learning about server side languages and client side languages.

The reason it's adding the row when the page loads is because the server is executing that code when rendering the page and not the client. You're looking for that functionality to occur when someone clicks the <a></a> tag, which is client side logic. You are getting them both confused.

Your javascript function needs to make an http xhr call to your backend service. This is called ajax. If you are calling <?php $this... ?> in your html, you are doing it wrong.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#7

There's one more thing you need to know about AJAX related to CodeIgniter.
In your Javascript (hopefully jQuery), one of the arguments is the "url".
You should set it to site_url(), with your controller/method between the brackets.

Example:
PHP Code:
url "<?= site_url('controllername/methodename');?>"

This seems odd, because now you're doing server sided stuff inside a Javascript!
But all the php code does, is injecting the complete url into your Javascript when the page is rendered, so your Javascript knows what url to call once the js function is executed.
Mind that <?= is short for: <?php echo
Reply
#8

@Wouter60

I would say hopefully not jquery!!! Smile
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#9

Been googling jquery posting, xhr calls, ajax. Tried:

[download_page.php]
class download_page extends MY_Controller {
function add_row() {
alert("downloading");
}
}

[downloads.php]
<a href="http://www.downloaddocs/file.doc" onclick="<?=site_url('download_page/add_row')?>">

.. but the add_row function still not being called. If I put a syntax error in add_row() then the downloads.php page gives an error.

The developer who made the site that I'm trying to add the click records to no longer around, which is the problem as would've taken him 2 mins.
Reply
#10

Where is your javascript? You haven't posted any javascript creating an ajax call. You also have an href to a document and an onclick event, which is not what I imagine you're trying to do. You need to use javascript for your ajax call.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB