0

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.

4
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by FlashCreations
0

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);
                }
            }
        }
        ?>
0

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

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

before any of the if statements.

1

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);
                }
            }
        }
        ?>

Edited by Daiva: n/a

1

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.

Edited by FlashCreations: n/a

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!

1

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! :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.