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

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

themrrobertCommented:
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!
0
rgranlundAuthor Commented:
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

0
Marco GasiFreelancerCommented:
You forgot to pass data through ajax (whereas you did it in your previous question) while now youìre passing as data the action:

<script>
$(document).ready(function(){
$( "#laser_finder" ).change(function (event) {
event.preventDefault();
	var cw = $('#cw').val();
	var pulse = $('#pulse').val();
function laser_finder() {
	$.ajax({
		type: 'GET',
				url: '{site_url}/ajax/laser_finder.php',
                               data: {
                                        cw: cw,
                                        pulse: pulse
                                },
				dataType: 'html',
   				success:function(result) {
					$('#results').html(result)
        		}
			});
		}
  			laser_finder();
		});
	});
</script>
                                      

Open in new window


In addition, if you want your php script return html you have to put them in a string:
$output = '';
foreach($laser_query->result_array() as $row){
	$aoutput .= $row['entry_id'].'<br />;
}
echo $output;

Open in new window


Now I have to go but I'll come back to you later :-)
Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.