CodeIgniter Forums
Onclick does nothing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Onclick does nothing (/thread-73605.html)

Pages: 1 2


Onclick does nothing - Deborah - 05-13-2019

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.


RE: Onclick does nothing - ciadmin - 05-13-2019

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.


RE: Onclick does nothing - albertleao - 05-13-2019

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>



RE: Onclick does nothing - Deborah - 05-13-2019

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()">


RE: Onclick does nothing - donpwinston - 05-13-2019

(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.


RE: Onclick does nothing - albertleao - 05-13-2019

(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.


RE: Onclick does nothing - Wouter60 - 05-13-2019

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


RE: Onclick does nothing - albertleao - 05-14-2019

@Wouter60

I would say hopefully not jquery!!! Smile


RE: Onclick does nothing - Deborah - 05-16-2019

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.


RE: Onclick does nothing - albertleao - 05-16-2019

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.