How to add an ajax script on cashdesk module

Hi all,

I’m selling Books, so I need to modify the cashdesk module to add a few things : two more fields author and editor, and an instant research with an ajax script.

All my books have a barcode. So when I flash It, the reference of the book appear in the ref field. I’ve found an ajax script which allow an instant research in the database with the reference, and fill author and editor fields with correct entries.

This script works on a simple form and my product database. My problem is to adapt it with dolibarr… I’m not a very specialist of Php/ajax. I’ve searched on the wiki, but I don’t know understand how to make custom quiery.

Could you help me ?

The Form (I’m working on facturation1.tpl):

<script type="text/javascript" src="ajax.js"></script>
          
<form id="frmFacturation" class="formulaire1" method="post" action="facturation_verif.php" autocomplete="off">
<input type="hidden" name="token" value="<?php echo $_SESSION['newtoken']; ?>" />
<input type="hidden" name="hdnSource" value="NULL" />
<table>
<tr><th class="label1">EAN</th></tr>
<tr><td><input type="text" size="12" name="ref" id="ref" onchange="ajaxref()"/></td></tr>
<tr><th class="label1">Label</th></tr>
<tr><td><input id ="label" size="20" name="label"/></td></tr>
<tr><th class="label1">Author</th></tr>
<tr><td><input id ="label_auteur" size="20" name="label_auteur"/></td></tr>
<tr><th class="label1">Editor</th></tr>
<tr><td><input id ="label_editeur" size="20" name="label_editeur"/></td></tr> 
</table>
</form>

The ajax script

var xhr = null;
    function getXhr()
    {
    if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
    else if(window.ActiveXObject)
    {
    try
    {
    xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    else
    {
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour");
    xhr = false;
    }
    }
    function ajaxref()
    {
    getXhr();
    xhr.onreadystatechange = function()
    {
    if(xhr.readyState == 4 && xhr.status == 200)
    {
    var chaine = xhr.responseText;
    var tableau = chaine.split("*"); 
    document.getElementById('label').value = tableau[0];
    document.getElementById('label_auteur').value = tableau[1];
    document.getElementById('label_editeur').value = tableau[2];
    }
    }
    xhr.open("POST",'ajaxrecupaddr.php',true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ref = document.getElementById('ref').value;
    xhr.send("ref="+ref);
    }

And the sql quiery

<?php $req=$db-\>query("SELECT label,label_auteur,label_editeur FROM llx_product WHERE ref = '$_POST[ref]'"); while($res = mysql_fetch_array($req)) { echo "$res[label]*$res[label_auteur]*$res[label_editeur]"; } ?\>

hello,
it depends what version of dolibarr you use;
in the latest 3.3 it seems that you just have to add the following code in the __construct() function of /core/modules/modCashDesk.class.php (note it will be overwritten on next dolibarr version update) :

[code]
$this->module_parts[‘js’] = ‘path_to_your_js’;[/code

Hello

Thanks for the response.

I’m on 3.3.

I don’t understand where I must paste my quiery. In the original ajax script, the quiery is in a “ajaxrecupaddr.php” . So I’m lost :happy: I’m sure that the quiery don’t work.

put the query stuff in a new file called ajaxrecupaddr.php; to make it work put this file and the javascript in the cashdesk directory; if you want you can put them in javascript and a new ajax directory (both under cashdesk) but in this case, you will have to adjust the pathes in your code.

Ok, now I the script works, great.

But in my first field I have this error message :

Fatal error “call to a member function query() on a non-object in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\dolibarr-3.3.0\htdocs\cashdesk\ajaxrecupaddr.php on line 3”

<?php $resql=$db-\>query("SELECT label,label_auteur,label_editeur FROM llx_product WHERE ref = '$_POST[ref]'"); while($res = mysql_fetch_array($resql)) { echo "$res[label]*$res[label_auteur]*$res[label_editeur]"; } ?\>

just add the following code after before $resql=…
it will include the dolibarr framework.

[code]
require_once ‘…/main.inc.php’;[/code

Thank you very much ! Now I can complete the form.

I have just another question. What’s the best way to fill price and stock field ? Could I use the ajax script ? I don’t know very well ajax, so I don’t know if it’s a good choice…

With this ajax script I need to change how the price / stock is calculated.