Solved

Pass parameter from one PHP  script to another

Posted on 2012-04-08
7
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 2

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Problem 10 68
Building a glossary into your website 9 56
WordPress:  Reset File Permission after server migration 5 57
Why is my select returning NaN 20 32
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

738 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