Solved

Getting A PHP file to run from Javascript

Posted on 2013-01-19
17
345 Views
Last Modified: 2013-01-20
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
Comment
Question by:DS928
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
17 Comments
 

Author Comment

by:DS928
ID: 38795964
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
 
LVL 2

Expert Comment

by:EndreCz
ID: 38796236
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38796393
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:DS928
ID: 38796704
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
 
LVL 2

Expert Comment

by:EndreCz
ID: 38796799
0
 

Author Comment

by:DS928
ID: 38796817
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
 
LVL 2

Assisted Solution

by:EndreCz
EndreCz earned 500 total points
ID: 38796840
$(".Doggie") refers to <select class="Doggie">

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

How does your HTML look like?
0
 

Author Comment

by:DS928
ID: 38796849
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
 
LVL 2

Expert Comment

by:EndreCz
ID: 38796854
Is it better now?
0
 

Author Comment

by:DS928
ID: 38796868
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
 
LVL 2

Expert Comment

by:EndreCz
ID: 38796959
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
 

Author Comment

by:DS928
ID: 38797050
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
 

Accepted Solution

by:
DS928 earned 0 total points
ID: 38797238
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
 

Author Comment

by:DS928
ID: 38797397
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
 
LVL 2

Expert Comment

by:EndreCz
ID: 38797398
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
 

Author Comment

by:DS928
ID: 38797509
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question