Results 1 to 3 of 3

Thread: XMLHttpRequest call not working

  1. #1
    areyesram is offline Junior Member
    Join Date
    May 2009
    Posts
    2

    Default XMLHttpRequest call not working

    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($querytrue"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.
    Last edited by areyesram; 2009-06-03 at 02:02 AM. Reason: typo

  2. #2
    areyesram is offline Junior Member
    Join Date
    May 2009
    Posts
    2

    Default Re: XMLHttpRequest call not working

    Good news at last!
    I found the answer in a different thread

    Apparently, the PHP code to be called from JavaScript must be in the root directory of the application.

    Also, I had to add this to run queries:
    PHP Code:
    require_once('include/entryPoint.php'); 
    And queries changed from:
    PHP Code:
    $this->db->query(...); 
    to:
    PHP Code:
    $current_user->db->query(...); 

    Hope this helps someone else.

  3. #3
    crmsiva's Avatar
    crmsiva is offline A Sugar Hero
    Join Date
    Jan 2009
    Location
    Chennai, India
    Posts
    1,130

    Default Re: XMLHttpRequest call not working

    Hi

    You can use YAHOO.util.Connect.asyncRequest() for sending http requests. This will simplify the AJAX part of your code also.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Schedule Call for call Entry Only - Simpler Screen?
    By zeezhao in forum Developer Help
    Replies: 2
    Last Post: 2009-04-28, 02:14 PM
  2. AGAIN: - Add invites not working on Schedule a call
    By fachtopia in forum General Discussion
    Replies: 6
    Last Post: 2006-07-31, 11:20 AM
  3. Call Logging/follow-up call
    By kickn in forum Classifieds
    Replies: 0
    Last Post: 2006-04-20, 06:22 PM
  4. Replies: 5
    Last Post: 2006-04-10, 03:20 PM
  5. Call List "Only My Items" checkbox not working
    By swhitlow in forum General Discussion
    Replies: 2
    Last Post: 2005-09-26, 09:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •