Using PHP Dom to change href

Hi all,

   I have a main webpage in PHP and it calls an AJAX page.  In that AJAX page I have it doing a bunch of SQL queries.  From one of the queries I get a result that I need to use in an HREF statement that has to dynamically change anytime the AJAX script is called.  I used

$dom = new DOMDocument();

Open in new window


on the main page and this in the AJAX page

$dom->getElementById('ACGFM')->setAttribute('href', 'domain.com/acgfm.php?q=' . $q2);
$dom->saveHTML();

Open in new window


But I'm getting an error "call to a member function getElementById() on a non-object"

Help!!

Thanks - Zack
LVL 4
zack4x4Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
"In the AJAX page"? How AJAX page knows about $dom? If you create an object in a page, this object is available only in that page unless you don't serialize it to pass it to another page (or if you don't use JSON to do so.
But if you're using AJAX, you can just change the haref within the javascript using the returned value from the AJAX page.
Anyway, I'd need to see more code from the main page, from the AJAX page and from the javascript  you're using to make Ajax call.
Julian HansenCommented:
The problem here is you are mixing PHP (server side) and Javascript (Clinet Side)

Your PHP document does not have access to the page that initiated the AJAX call.

To do what you want to do you have to make the AJAX call from your client side page to your PHP script which then returns the requested value and then using JavaScript in your AJAX success call you need to set the HREF of the desired element.

This simple example demonstrates the concept.
HTML
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
  Data <input type="text" name="linkfield" /> <br/>
  <input type="submit" />
</form>
<a href="#" id="target">URL</a>
<script src="http://code.jquery.com/jquery.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="js/bootstrap.js"></script>
<script type="text/javascript">
$(function() {
  $('form').submit(function() {
    $.ajax({
      url: 'geturl.php',
      data: $(this).serialize(),
      type: 'POST',
      success:function (resp) {
        if (resp != '') {
          $('#target').attr('href', resp);
        }
      }
    });
    return false;
  });
});
</script>

</body>
</html>

Open in new window

PHP
<?php
$linkfield = empty($_POST['linkfield']) ? '' : $_POST['linkfield'];
echo $linkfield;

Open in new window

The code takes what you enter AJAX's it to the PHP script which echo's it back to the script which then changes the URL link.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
zack4x4Author Commented:
Thanks!
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.