passing a parramter from php to javascript back to php - php

I have a php page, which using javascript, creates a popup window which contains another php page. the php page in the popup window is basically a form, and will use that value to insert into a database. Using the following function to load the php page in a popup:
phppopup('edit_status.php?cmd=EditStatusData')
function phppopup(page){
child1 = window.open (page, "Ebay Auctions", "height=600,width=600,status=yes,toolbar=no,menubar=no,location=no");
child1.document.close();
}
How can i pass a value from the calling page to the page in the popup?
I am trying at the moment:
echo "<p>Edit User Info
<p>Historie;
Which generates the following html:
Edit User Info
</strong></strong></strong></strong></p><p><strong><strong><strong><strong>Historie
It works fine for updateByEdit, why not for phpopup?
$pk is just an integer, which display fine in the page it the window is called from.

Well, you're already doing it - the GET variable in edit_status.php will have one entry, with key='cmd' and value='EditStatusData'. Just pass as many parameters as you need (while being mindful that such data will be publicly viewable).

Just add the arguments as part of the get request:
phppopup("index.php?var1=value1&var2=value2"); // Get
etc.
Alternatively, for passing it arguments when it is open already, use the handle that is returned (child1) and access that window's DOM via javascript.
child1.document.getElementById("myelement").innerHTML = "Hello World"; // Parent Child
If you have trouble with this, get the child window to trigger a callback in the parent window via window.opener when the child document has loaded, e.g.
window.opener.myCallback(requestedinfo); // Parent window callback function

I noticed that the generated HTML contains a query string that has HTML entities ('& amp;') instead of their actual equivalents. Make sure that updateByEdit(...) isn't returning entities or the query string won't be printed correctly.

Related

Passing part of a URL to PHP and return results from a database

I'm working on an interactive map and I am wondering if it's possible to pass part of a URL to a PHP script and have it look up information and then fetch it back to display within an iframe in a lightwindow.
The map is CSS based and each the link on each state within the href contains the state name (e.g. - #statename).
When a user clicks on the map, the #statename is appended to the URL.
I'm wondering that if it's possible to pass that part of the URL (#statename) to PHP and have it run the script from there?
Here's a link to the interactive map that I am working on. If you click on the state of Texas you'll see what I mean:
http://sjdunham.com/beta/maptest/
Yep, it's fairly easy, you could bind an event to the hashchange event, if you are using jQuery, you could do something like this:
$(window).on('hashchange', function() {
console.log(window.location.hash); // replace this
});
You can then replace the console.log line with an ajax call to the PHP script and use whatever you send back to modify the iframe when the request is done.

Placing javascript/jquery functions in a universally accessible page

I am trying to clean up my pages, and one of the major ways that I am doing so is to put JavaScript and JQuery functions into a file called 'scripts.js' that is automatically accessed upon page load.
I've run into a problem with functions that use php to call from the page itself. For example, the following function doesn't work, and in fact 'kills' the script for all pages (so now things that were supposed to be hidden are not, and things are not loading properly). I've narrowed it down to the fact that I use to call a variable. I would really like to be able to keep functions using PHP in this universal file as opposed to clogging up the HTML template pages, any thoughts on either how to make this work, or if not how else I may be able to call the values needed? They are always extracted to the page before rendering if that helps.
function positiveSelect()
{
var size = <?php echo $idea[0]["size"]; ?> * 1;
if (size > 5)
return true;
else
return false;
}
if you can't retrive your data form the DOM itself you can store values with the corresponding object:
<div data-size=20>
and then retrive it with:
$(element).data("size");
or if you have global data you want to store you can create a value "container" in the head of you html document like this:
<script type="text/x-json" class="global-data">{"value1":"1","value2":"2"}</script>
and then read the content of that element and parse it with JSON.parse
If this function is that specific to a certain page, you might want to add a second js script that just gets loaded on that page.
An alternative would be to echo out a js variable in that php page and have your code call that function with that variable as a parameter.
You can give the javascript a ".php" extension and call it in the script in the same exact way:
<script type="javascript" src="path/to/scripts.php"></script>
You could just name the generate scripts file scripts.php or scripts.js.php; then the PHP preprocessor will process the file and the PHP statements will be evaluated.
When mixing php or any server side language with javascript you need to be aware that the php gets executed only once when the javascript file is created on the client side.
This is probably why you are getting unexpected results. As you move from page to page the php snippet in your global scripts.js will not get updated.

How to manage parameter in thick box tb_show function?

I am using below script on a form (say, form-A) to load another form (form-B) in thickbox, with values passed by controller.
First, in form-A, i am selecting one option from dropdown "customerID", then "Add Project" button (with id 'addProject') becomes visible, on clicking which, a thickbox appears with form-B in it. Here, in form-B, i want to pass selected customer. How can I do that?
I tried below code, and tried to access the $_GET['custID'] in controller's manage_project function, but it is showing blank. But when i am alerting the url1 (i have commented below), ID is coming there.
Below code is in form-A view file.
('#addProject').click(function(){
var url1='<?php echo SITEURL ?>/xome/invoice/manage_project?TB_iframe=true&height=800&width=700&inlineId=innerDiv&class=thickbox&custID='+$('#customerID').val();
//alert(url1);
tb_show('Add More Project',url1,'');
})
According to documentation at (http://thickbox.net/):
Important to Remember: Add all other query parameters before the
TB_iframe parameters. Everything after the "TB" is removed from the
URL.
So, try add custID before TB_iframe. And then you will be able to operate with a variable in the script, such as access them via $_GET['custID']. For example:
var url1='<?php echo SITEURL ?>/xome/invoice/manage_project?custID='+$('#customerID').val() + '&TB_iframe=true&height=800&width=700&inlineId=innerDiv&class=thickbox';

Refresh Div Containing PHP Function

So I have the following code:
<div id="currentmotto"><?php mottoGrab($name); ?></div>
And what this does is it uses curl to screen scrape a users motto and display it on the site. What I need it to do is for that function to refresh every few seconds to see if the user has updated the motto.
I know with jquery I can use the .load('phpfile.php') but the problem then is if I put that function in that file, it no longer gets the $name variable as that is from another page.
Any ideas?
Pass name to phpfile.php via the query string:
.load('phpfile.php?name=THENAME');
Then grab the name from within phpfile.php using $_GET['name'] and stick it in the function.
OR
Make an AJAX request passing the name and update the page using javascript.
Since PHP is executed server-side, it doesn't change once the client loads the page. The only way to refresh a div without JavaScript is to reload the page.
I would try using AJAX to get the name from the other file and update the HTML with JavaScript

Variable from JavaScript to PHP

Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

Categories