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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Location
    Chicago, IL
    Posts
    169
    Thanks
    26
    Thanked 3 Times in 3 Posts

    Running code passed back by the AJAX php page

    Currently, I have three pages:
    viewmyfsj.php (the main page that users will use)
    insert.js (the JS page)
    action.php (the PHP page that processes the JS page)

    Currently, the AJAX portion of everything is working (didn't take too long to learn AJAX, surprisingly). However, I am kind of at a loss for how I pass back some values from the action.php to viewmyfsj.php. Here's my code, and I'll explain exactly what I'm having issues with.

    (viewmyfsj.php)
    Code:
    <form id="form1" name="form1" method="POST" action="javascript:insert()">
    <table width="100%" border="0">
    <tr>
    	<td width="5%"></td>
    	<td width="12%" align="center"><?php echo date("n-j-Y"); ?></td>
    	<td width="20%" align="center"><input name="restaddrestaurant" type="text" id="restaddrestaurant" class="tb-background" value="<?php echo $input1; ?>" onkeyup="searchSuggest();" autocomplete="off"/>
    		<div id="search_suggest"></div>
    	</td>
    	<td width="23%" align="center">
    		<input type="text" name="restaddfood" id="restaddfood" class="tb-background" value="<?php echo $input2; ?>" />
    	</td>
    	<td width="15%" align="center">
    	<td width="20%" align="center"><input type="text" name="restaddnotes" id="restaddnotes" class="tb-background" value="<?php echo $input4; ?>" />
    	</td>
    	<td width="5%" align="center"><input type="submit" name="restaddentry" id="restaddentry" value="Add" /></td>
    </tr>
    </table>
    <?php echo $entryfailure; ?>
    </form>
    (insert.js)
    Code:
    var nocache = 0;
    
    function getXmlHttpRequestObject() 
    {
    	// all my browser-checking
    }
    
    var http = getXmlHttpRequestObject();
    
    function insert() 
    {
    	document.getElementById('insert_response').innerHTML = "Adding entry..."
    	nocache = Math.random();
    	var restaddrestaurant = encodeURI(document.getElementById('restaddrestaurant').value);
    	var restaddfood = encodeURI(document.getElementById('restaddfood').value);
    	var restaddsensitive = encodeURI(document.getElementById('restaddsensitive').value);
    	var restaddnotes = encodeURI(document.getElementById('restaddnotes').value);
    	
    	http.open('get', 'action.php?a=addrestfood&restaddrestaurant='+restaddrestaurant+'&restaddfood='+restaddfood+'&restaddsensitive='+restaddsensitive+'&restaddnotes='+restaddnotes+'&nocache='+nocache,true);
    	http.onreadystatechange = insertReply;
    	http.send(null);
    }
    
    function insertReply() 
    {
    	if(http.readyState == 4)
    	{ 
    		document.getElementById("insertdata").innerHTM = http.responseText;
    		// else if login is ok show a message: "Site added+ site URL".
    		document.getElementById('insert_response').innerHTML = 'Entry added.';
    	}
    }
    And finally action.php
    Code:
    <?php
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    header("Content-Type: text/xml; charset=utf-8");
    
    require('authenticate.php');
    $authenticate_user = authenticate_user();
    if($_GET[a] == "addrestfood") 
    {
    	include('database.php');
    	if($authenticate_user)
    	{
    		$date = mktime(date(G),date(i),date(s),date(n),date(j),date(Y));
    		$sqldate = mysql_real_escape_string($date);
    		// bunch of safeguarding for the mysql DB
    		
    		if($_GET[restaddrestaurant] == "" OR $_GET[restaddfood] == "" OR $_GET[restaddsensitive] == "0")
    		{
    			$ispanelopen = "{contentIsOpen:true}";
    			$entryfailure = "<tr width=\"100%\"><td align=\"center\" colspan=\"5\"><deletex>Please fill in the above forms.  \"Notes\" is optional.</deletex></td></tr>";
    			$input1 = $_GET[restaddrestaurant];
    			$input2 = $_GET[restaddfood];
    			$input4 = $_GET[restaddnotes];
    			if($_GET[restaddsensitive] == "no"){ $input3selected2 = "selected=\"selected\"";}
    			elseif($_GET[restaddsensitive] == "maybe"){ $input3selected3 = "selected=\"selected\"";}
    			elseif($_GET[restaddsensitive] == "yes"){ $input3selected4 = "selected=\"selected\"";}
    			else{ $input3selected1 = "selected=\"selected\"";}
    		}else
    		{
    			$sql = //my sql insert command
    			$result = mysql_query($sql);
    			$ispanelopen = "{contentIsOpen:false}";
    			$entryfailure = "";
    			$input1 = "";
    			$input2 = "";
    			$input3selected1 = "selected=\"selected\"";
    			$input3selected2 = "";
    			$input3selected3 = "";
    			$input3selected4 = "";
    			$input4 = "";
    		}
    	}
    }
    The big issue for me is the inability of $ispanelopen being read by "viewmyfsj.php." I need to pass that, as well as all these other variables I did not include in my code above. Is there a way to pass this back and have "viewmyfsj.php" run the code that is passed back?

    Thank you so much, and I apologize for all the code posted.

  • #2
    Regular Coder
    Join Date
    Jul 2009
    Location
    Chicago, IL
    Posts
    169
    Thanks
    26
    Thanked 3 Times in 3 Posts
    Nevermind this reply.
    Last edited by wldrumstcs; 07-06-2009 at 09:02 PM.


  •  

    Posting Permissions

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