Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unexpected alert box from AJAX script

    Hello all,

    I am working on an Ajax routine that uses POST to send data to php script files for processing. Previously, the js behaved as expected but of recent an alert box pops up with the word "POST", which is the url parameter used with the AJAX open statement. I verified that by changing the capitalization: POST still worked but the pop-up box displayed "post".



    It's odd that different browsers treat this event differently:

    • IE9 displays the popup box containing POST labeled "Message from webpage" then continues the script.

    • Firefox displays a plain-jane pop-up with POST, stops the script, and returns to the calling program.

    • Chrome does not produce a pop-up at all and the script works as designed.

    I'm stumped! Note that the js line highlighted in red is generating the alert. Here's the code:

    HTML:
    Code:
    <table id="tblDisplayRecMenu" align="center" >
          <tr id="MenuBarRow">
          <th colspan="2" > 
          <ul id="MenuBar1" class="MenuBarHorizontal" >
    	<li><a href="#" onclick="javascript:ajax_post('Billing')">Billing</a></li>				
             <li><a href="#" onclick="javascript:ajax_post('AcctMgmt')">Acct Mgmt</a></li>
              <li><a href="#" onclick="javascript:ajax_post('Fines')">Fines</a></li>
              <li><a href="#" onclick="javascript:ajax_post('TowCo')">Tow Co</a></li>
            </ul>
            </th>
          </tr>
    </table>
    Javascript:
    Code:
    <script type="text/javascript">
    
    function ajax_post(choice) 
    {
    	var recNum = "recNum=" + keyid;		//  Record number
    	var formData = "choice=" + choice;         //  Selection
    	var url = "display" + choice + ".php";       //  File name
    	var ajax = ajaxObj("POST" , url);
    	ajax.onreadystatechange = function() 
    	{
    		if(ajaxReturn(ajax) == true) 
    		{
    			document.write(ajax.responseText);
    		}
    	}
    ajax.send(formData+"&"+recNum);
    }
    </script>
    The js library file:

    Code:
    function ajaxObj( meth, url ) 
    {
    	var x = new XMLHttpRequest();
    	x.open( meth, url, true );
    	x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	return x;
    }
    function ajaxReturn(x)
    {
    	if(x.readyState == 4 && x.status == 200)
    	{
    		return true;	
    	}
    }
    Sample php file receiving the POST data:
    Code:
    <?php
    //<!-- ******** displayBilling.php ******** -->
    
    echo "<h3>Billing</h3><br />";
    echo "1. ";
    var_dump($_POST);
    echo "<br />";
    
    if(isset($_POST["choice"])){
        echo "2. " . $_POST["choice"] . "<br />";
    
    //exit();
    }
    echo "3. ";
    foreach ($_POST as $key=>$value) {echo $key . ": ".$value . "\n";}
    echo "<br /><br />";
    ?>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Get rid of the document.write immediately ... and forget everything about its existence. document.write is only(!) useful in case of the initial(!) page creation. As soon as the initial page creation has been finished, each call to document.write will delete the current document (including your Javascript!) and create a new one.

    You should learn about DOM manipulation methods like .innerHTML, .createElement, .appendChild, .insertBefore etc. instead

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    47
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Problem resolved -- another mystery

    Thanks for that advice. I have been working very diligently to learn as much about DOM as I can digest. I have it interspersed in my code, but the learning curve is extremely steep! It will be a gradual process... there is only so many hours per lifetime!!

    The document.write was only for testing -- It seemed to do what I was trying to accomplish, but it was very preliminary and js came to the rescue -- I found a very cool way to accomplish the same task without additional php files/scripts.

    As it turns out, I did some recoding and the pop-up went away. I wish I had some idea what had caused and then corrected it. I keep earlier versions of my work but the format of the open statement is exactly the same. There must have been something else causing that problem.


  •  

    Posting Permissions

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