Jump to content


Photo

Including a PHP in a HTML via JS


  • Please log in to reply
13 replies to this topic

#1 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 20 August 2006 - 10:54 PM

I really wasn't sure if I should use a PHP forum or JS Forum for this one.

What I want to know is:

Is it possible to use JS to include a PHP?     I know that PHP is SS and JS is CS but if the JS calls a file from the server to 'include' will that server parse the php file and spit out the html?  ...just like it does if you go direct to the PHP file in your browser?

Let me know if should really be asking this in a JS forum.    ...it does seem more like a PHP question though.

...sorry guys forgot to mention: The file that I was going to have the JS in to call the PHP is itself a PHP file, (which I wouldn't think would matter really)
- Humpty
  #> Where does the light go when it goes out?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 20 August 2006 - 11:06 PM

One obvious question is "what happened when you tried it?"

The JS isn't going to execute until it hits the client browser.  I'm not sure why you want/need to use such an apparently convoluted technique and can't help wondering if some minor code/logic changes would allow a simpler approach to achieve the end result you want.
Legend has it that reading the manual never killed anyone.
My site

#3 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 20 August 2006 - 11:29 PM

One obvious question is "what happened when you tried it?"

I haven't tried it yet I'm still trying to find my code and syntax for the JS include, and then how to change that on the fly :D

...can't help wondering if some minor code/logic changes would allow a simpler approach...


OK, I'll spill the beans and maybe some better logic will come out.

It's for letting a user add an item to a database where there is a category, sub-category and sub-sub category. 

There could be a lot of variables depending on the category, so for example I wanted a user to select thier category, from that I can initiate JS to include the appropriate PHP file that lists all the sub categories of that category.  and same for the sub-sub categories depending on what sub category they selected.

There are 2 ways i know to do this:
1) Multiple PHP pages, 1 page lists the categories, select what you want, submit, the next page uses that to display the available sub categories, and so on and so forth.
2) Load all the possible variations into 1 file and use JS to hide everything, only showing what is needed as the user clicks  (this could be VERY large and VERY messy)

