Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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

0
DS928
Asked:
DS928
  • 9
  • 6
2 Solutions
 
DS928Author Commented:
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

0
 
EndreCzCommented:
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.
0
 
Ray PaseurCommented:
This article shows all the moving parts. You can copy the scripts, install and run them, then modify them for your own needs.
http://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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DS928Author Commented:
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

0
 
EndreCzCommented:
0
 
DS928Author Commented:
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

0
 
EndreCzCommented:
$(".Doggie") refers to <select class="Doggie">

$("#Doggie") refers to <select id="Doggie">

How does your HTML look like?
0
 
DS928Author Commented:
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

0
 
EndreCzCommented:
Is it better now?
0
 
DS928Author Commented:
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?
0
 
EndreCzCommented:
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/
0
 
DS928Author Commented:
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

0
 
DS928Author Commented:
This works.  Question answered.

$(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 () {
				}
				});
				});

Open in new window

0
 
DS928Author Commented:
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.
0
 
EndreCzCommented:
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.
0
 
DS928Author Commented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now