Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

PHP AJAX Issue

I have an AJAX call that I believe is put together correctly.  However, when I run it, there are no returns.  Even though the query should return entry ID's.  Can anyone just have a quick look and let me know their thoughts where I am going wrong?

<html>
		<form action="#" method="get" id="laser_finder">
  <!--  Operating Mode  -->
  	<div class="form-control main title">
  		Operating Mode
  	</div>
 <!--  START CW  -->
  	<div class="form-control">
  			<input type="checkbox" name="cw" class="laser_operating_mode"  id="cw" value="CW (Continuous Wave)" >
  			<label for="cw">
  				CW (Continuous Wave)
  		</label>
  	</div>
<!--  START Pulsed  -->
  	<div class="form-control">
  				<input type="checkbox" class="laser_operating_mode" name="pulsed" id="pulsed" value="Pulsed" >
  				<label for="pulsed">
  				Pulsed
  		</label>
  	</div>

Open in new window

The jQuery
<script>
$(document).ready(function(){
$( ".laser_operating_mode" ).change(function (e) {
e.preventDefault();
function laser_finder() {
	$.ajax({
		type: 'GET',
				url: '{site_url}/ajax/laser_finder.php',
				data: {action:"laser_finder"},
				dataType: 'json',
				success:function(data) {$('#results').html(data);}
			});
		}
  			laser_finder();
		});
	});
</script>

Open in new window


The PHP
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$action=$_GET["action"];
	if($action=="laser_finder") {
		
		$cw = $_GET['cw'];
		$pulsed = $_GET['pulsed'];
		
		$laser_query = ee()->db->select('entry_id')
		->from('exp_matrix_data');
		->where('col_id_6401', $cw)
		->where('col_id_6401', $pulsed)
		->get();
		
		
		foreach($laser_query->result_array() as $row){
   			echo $row['entry_id'];
   		}
	}  
?>

Open in new window

Avatar of themrrobert
themrrobert
Flag of United States of America image

Well first of all, there is no DOM object with id="results" in your HTML, that could be from your cropping.

Also, your PHP looks ... not so great.  At the very least, you need to remove the semicolon from the line:
->from('exp_matrix_data');

Open in new window

There may be other issues, hard to tell from that snippet.

Take care of those things, then let us know how it goes.

If you need more help, we will need to see more of the PHP, like what you're including, etc, as well as the relevant HTML also, thanks!
Avatar of Robert Granlund

ASKER

I did that and it cleared up most of the issue.  Now I'm getting an error; undefined index cw undefined index pulsed

Also, I guess I need to add formData to the data type.  How do I do that?

This is pretty much all of the code as it stands right now.
HTML
<div class="select-input">
		<form action="#" method="get" id="laser_finder">
  <!--  Operating Mode  -->
  	<div class="form-control main title">
  		Operating Mode
  	</div>
 <!--  START CW  -->
  	<div class="form-control">
  			<input type="checkbox" name="cw" class="laser_operating_mode"  id="cw" value="CW (Continuous Wave)" >
  			<label for="cw">
  				CW (Continuous Wave)
  		</label>
  	</div>
<!--  START Pulsed  -->
  	<div class="form-control">
  				<input type="checkbox" class="laser_operating_mode" name="pulsed" id="pulsed" value="Pulsed" >
  				<label for="pulsed">
  				Pulsed
  		</label>
  	</div>
</form>
</div>
<div id="results"></div>

Open in new window

jQuery at the top of the HTML
<script>
$(document).ready(function(){
$( "#laser_finder" ).change(function (event) {
event.preventDefault();
function laser_finder() {
	$.ajax({
		type: 'GET',
				url: '{site_url}/ajax/laser_finder.php',
				data: {action:"laser_finder"},
				dataType: 'html',
				success:function(result) {
					$('#results').html(result)
        		}
			});
		}
  			laser_finder();
		});
	});
</script>

Open in new window


ajax/laser_finder.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$cw ='';
$pulsed = '';
$action=$_GET["action"];
	if($action=="laser_finder") {
		$cw = $_GET['cw'];
		$pulsed = $_GET['pulsed'];
		
		$laser_query = ee()->db->select('entry_id')
		->from('exp_matrix_data')
		->where('col_id_6401', $cw)
		->get();
		
		$laser_finder = $laser_query->result_array();
	
		foreach($laser_query->result_array() as $row){
   			echo $row['entry_id'].'<br />;
   		}
   		 
	}  
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are missing a closing ' after your <br/> in your php file
As Marco pointed out you are not passing your cw and pulsed values to the AJAX function.

Some pointers - POST your data to the script - include the action in the URL
In the code below I have taken the laser_finder function out of the event handler but you can choose.

Updated JavaScript - posts cw and pulsed data to laser_finder
<script>
$(function(){
  $( "#laser_finder" ).change(function (event) {
    event.preventDefault();

    var data = $(this).serialize();
    
    laser_finder(data);
  });
});
// PASS DATA TO FUNCTION
// AND POST IT
function laser_finder(data) {
  $.ajax({
    type: 'POST',
    // ADD ACTION AS GET PARAMETER
    url: 'laser_finder.php?action=laser_finder',
    data: data,
    dataType: 'html',
    success:function(result) {
      $('#results').html(result)
          }
  });
}
</script>

Open in new window

PHP - modified to not interface to CI - you will need to put that back in
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$cw ='';
$pulsed = '';
$action=isset($_GET["action"]) ? $_GET["action"] : '';
if($action=="laser_finder") {
  $cw = isset($_POST['cw']) ? $_POST['cw'] : '';
  $pulsed = isset($_POST['pulsed']) ? $_POST['pulsed'] : '';
  // REPLACE WITH CI CODE
  $laser_query = new stdClass;
  $laser_query = array (
    array('entry_id' => 1),
    array('entry_id' => 3),
    array('entry_id' => 4)
  );

  foreach($laser_query as $row){
    // MISSING ' INSERTED
    echo $row['entry_id'].'<br />';
  }
  // END REPLACE
}

Open in new window


Working sample here