Jump to content

Using PHP variables in external javascript pages


envexlabs

Recommended Posts

Hey,

 

I have an AJAX edit-in-place script that i'm using. Here is the code for editinplace.js:

 

function init(){
makeEditable('desc');
makeEditable('pizza');
}

function makeEditable(id){
Event.observe(id, 'click', function(){edit($(id))}, false);
Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false);
Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false);
}

function edit(obj){
Element.hide(obj);

var textarea = '<div id="'+obj.id+'_editor"><textarea id="'+obj.id+'_edit" name="'+obj.id+'" rows="4" cols="60">'+obj.innerHTML+'</textarea>';

var button	 = '<div><input id="'+obj.id+'_save" type="button" value="SAVE" /> OR <input id="'+obj.id+'_cancel" type="button" value="CANCEL" /></div></div>';

new Insertion.After(obj, textarea+button);	

Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj)}, false);
Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);

}

function saveChanges(obj){

var new_content	=  escape($F(obj.id+'_edit'));

obj.innerHTML	= "Saving...";
cleanUp(obj, true);

var success	= function(t){editComplete(t, obj);}
var failure	= function(t){editFailed(t, obj);}

  	var url = 'edit.php';
var pars = 'content='+new_content+'&edit=true';
var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});

}

 

I need to also pass a PHP variable, lets say $a, along with

var url = 'edit.php' var pars = 'content='+new_content+'&edit=true';

but i dont know how to get the php variable from index.php -> editinplace.js -> edit.php

 

Any help is greatly appreciated!

Take the PHP variable and pass it in the URL.

 

So with $ExamplePHPVaraible

var url = 'edit.php?var=<?=$ExamplePHPVaraible?>' var pars = 'content='+new_content+'&edit=true';

 

Then somewhere get the variable:

 

$hereItIs = (isset($_GET["var"])) ? $_GET["var"] : NULL;

 

Hope that helps...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.