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 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can't get response from PHP using AJAX

    Just want to get the echo value from PHP.

    AJAX code:

    Code:
    	
    		 $.ajax({
    			type: 'POST',
    			url: 'member_data.php',
    			datatype: 'text',
    			async: false,	
    			success: function(response){var k = response;}
    					});
    PHP code:

    PHP Code:
    <?php
        $ID 
    '60543';
        echo 
    $ID;
    ?>
    I tried an alert on the k variable but nothing other than undefined. Tried many variations but so far no luck. I must be missing some little thing somewhere.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,998
    Thanks
    36
    Thanked 1,072 Times in 1,068 Posts
    Are you calling jquery?
    You're missing the closing brackets for the function and you're doing nothing with the return:
    Code:
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $.ajax({
    	type: 'POST',
    	url: 'ajax.php',
    	datatype: 'text',
    	async: false,	
    	success: function(response){
    		var k = response;
    		alert(k);  //OR WHAT EVER YOU WANT TO DO WITH THE RESPONSE
    	}
    });
    </script>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That is exactly what I am doing. The brackets was just copy and paste.

  4. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,998
    Thanks
    36
    Thanked 1,072 Times in 1,068 Posts
    Quote Originally Posted by cxxxv View Post
    That is exactly what I am doing. The brackets was just copy and paste.
    If so then everything should be working.
    success: function(response){var k = response;}
    does not do anything with the response. What are you doing with k?
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  5. #5
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    K has the results of a query from PHP in the form of an array. What I am struggling with now is that what comes out of PHP and is read in Jquery don't match. The array is just a set of key/value pairs. The odd thing is that in PHP all the pairs are there. However, when reading it in jQ all the keys are there but in some cases the values are missing.

  6. #6
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,998
    Thanks
    36
    Thanked 1,072 Times in 1,068 Posts
    Then you probably have a problem with whats/or not stored in your DB. For starter. test that and if it's positive check what and how your storing the info. If the DB is good check the PHP for how the response is assembled.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  7. #7
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've done all the checks from the DB (the DB has been in use for a long time, so no issues there). In doing some more reading I came across a post about UTF8 and JSON. Made me wonder if the issue is with UTF8. So later I will try this:

    Code:
    <?php
    $encodedArray = array_map(utf8_encode, $rawArray);
    ?>

  8. #8
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I figured out the issue but do not know how to solve it. In my page I use json to load a tree for navigation in the
    Code:
     $(document).ready
    and this works great. I also have in my page a function to fetch data using an index value from the tree. The function also uses json. Seems that somehow the first json interferes with the second. I was able to isolate it be creating a small page with only the function. When doing that then all the data was correct.

    So the issue is how to clear the first json or destroy it completely. Coming from a Windows desktop developer background we use this often to clear memory and also to not have memory leaks. Assuming my theory is correct I have not found a way for similar functionality on the WEB world.

  9. #9
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,998
    Thanks
    36
    Thanked 1,072 Times in 1,068 Posts
    Hard to help when you show nothing and keep us in the dark.

    If you put your javascript where it is supposed to be : After the HTML code, but before the </body> tag you don't need the " $(document).ready" function.

    If you have two separate objects to house the converted json they shouldn't interfere with each other.
    And now I again must infer because I lack knowing your code, that both json are converted in JS and for that you should be using the json.parse() method. Are you?
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  10. #10
    New Coder
    Join Date
    Jun 2017
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have been following examples. Every thing I read talks about the document ready. Here is the whole file. Perhaps you could take a look and offer some advise. I would like to learn how to do this correctly. BTW, not worried about SQL injection. It's an Intranet app
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
        <link rel="stylesheet" href="styles/jqx.base.css" type="text/css" />
    	<link rel="stylesheet" href="styles/jqx.energyblue.css" type="text/css" />
    	<link rel="stylesheet" href="styles/w3.css"  />
        <script type="text/javascript" src="scripts/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="js/jqxcore.js"></script>
        <script type="text/javascript" src="js/jqxdata.js"></script>
        <script type="text/javascript" src="js/jqxbuttons.js"></script>
        <script type="text/javascript" src="js/jqxscrollbar.js"></script>
        <script type="text/javascript" src="js/jqxdatatable.js"></script>
        <script type="text/javascript" src="js/jqxtreegrid.js"></script>
    	<script type="text/javascript" src="js/jqxpanel.js"></script>
        <script type="text/javascript" src="js/scripts/demos.js"></script>
    		
        <script type="text/javascript">
    	
            function GetMemData(MEMBER_ID){	
    
    					jQuery.ajax({
    					  type: 'POST',
    					  url: 'member_data.php',
    					  datatype: 'json',
    					  data:{MID:MEMBER_ID},
    					  async: false,
    					  success: function(data){
    						k = data;
    					  }
    					});								
    
    					meminfo = JSON.parse(k);
    					<!-- console.log(meminfo);	 -->
    					
    					$.each(meminfo, function(k, v) {
    						$("#" + k).val(v);
    					});
    
    					
            }
    		
        </script>	
    	
        <script type="text/javascript">
            $(document).ready(function () {    
    			
                var source =
                {
                    dataType: "json",
                    dataFields: [
                            { name: 'id' },
                            { name: 'parentid' },
                            { name: 'text' },
                            { name: 'value' }
                    ],
                    hierarchy:
                    {
                        keyDataField: { name: 'id' },
                        parentDataField: { name: 'parentid' }
                    },
                    id: 'id',
    					url: 'housemem_data.php',
    					async: false		
                };
    			
                var dataAdapter = new $.jqx.dataAdapter(source);
    			
                $("#treeGrid").jqxTreeGrid(
                {
                    width: 250,
    				height: 750,
                    source: dataAdapter,
                    sortable: true,
    				editable: true,
    				altRows:true,
    				<!-- theme:'energyblue', -->
    	            editSettings: { saveOnPageChange: true, saveOnBlur: true, saveOnSelectionChange: true,
    								cancelOnEsc: true, saveOnEnter: true, editSingleCell: true, editOnDoubleClick: true, editOnF2: true },
                    ready: function()
                    {
                        $("#treeGrid").jqxTreeGrid('expandRow', '1');				
                        $("#treeGrid").jqxTreeGrid('expandRow', '2');
                        $("#treeGrid").jqxTreeGrid('expandRow', '3');					
                    },
                    columns: [
                      { text: 'NAME',  dataField: 'text', width: 235 },
                    ]
    
                });
    
                $("#treeGrid").on('rowDoubleClick', function (event) {
                    var args = event.args;
    				GetMemData(args.key);		
    							
                 });
    		});
    		
    		dataAdapter = undefined;
    		source = undefined
        </script>
    </head>
    <body class='default'>
        <div id="treeGrid" style="display:inline-block; vertical-align:top; margin-top:10px; margin-left:10px"></div>	
    	<div style="display:inline-block; vertical-align:top; margin-top:10px; margin-left:10px;">
    	 
    		<form>
    			 <fieldset style="display:inline-block; vertical-align:top; height: 260px; margin-left:30px; width:900px; background-color:#F0F0F0; ">
    				  <legend><b>Member Information</b></legend>
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >First Name:</label>
    					  <input type="text" id="FN" />
    					  
    					  MI: <input type="text" id="MI"  style="width:30px;"/>
    					  
    					  Last Name: <input type="text" id="LN"/><br>  
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >DOB:</label>
    					  <input type="text" id="DOB"><br>  
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Address 1:</label>
    					  <input type="text" id="ADD1" style="width:320px;">
    					  
    					  Address 2: <input type="text" id="ADD2" style="width:320px;"<br>
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >City:</label>
    					  <input type="text" id="CTY" style="width:170px;">
    					  
    					  State: <input type="text" id="ST" style="width:50px;">
    					  
    					  ZIP CODE: <input type="text" id="ZIP" style="width:65px;">	
    					  
    					  COUNTY: <input type="text" id="CNTY" style="width:150px;"><br>	
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Phone (Home):</label>
    					  <input type="text" id="PH1" style="width:150px;">	
    
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Phone (Mobile):</label>					  
    					  <input type="text" id="PH2" style="width:150px;">
    
    					  EMAIL: <input type="text" id="EM" style="width:200px;"><br>
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >AIMS:</label>
    					  <input type="text" id="AIMS" style="width:150px;">						  					  
    					  
    							  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Gender:</label>
    					  <select <id="GEN">
    							<option value="0">Select</option>				  
    							<option value="male">M</option>
    							<option value="female">F</option>
    					  </select>
    
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Race:</label>
    					  <select <id="RACE">
    							<option value="0">Select</option>				  
    							<option value="AA">African-american</option>
    							<option value="CAU">Caucasian</option>
    					  </select>		
    						<br>
    					 
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Funding:</label>				 
    					  <select <id="FUND">		  
    							<option value="0">Select</option>		  
    							<option value="1">ARRA</option>
    							<option value="2">CARE TRANS</option>
    							<option value="3">CCSP</option>
    							<option value="4">FS</option>		  
    							<option value="5">ITC</option>
    							<option value="6">NSIP</option>		  
    							<option value="7">NSIP SSBG</option>
    							<option value="8">NSIP STATE</option>		  
    							<option value="9">PRIVATE PAY</option>
    							<option value="10">T-3</option>				
    							<option value="11">UA</option>
    							<option value="12">UW</option>						
    					  </select>
    					  
    					  
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Poverty:</label>							  
    					  <input type="checkbox" id="POV" style="padding-left:10px;">		
    
    					  <label style="display: inline-block;  width: 140px;  text-align: right;  padding: 5px;" >Inactive:</label>							  
    					  <input type="checkbox" id="INAC" style="padding-left:10px;">						  
    					  
    			  
    			 </fieldset>
    		</form>
    	
    </div>	
    
    </body>
    </html>


 

Tags for this Thread

Posting Permissions

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