Hey guys, I'm trying to mix php and it's not working. I made a form that takes user input and changes the website they input to it's IP address. Next I tried to put that IP as a variable $ip and tried to put that variable in a link(href="") and the variable also as the link text. Heres the code:

<?php
        $site = $_POST['site'];
        $ip = gethostbyname($site);
        ?>

And then the link:

<a href"<?print $ip;?>"><?print $ip;?></a>

I can get it to just display the IP but I can't put it into a link.

This is my redone code that should have been fail-safe, but it doesn't even display the "or die"??

<?php
        if (isset($_POST['submit'])) {
            if(isset($_POST['site'])) {
                $site = $_POST['site'];
                $ip = gethostbyname($site);
                function showLink(){
                    print "<a href='<? print $ip?>'><?print $ip?></a>" or die ($ip);
                }
            }
        }
        ?>

Why don't you test your post variable by adding:

echo $_POST['site'];
echo $_POST['submit'];

before any of the if statements.

You don't need to repeat <? ?> tags in a print output, it should be a string.
Also, you don't call your showLink() function so it is not executed.
Try this:

<?php
        if (isset($_POST['submit'])) {
            if(isset($_POST['site'])) {
                $site = $_POST['site'];
                $ip = gethostbyname($site);
		showLink();
                function showLink(){
                    print "<a href=".$ip.">".$ip."</a>" or die ($ip);
                }
            }
        }
        ?>

Thanks this helps a lot!

Regarding for your original code, the reason why the IP displayed but you couldn't click the link is because you were missing an = between the href and the first quote. Try this:

<a href="<?= $ip ?>"><?= $ip ?></a>

Also notice that since you have shorttags enabled I used shortened syntax to print variables. It removes the need for print or echo in short, one-line variable outputs and will be compatible with PHP (with shorttags enabled) until 6.0.0.

Yeah I saw that and changed it but in my original code it still didn't work. But it's all good now thanks a lot!

Yeah I saw that and changed it but in my original code it still didn't work. But it's all good now thanks a lot!

No problem! Glad it's all fixed! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.