Solved

Pass parameter from one PHP  script to another

Posted on 2012-04-08
7
219 Views
Last Modified: 2012-04-08
I would like to shorten the amount of code in my HTML file to keep it clean.  I have a PHP script which takes the title of a organization's officer, goes to an XML file to look up the current holder's name and email address, then returns a clickable link.  Here's the code:

<?php
    $xml_path = "example6/";
    $xml_doc = new DomDocument;
    $xml_doc->load("example6/contacts.xml");

    $xsl_doc = new DomDocument;
    $xsl_doc->load("example6/email.xsl");

    $xp1 = new XsltProcessor();
    $xp1->importStyleSheet($xsl_doc);

    $params['title'] = "president";
    $xp1->setParameter("", $params);

    $contact = $xp1->transformToXML($xml_doc);
    if ($contact = $xp1->transformToXML($xml_doc)) {
        return $contact;
    } else {
        trigger_error("XSL transformation failed.", E_USER_ERROR);
    } // if
?>

Open in new window


Is there a mechanism I can use, like "include", so that on one HTML page I can get the president's contact info, and on another page I can get the membership chairman's contact info?  I'm hoping for something simple like:

<?php
include getcontact.php ("president");

or

include getcontact.php?title=president;
?>

Open in new window

0
Comment
Question by:VieleFragen
  • 4
  • 2
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37822019
I don't understand quite what you have.  If it is truly an HTML page and not a *.php page, you can use AJAX to GET "getcontact.php?title=president" .  But if it is a PHP page, you can simply declare '$params['title'] = "president"' at the top of the page followed by "include getcontact.php;" with that line removed.

HTML doesn't have a 'include' mechanism which is why you would have to use AJAX to get the info.  And when you 'include' a file in PHP, it doesn't run the file at that point, it 'includes' the file text in the current page where it gets run inline with the rest of the page code.  The query string added to the end in that case will get ignored.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37822026
And this line with a single '=' is not a comparison but an assignment:

if ($contact = $xp1->transformToXML($xml_doc))

Open in new window


If you want a comparison, it needs to be '==', not '='.
0
 
LVL 1

Author Comment

by:VieleFragen
ID: 37822096
Thanks for the correction on the '=='; I'll update my code with that.

What I have is an HTML page with a lot of content, but in the footer is a paragraph like "Please send corrections to joesmith@test.com".  The PHP code I posted earlier is part of this paragraph, and "joesmith@test.com" is an actual hyperlink.  This hyperlink is created as the page loads.  Because this is a PHP script, I have to name the HTML file with a PHP extension, otherwise the script doesn't do its job.

I want to embed this kind of lookup in several of our website's pages, but for different contacts.  I can create separate standalone PHP scripts, which I have done for testing, for each officer.  For example, in the HTML page I can include getpresident.php.  On another page I can include getlinkseditor.php.  But it seems silly to have multiple scripts when the only thing that changes is the title of the contact.  The XML transformation setup, the data file and the stylesheet all remain constant.

Does my explanation make sense?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37822191
to shorten the amount of code in my HTML file to keep it clean...
Usually the correct approach is to lengthen the amount of code, reducing the number of operations to one per line!

You can tell the server to parse .htm and .html files through PHP, just like .php files.  (Especially useful with Dreamweaver).

A sensible design patten might include some OOP code that put all the variables in the right kind of objects.  HTH, ~Ray
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 200 total points
ID: 37822246
I would keep the current method except the code at the top would become 'inline' so that you can use 'echo $contact;' instead of 'return $contact;'.  The php in page would be:

<?php
$params['title'] = "president"  // or which ever officer for this page.
include getcontact.php;  // which is the code at the top minus the $params[] line.
?>

Open in new window


That turns 'getcontact.php' into an 'inline function' that is common to all the pages leaving only the value of $params['title'] to be set on each page which it has to be anyway.
0
 
LVL 1

Author Closing Comment

by:VieleFragen
ID: 37822317
Thanks Dave, that works perfectly!  I appreciate the help.

Alan Varga
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37822334
You're welcome, glad to help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

810 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