Solved

Getting A PHP file to run from Javascript

Posted on 2013-01-19
17
336 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
  • 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 108

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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now