Törölve Tól től Ismeretlen 22 febr. 2021 13:51 Hello, I added a new page in the Admin part. In this page, I created a table with a form. (a table to manage products by manufacturer) in the script part, I take variable and I send it via AJAX to a php file in the same folder. (the php file execute a sql command ) But nothing happend. As Ajax is not easy for me . I tried several script in my localhost. I took an exemple here from W3school: (it's juste an example to see the ajax command and the call to an php file) https://www.w3schools.com/js/js_ajax_database.asp In my localhost it work . But When I copy/paste the code to my page in litecart, nothing happend and no error. The AJAX command seems not to work under litecart. no call to my php file. My javascript code is working because I can select a row and delete it on the display (but not via AJAX in my database) [b]Example of script[/b] (>>work on LOCALHOST: the call to php file is done) var xhttp; if (str == "") { document.getElementById("prodid").innerHTML = ""; return; } xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("prodid").innerHTML = this.responseText; } }; xhttp.open("GET", "getproducts.php?q="+str, true); xhttp.send(); } Other example of my code used: $("#deleterowid").click(function{ $.post( 'productdelete.inc.php', { rowid : $("#rowid").val(), prodname : $("#prodname").val() }, function(data){ if(data == 'Success'){ $("#resultat").html("<p>Vous avez effacé le produit !</p>"); } else{ $("#resultat").html("<p>Erreur lors de la connexion...</p>"); } }, 'text' ); }); });``` Question: There is a security in litecart who block my AJAX call to a php file ? Must I add something in my page with the AJAX function ? and/or my php file also ? (Or I must put my php file in a specific folder ?) Thank you for your guidance. Br, jf
tim Founder Tól től Sweden Tagság máj. 2013 óta tim 23 febr. 2021 02:32 You can't access a .inc.php file directly. Is this the backend or frontend? If frontend rename it to .php only (and preferably place it in the root or elsewhere) or change your link to '<?php echo document::ilink('productdelete'); ?>' to access /pages/productdelete.inc.php. $.post('<?php echo document::ilink('productdelete'); ?>', ...); If it's the backend you need to protect it with user::require_login() and define the file in config.inc.php. https://wiki.litecart.net/how_to_create_a_page
Törölve Tól től Ismeretlen 23 febr. 2021 17:32 Hello, Thank you for your response. With you comments, I understand that my problem is well the "URL call" I tried yours propositions, but It seems not to work with $.post or $.ajax Here is some possibilities for mypage in backend to make an AJAX call to my phpfile (backend or frontend): 1/ via ilink in the backend folder (/admin/myfolder) $.post('<?php echo document::ilink('productdelete'); ?>',... 2/ via ilink to frontend (/pages or root) $.post('<?php echo document::ilink('productdelete'); ?>',... $.post('<?php echo document::ilink('productdelete.php'); ?>',... 3/ via link to frontend (/pages or root) $.post('<?php echo document::link('productdelete.php'); ?>',... 4/ via Href_link to frontend (/pages or root) $.post('<?php echo document::href_link('productdelete.php'); ?>',... For my phpfile: I added this before my SQL query : document :: $ snippets [ 'title' ] [ ] = ... document :: $ snippets [ 'description' ] =... result: The call to my phpfile is not done. :-( IF I call directly my phpfile in the webbrowser: //mywebpage/myphpfile My SQL query is well executed in my database. Strange. To give a summary: 1/ I have a page in the backend with a table (mysql) 2/ I must update the table with AJAX 3/ the ajax code must call a php file with my sql command to add/remove data in the database. 4/ The table is updated I can put my php file in frontend or backend. it's ok for me. (the easier way) I tried to put " user::require_login() " in my phpfile. (I suppose it's to keep the SESSION) Question: 1/ Do you see a way for me to investigate ? or what wrong ? (I'm starting to have no idea) 2/ or maybe added my ajax script in the JS file (app.js) good idea or not ? (I attached my table) Thank you for you comments. Br, jf
tim Founder Tól től Sweden Tagság máj. 2013 óta tim 23 febr. 2021 19:31 You didn't answer my question. For the backend you need to define the file in the app's config.inc.php. A link can be formed like this: <?php echo document::link(WS_DIR_ADMIN, ['app' => 'appname', 'doc' => 'thedefinedappdocumentname']); ?> This will change in a future version.
tim Founder Tól től Sweden Tagság máj. 2013 óta tim 23 febr. 2021 19:36 You didn't answer my question. For the backend you need to define the file in the app's config.inc.php. A backend link can be formed like this: <?php echo document::link(WS_DIR_ADMIN, ['app' => 'appname', 'doc' => 'thedefinedappdocumentname']); ?> Admin links will change in a future version. [quote]1/ via ilink in the backend folder (/admin/myfolder) $.post('<?php echo document::ilink('productdelete'); ?>',...[/quote] That will access ~/pages/productdelete.inc.php [quote]2/ via ilink to frontend (/pages or root) $.post('<?php echo document::ilink('productdelete'); ?>',... $.post('<?php echo document::ilink('productdelete.php'); ?>',...[/quote] That will access ~/pages/productdelete.inc.php or ~/pages/productdelete.php.inc.php For the root simply do: document::link(WS_DIR_APP . 'myfile.php') [quote]3/ via link to frontend (/pages or root) $.post('<?php echo document::link('productdelete.php'); ?>',...[/quote] That will access productdelete.php in the same folder as the url. If called in the frontend using a language path prefix it will look for a folder and filer e.g. ~/en/productdelete.php. [quote]4/ via Href_link to frontend (/pages or root) $.post('<?php echo document::href_link('productdelete.php'); ?>',...[/quote] That will access ~/pages/productdelete.php.inc.php and pass it through htmlspecialchars(). It's clear that you are speeding without having an understanding what ilink() does. See the [url=https://wiki.litecart.net/introduction]wiki article[/url] what ilink() and link() does. ilink() is for front page controllers (By default those in ~/pages/). Admin doesn't have a url system today. It's simply the doc= URL parameter. But the file needs to be defined in the config.
Törölve Tól től Ismeretlen 23 febr. 2021 20:57 Thank you for you response. Yes, I focused too much on ajax code and not enought on your code ilink /link. For the config, sorry, I forgot to give you my modification. (I checked how you configure config.inc in your code) I did a modification, but. it's seems not good. here is the config.inc from the backend /Admin/myfolder/ ) The pageP0002prod has been added in the Menu backend >>> Already done //========================== [b]Extract of the config.inc:[/b] array( 'title' => 'La ferme du W Prod', >>>> My page displayed in the Menu >> Already done 'doc' => 'P0002prod', 'params' => array(), ), ), 'docs' => array( 'P0002prod' => 'P0002prod.inc.php', 'productdelete' => 'productdelete.inc.php', >>>> Row added (you spead about this ? ) //============================ AND here is the ajax code in the P0002prod.inc.php //=================================== $.post('<?php echo document::ilink('productdelete'); ?>', >>> productdelete is linked with the productdelete(from config.inc) ? //=================================== But The AJAX call to my 'phpfile' doesn't work. "The row ajax is like transparent" Is it the good way to do ? (or must I via $.ajax GET ?) (I am rather a modest developper. I volunteer for an association. My last development was in mysql and mysqli ;-) ) I will investigate with your comments. I'm missing something, I'm still going to look. Thank you already for your comments.
Törölve Tól től Ismeretlen 23 febr. 2021 22:08 After several days of research ;-) And with the guidance of Tim, I found the good AJAX link Big Thank to Tim !! I share my code if it can help (> It's my first script in js/ajax) Here is the scrip to take action in a table mysql (take the id of the row selected and send it to a phpfile in the same folder) [b]1/ added in the config.inc.php[/b] 'docs' => array( 'productdelete' => 'productdelete.inc.php', [b]2/ script in the page in backend:[/b] $ (document) .ready (fonction ($) { $ (document) .on ('clic', ". deleterowid", fonction (e) { e.preventDefault (); // id of product var rowid = $ (this) .parent (). parent (). attr ('id'); if (rowid! = "") { $ .post ('<? php echo document::link (WS_DIR_ADMIN. "? app = commandproducteurs & doc = productdelete");?>', {rowid: rowid}, function (data) { var obj = jQuery.parseJSON (données); console.log(obj); }); [b]3/ The productdelete.inc.php is executed[/b] and my database is modified "the datatype can be adapted to html or txt..." Thank you Tim !!
tim Founder Tól től Sweden Tagság máj. 2013 óta tim 23 febr. 2021 23:46 This would be the right code: $.post('<?php echo document::link(WS_DIR_ADMIN, ['app' => 'commandproducteurs', 'doc' => 'productdelete']); ?>', ...);