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]"; } ?\>