Solved

Pass parameter from one PHP  script to another

Posted on 2012-04-08
7
221 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

829 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