hope that helps, (and AndyB, good to see you and barnard still helping people like me, I havne't been in since before they changed the forum layout  (...oh.. i was in last week))
- Humpty
  #> Where does the light go when it goes out?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 20 August 2006 - 11:46 PM

Ah hah! Barand's excellent baaSelect method might well be the kind of thing you're looking for - http://members.aol.c...electguide.html
Legend has it that reading the manual never killed anyone.
My site

#5 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 12:36 AM

thanks for that, will look into it properly when i don't have a headache. ....will let ya know how i go.


Thanks AndyB, Thanks Banard (even though you didn't help me, I think you helped me :D)
- Humpty
  #> Where does the light go when it goes out?

#6 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 01:02 AM

Thanks Barnard,

You have succesfully confused me without even comming into the post :D

AndyB:  I think I'll go with the ol' multi page thing, Still wanna know if can include a PHP using JS.  Will be handy at a later date (see I'm not real smart with this stuff, so for me it is best to have 5 times the code and still be able to understand it)
- Humpty
  #> Where does the light go when it goes out?

#7 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 01:13 AM

HUMPTY what you are looking for is AJAX. That is what you described...

The only things you need is a script that will accept the value of the cat/subcat and select all the cats beneath that.  I have a bit of ajax that does a similar job AND has the js to create the next select box in IE (IE6< doesn't like using innerHTML for select inputrs!!!!!)

If you need it just holla

follow me on twitter @PHPsycho

#8 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 01:19 AM

But how does it work?

Does it load all the data to start with and display the appropriate bit, or does it query the database a second time based on the info in the list?   

if it queries the database a second time does it query it using JS or PHP?
- Humpty
  #> Where does the light go when it goes out?

#9 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 01:21 AM

it queries a second time.

JS calls a php script and waits for it to return a string (a string you can format in php). You can then split that string up and use it to generate/replace html
follow me on twitter @PHPsycho

#10 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 02:03 AM

ok so based on that it is entirely possible to have JS call an include for a PHP file on the fly?  (without having to refresh the page?)

- Humpty
  #> Where does the light go when it goes out?

#11 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 09:04 AM

Yep
follow me on twitter @PHPsycho

#12 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 09:49 AM

Yep


Cool do you have a code example?
- Humpty
  #> Where does the light go when it goes out?

#13 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 10:40 AM

OK...

the ajax controller....
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}

I have this in its own .js file and include it on every page driven by ajax.....

In this eaxmple I have a select group like so

<div id="sections">
<select name="cat" id="cat" onChange="getInfo(this.value,'cat');">
 <option value="0"></option>
 <option value="1">Sec 1</option>
 <option value="2">Sec 1</option>
</select>
</div>

Ok the call....

When that selectbox changes the getInfo function is called..

function getInfo(sec,id)
{
	var url= "/admin/includes/ajax/ssscripts/sections.php?sec=" + sec + "&id=" + id;
	xmlHttp=GetXmlHttpObject(getInfoDone);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

This calls a script (located at the specified url) and passes the parameters to it for processing. Now ajax handles what the script prints out. So the php scritp selects all the sub categories of that cat and generates a single string that can be split by js one it is returned...

Now the php is from My own script so you will have to modify accordingly...

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/includes/config/cmsconfig.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/connection/dbcon.php');

function getSecs ()
{
	$qry	=		"
			SELECT
				*
			FROM
				`section_management`
			ORDER BY
				`table_driver`	ASC,
				`section_root`	ASC,
				`section_par`	ASC,
				`section_title`	ASC
			";
	$qry	=	mysql_query($qry);
	$secarr	=	array();
	while	($row	=	mysql_fetch_assoc($qry))
	{
		foreach	($row as $key	=>	$val)
		{
			$secarr[$key][]	=	$val;
		}
	}
	return $secarr;
}

$action	=	$_GET['action'];

		if	(
			!isset	(
					$_GET['sec']
					)
			)
		{
			exit();
		}
		$id	=	$_GET['id'];
		$secarr = getSecs();
		
		$sec		=	$_GET['sec'];
		$secstring	=	NULL;
		
		
		if	(
			$sec > 0
			)
		{
			$nextsec		=	$sec;
			$key			=	array_keys($secarr['section_id'],$nextsec);
			$root			=	$secarr['section_root'][$key[0]];
			$par			=	$secarr['section_par'][$key[0]];
			if	(
				$type	==	'new'
				)
			{
				$title			=	NULL;
				$root			=	$secarr['section_root'][$key[0]] != 0 ?	$secarr['section_root'][$key[0]]	:	$secarr['section_id'][$key[0]];
				$par			=	$secarr['section_id'][$key[0]];
				$temp			=	array_keys($secarr['section_par'],$sec);
			}
			else
			{
				$title			=	stripslashes($secarr['section_title'][$key[0]]);
				$root			=	$secarr['section_root'][$key[0]];
				$par			=	$secarr['section_par'][$key[0]];
			}



			$i	=	0;
			do
			{
				$tkey		=	array_keys($secarr['section_id'],$nextsec);
				$tkey		=	$tkey[0];
				$tpar		=	$secarr['section_par'][$tkey];
				$troo		=	$secarr['section_root'][$tkey]	==	0	?	$secarr['section_id'][$tkey]	:	$secarr['section_root'][$tkey];
				$nextsec	=	$tpar;
			}	while	($tpar	!=	0);
		
		
		}
		else
		{
			$sec			=	0;
			$root			=	0;
			$par			=	0;
			$key			=	0;
			$title			=	NULL;
		}
		//create list of subsections.
		
		$options	=	"\r\n\t\t<option value=\"" . $sec . "\"></option>";
		$optionsie6	=	$sec . ":";
		$keys		=	array_keys($secarr['section_par'],$sec);
		
		foreach ($keys	as $key => $value)
		{
				$options	.=	"\r\n\t\t<option value=\"" . $secarr['section_id'][$value] . "\">" . stripslashes($secarr['section_title'][$value]) . "</option>";
				$optionsie6	.=	"##" . $secarr['section_id'][$value] . ":" . stripslashes($secarr['section_title'][$value]);
		}
		
		
		echo		$sec			. "||" 
				.	$root			. "||"
				.	$par			. "||"
				.	$options		. "||"
				.	$optionsie6		. "||"
				.	$title			. "||";

}
?>

Now i have edited a bit that you won't need hopefully it won't confuse too much, BUT the part you are really intersted in is how $options and $optionsie6 is generated. My script actually only updates one select box and places links to the previous levels - a breadcrumb. I did this to aide building the slect box and to prevent a few things happening that I am not going to tell as the solutios where far to cleaver to divulge ;)

BUT

the output of the $options is just html to build a selectbox and $optionsie6 look like this. SEC_ID:SEC_TITLE##SUBSEC_ID:SUBSEC_TITLE ad nauseaum..... they are parameters 4 and 5 in the '||' separated string i produce (just so you can see where I grab them in the js)

Bakc to the js. The response handler declared as getInfoDone in our function so here it is (I have removed thebits you don't need - you will have to build the new select box - but that will be easy - this shows you how to populate it)

function getInfoDone()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		params=xmlHttp.responseText.split('||');
		if	(
			browserName	==	"Microsoft Internet Explorer"
			&&
			browserVer	<=	6
			)
		{
			document.getElementById("cat").options.length = 0;
			opts	=	params[5].split('##');
			i	=	0;
			for (x in opts)
			{
				val	=	opts[x].split(':');
				document.getElementById("cat").options[i] = new Option(val[1],val[0]);
				i++;
			}
		}
		else
		{
			document.getElementById("cat").innerHTML=params[4];
		}
	}
}

Again that shows you how to populate a selectbox - and that is the 'hardest' part.

You can figure how to generate the new select boxes - but if you get stuck feel free to bounce some ideas of me.




follow me on twitter @PHPsycho

#14 Humpty

Humpty
  • Members
  • PipPipPip
  • Advanced Member
  • 132 posts

Posted 21 August 2006 - 11:54 AM

I can't concentrate enough to follow that ATM, will look at it and trial it out tomorrow (asking any questions as required if required).

Although it is more code than Barnard's it seems somewhat a lot easier to follow (the bits i looked at so far)

Thankyou very much for that.
- Humpty
  #> Where does the light go when it goes out?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users