Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Call to script via ajax $.post fails unless followed by alert()

jquery ajax post fails alert

Best Answer sayitblue, 13 July 2015 - 11:42 AM

Got it.  I just had to place the function that followed as the callback function instead.  The asynchronous request was hung momentarily, and the alert was giving it just enough time to complete without a response from the server, before the function call that followed.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 sayitblue

sayitblue

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 11 July 2015 - 11:13 PM

Hi.

 

This has happened before and I can never figure it out.  Sometimes when I call $.post (or $.get), the call fails to execute unless immediately followed by an alert(). I'm using Firefox 39.0, on Windows 8.1, Xampp's Apache 2.4.10, and PHP 5.6.3.  I appreciate any help I can get.

 

Here's the function call to writexml():

$("#suggestpromise").click(function()
                        {
                        $.get("getsessionvar.php", {getwhat:"activememberid"}, function(data)
                            {
                            if(parseInt(data)>0)
                                {
                                $("#containsuggestpromise").html("Book, Chapter &amp; Verse(s): <input type='text' id='location'><textarea id='suggestpromisetextarea'></textarea><br /><div style='text-align:center'><span id='submitsuggestpromise'>Submit</span></div>");
                                $("#location").focus();
                                $("#submitsuggestpromise").click(function()
                                    {
                                    writexml("suggestpromise");
                                    });
                                }
                            });
                        });

Here's the function definition.  It's supposed to write to an xml file then reload.  It works when the alert() is uncommented:

writexml = function(directive)
    {    
    if(directive === "getpromises")
        {
        $.post("writemembercontent.php", {directive:"getpromises", request:$("#getpromisestextarea").val()});
        }
    if(directive === "suggestpromise")
        {
        text = $("#suggestpromisetextarea").val();
        $.post("writemembercontent.php", {directive:"suggestpromise", filenum:$(".visiblerequest").attr("class").split(" ")[0], location:$("#location").val(), text:text});
        //alert($("#suggestpromisetextarea").val());
        restorestateonreload();
        }
    };

Here's the script that's called, "writemembercontent.php":

<?php

session_start();

$maxchars = 465;
if($maxchars >= strlen($_POST["text"]))
    {
    if($_POST["directive"] === "getpromises")
        {
        $domdoc = new DOMDocument();
        $domdoc->preserveWhiteSpace = false;
        $domdoc->formatOutput = true;
        
        $folderhandle=opendir("membercontent");
        while($filename=readdir($folderhandle))
        {
        if ($filename != "." && $filename != "..")
            {
            $files[]=$filename;    
            }
        }
        //natsort($files);
        $newfilenum=count($files);
        $newfilenum++;
        
        $content=$domdoc->createElement("content");
        $domdoc->appendChild($content);

        $difficulty=$domdoc->createElement("difficulty");
        $cdata2=$domdoc->createCDATASection($_POST["request"]);
        $difficulty->appendChild($cdata2);
        
        $suggestions=$domdoc->createElement("suggestions");        
        $difficulty->setAttribute("by", $_SESSION["firstname"] . " " . $_SESSION["lastname"]);
        $difficulty->setAttribute("datetime", date("D, M j, Y, g:i:s"));
        $domdoc->getElementsByTagName("content")->item(0)->appendChild($difficulty);
        $domdoc->getElementsByTagName("content")->item(0)->appendChild($suggestions);
        $domdoc->save("membercontent/" . $newfilenum . ".xml");        
        }
    if($_POST["directive"] === "suggestpromise")
        {
        $domdoc = new DOMDocument();
        $domdoc->preserveWhiteSpace = false;
        $domdoc->formatOutput = true;
        
        $domdoc->load("membercontent/" . $_POST["filenum"] . ".xml");
        $suggestion = $domdoc->createElement("suggestion");
        $fullname=$_SESSION["firstname"] . " " . $_SESSION["lastname"];
        $suggestion->setAttribute("by", $fullname);
        $suggestion->setAttribute("datetime", date("D, M j, Y, g:i:s"));
        
        
        $text = $domdoc->createElement("text");
        $cdata1 = $domdoc->createCDATASection($_POST["text"]);
        $text->appendChild($cdata1);
        
        $location = $domdoc->createElement("location");
        $cdata2 = $domdoc->createCDATASection($_POST["location"]);
        $location->appendChild($cdata2);
        
        $suggestion->appendChild($text);       
        $suggestion->appendChild($location);
        if(!empty($domdoc->getElementsByTagName("suggestions")->item(0)))
            {
            $domdoc->getElementsByTagName("suggestions")->item(0)->appendChild($suggestion);
            }
            else
                {
                $suggestions=$domdoc->createElement("suggestions")->appendChild($suggestion);
                $domdoc->appendChild($suggestions);
                }
        
        $domdoc->save("membercontent/" . $_POST["filenum"] . ".xml");        
        }
    }

Thank you in advance.

 

 



#2 sayitblue

sayitblue

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 13 July 2015 - 11:42 AM   Best Answer

Got it.  I just had to place the function that followed as the callback function instead.  The asynchronous request was hung momentarily, and the alert was giving it just enough time to complete without a response from the server, before the function call that followed.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download