Hi, I'm trying to make a little customization to a SugarCRM-based product and something is not working.
What I'm trying to do in principle is making Purchase Order Numbers editable. That part is working but a side effect is now I have to validate PO Numbers for duplicates. Particularly, I'm trying to call a server-side method directly from JavaScript to keep the FORM from submitting when the PO Number already exists in a different record.
One of the alternatives I'm trying is AJAX. I found a nice example here:
http://www.w3schools.com/PHP/php_ajax_database.asp
It worked in a separate project but when I try to integrate it to SugarCRM, it seems to reject the request (the page I'm calling from JavaScript gets redirected to Home).
This is the code I've got:
The call (it is in the Save button):
Code:
<input title="{APP.LBL_SAVE_BUTTON_TITLE}" accesskey="{APP.LBL_SAVE_BUTTON_KEY}" class="button"
onclick="this.form.action.value='Save'; return TallyEditor.pre_save() && check_form('PurchaseOrderEditView') && check_duplicate(this.form);"
name="button" value=" {APP.LBL_SAVE_BUTTON_LABEL} " type="submit"> The JavaScript code:
Code:
var xmlhttp;
function check_duplicate(form)
{
return exists(form.po_number_c.value, form.record.value) != 1;
}
function exists(po, id)
{
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null)
{
window.alert("Browser does not support HTTP Request");
return 1;
}
var url = "modules/PurchaseOrders/dupid.php";
url = url+"?po=" + po;
url = url+"&id=" + id;
url = url+"&sid="+Math.random();
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
if (xmlhttp.responseText == 1)
window.alert("ID already exists");
return xmlhttp.responseText;
}
function GetXmlHttpObject()
{
// IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
return new XMLHttpRequest();
// IE6, IE5
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
return null;
} The PHP code being called (dupid.php):
PHP Code:
<?php
$po = $_GET["po"];
$id = $_GET["id"];
$query = "SELECT po_number FROM purchase_orders" .
" WHERE po_number = '" . $po . "'" .
" AND id != '" . $id . "'" .
" LIMIT 0,1";
$result = $this->db->query($query, true, "Error retrieving PO Numbers.");
if($result && ($row = $this->db->fetchByAssoc($result)) !== false)
echo 1;
else
echo 0;
?>
The page dupid.php is supposed to respond with a "1" or a "0" in plain text, but instead, it is redirected to the Home page.
Is there something I should know?
BTW: I've also considered using Web Services but I don't know how.
Please help. I'm new to PHP but I've used ASP.NET for several years.
Bookmarks