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!

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.