Solved

Pass parameter from one PHP  script to another

Posted on 2012-04-08
7
218 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 200 total points
Comment Utility
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
Comment Utility
Thanks Dave, that works perfectly!  I appreciate the help.

Alan Varga
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now