Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pass parameter from one PHP  script to another

Posted on 2012-04-08
7
Medium Priority
?
229 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:Alan Varga
[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 84

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 84

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:Alan Varga
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

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 84

Accepted Solution

by:
Dave Baldwin earned 800 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:Alan Varga
ID: 37822317
Thanks Dave, that works perfectly!  I appreciate the help.

Alan Varga
0
 
LVL 84

Expert Comment

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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
Suggested Courses

610 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