Link to home
Start Free TrialLog in
Avatar of DS928
DS928Flag for United States of America

asked on

Getting A PHP file to run from Javascript

I have switch statement in a header.  It's kicked off by Javascript, however; the query is in PHP and it wont run from javascript.  I could safe it to a seperate PHP file and call that?  Correct?  But how can I call the file, I assume with AJAX, but how?  Thank you.

<script type="text/javascript">

$(document).ready(function() {
	$("input:image").click(function(evt) {
	evt.preventDefault();
	var n = setSearch( $(this).val() );})
	})
		
	function setSearch(n)
	{
		switch(n){
 			case "Place":
  				//alert("Header is Place");
				<?php
				$sql = mysql_query("SELECT tblLocations.RestID as Rid, tblRestaurants.RestName as name
				FROM tblRestaurants INNER JOIN tblLocations
				ON tblRestaurants.RestID = tblLocations.RestID
				GROUP BY tblLocations.RestID, tblRestaurants.RestName
				ORDER BY tblRestaurants.RestName ASC");
				while($row=mysql_fetch_array($sql))
				{
				echo '<option value="'.$row['Rid'].'">'.$row['name'].'</option>';
				}
				?>
				break;
                                                                           Other cases............more files to call!

Open in new window

Avatar of DS928
DS928
Flag of United States of America image

ASKER

Is this in the right direction?  I want this php file to run to fill a listbox the same as the others that I have....

$(document).ready(function() {
	$("input:image").click(function(evt) {
	evt.preventDefault();
	var n = setSearch( $(this).val() );})
	})
	alert("n = " + n);	
	function setSearch(n)
	{
		switch(n){
 			case "Place":
  				ajax.open('get', 'place_place.php', true);
				break;
 			case "Cuisine":

Open in new window

Almost...

From the code you've posted I assume you are using jQuery.

To load a part of the page the easiest is to use the load method (http://api.jquery.com/load/).

Let's assume that you want to load stuff into a div:

<div id="placeHolder"></div>

In this case you can use in your JavaScript

$('#placeHolder').load('place_place.php');

This will call PHP script, which should generate and return the required content.
This article shows all the moving parts. You can copy the scripts, install and run them, then modify them for your own needs.
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

Best of luck with your project, ~Ray
Avatar of DS928

ASKER

I'm starting to feel like I'm in a bowl of spaghetti!  I have three listboxes.  I am trying to populate the first based on case.  The first listbox populates if I have the PHP code in the body but I can't select different cases from there.  So I want to move it to the header where I can select different cases.  I am using this code to update the other two listboxes.  You can see where I am calling the php files.  And this works.  As long as I have the php code in the body. but like I said.  That is only one instance and I need 4 others.  Heres is the update code.

$(document).ready(function()
{
$(".Doggie").change(function()
{
	var LocationString ='Rid='+ $(this).val();
    $.ajax({
        type: "POST",
        url: "place_city.php",
        data: LocationString,
		cache: false,
        success: function (html) {
            $(".Kitty").html(html);
        }
    });
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(),  // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
$.ajax({
        type: "POST",
        url: "place_area.php",
        data: {"Rid":Rid,"Cid":Cid}, 
        cache: false,
        success: function (html) {
$(".Pig").html(html);
} 
});
});
});

Open in new window


And here's what I did for the first case "Place" in the header...
switch(n){
	case "Place":
	$(".Doggie").load(function()
	$.ajax({
     		type: "POST",
        		url: "place_place.php",
        		cache: false,
        		success: function (html) {
	$(".Doggie").html(html);
	} 
	});
	});
	break;

Open in new window


BTW the three boxes are called Doggie, Kitty and Pig.

And my PHP file place_place.php
<?php
require('config.php');

$sql = mysql_query("SELECT tblLocations.RestID as Rid, tblRestaurants.RestName as name
		FROM tblRestaurants INNER JOIN tblLocations
		ON tblRestaurants.RestID = tblLocations.RestID
		GROUP BY tblLocations.RestID, tblRestaurants.RestName
		ORDER BY tblRestaurants.RestName ASC");
		
		echo '<option selected="selected">--Select Place--</option>';
		while($row=mysql_fetch_array($sql))
		{
		echo '<option value="'.$row['Rid'].'">'.$row['name'].'</option>';
		}
//}
?>

Open in new window

Avatar of DS928

ASKER

Yes and No, I have the whole thing working.  The dilema started when I decided to have five different options on the first box.  Select this and this loads, select that and that loads, etc.  
I just need place_place.php to run and populate listbox doggie and I am good to go!  Why is this not working?

switch(n){
	case "Place":
	$(".Doggie").load(function()
	$.ajax({
     		type: "POST",
        		url: "place_place.php",
        		cache: false,
        		success: function (html) {
	$(".Doggie").html(html);
	} 
	});
	});
	break;

Open in new window

SOLUTION
Avatar of EndreCz
EndreCz
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of DS928

ASKER

Thank you. I appreciate the info about #.  Here is where I am now.
$(document).ready(function () {
	$.ajax({
	url: 'place_place.php',
	success: function (html) {
	var source = $.parseJSON(html);
	$("#Doggie").html(html);
	},
	error: function () {
	alert('The source is unavailable!');
	}
	});
	});

Open in new window

Is it better now?
Avatar of DS928

ASKER

No, still not working.. I am here now.

alert("Header is Place");
$(document).ready(function () {
	$.ajax({
	url: 'place_place.php',
	success: function (html) {
	//var source = $.parseJSON(html);
	$("#Doggie").html(html);
	},
	error: function () {
	alert('The source is unavailable!');
	}
	});
	});

Open in new window


I'm getting to the right place because "Header Is Place"  is popping up.  But the listbox doggie is not populating.

Here is the place_place.php  maybe something there?
<?php
require('config.php');

$sql = mysql_query("SELECT tblLocations.RestID as Rid, tblRestaurants.RestName as name
		FROM tblRestaurants INNER JOIN tblLocations
		ON tblRestaurants.RestID = tblLocations.RestID
		GROUP BY tblLocations.RestID, tblRestaurants.RestName
		ORDER BY tblRestaurants.RestName ASC");
		
		echo '<option selected="selected">--Select Place--</option>';
		while($row=mysql_fetch_array($sql))
		{
		echo '<option value="'.$row['Rid'].'">'.$row['name'].'</option>';
		}

?>

Open in new window


When the PHP code was in the body of the page it had this before it...
<select name="Doggie" class="Doggie" id="Doggie">

Open in new window


Do I need to include this in place_place.php and if so, how would I do that?
If you are using Chrome, you can see the output of your PHP script in Developer Tools (press F12 to open), in Firefox you can use FireBug (http://getfirebug.com) for the same purpose. This way you can check whether your PHP script is called, and its return value is correct.

I still would consider to start again from scratch, and using "more proper" methods:
- The options should be returned from the server side as a JSON array
- On the client side the array should be converted to Option elements, and those should be inserted using DOM manipulation http://api.jquery.com/append/
Avatar of DS928

ASKER

Thank you,  I will try that next.  In the meantime, this is defintly getting the info, however its not loading into the listbox  When the alert runs, it's not longer empty, it's showing all of the data.  How would I get this to show in the listbox?

alert("Header is Place");
		$(document).ready(function () {
		$.ajax({
		url: 'place_place.php',
		success: function (html) {
alert('This is what is returned from the php script: ' + html);
		$("#Doggie").html(html);
		},
		error: function () {
alert('The source is unavailable!');
		}
		});
		});

Open in new window


and the place_place.php file
<label>Place :</label>
<select name="Doggie" class="Doggie" id="Doggie">
<option selected="selected">--Select Place--</option>
<?php
require('config.php');

$sql = mysql_query("SELECT tblLocations.RestID as Rid, tblRestaurants.RestName as name
	FROM tblRestaurants INNER JOIN tblLocations
	ON tblRestaurants.RestID = tblLocations.RestID
	GROUP BY tblLocations.RestID, tblRestaurants.RestName
	ORDER BY tblRestaurants.RestName ASC");
	while($row=mysql_fetch_array($sql))
	{
	echo '<option value="'.$row['Rid'].'">'.$row['name'].'</option>';
	}
?>
</select>

Open in new window

ASKER CERTIFIED SOLUTION
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
Avatar of DS928

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for DS928's comment #a38797238

for the following reason:

This works, it calls and populates the listbox.
As I've given some advise I suppose it is unfair to close the question accepting OP's answer, as it based on our correspondence.
Avatar of DS928

ASKER

Received advice and did a lot of seraching on my own, problem was not only with the javascript but it lurked in the PHP files as well.  I am grateful for all of the help though.  You are welcome to all of the points, I'm just happy that it is resolved.  Thank you.