PHP Guestbook: Drop down values and functions

Everything related to the visual and coding aspects of websites.
Post Reply
Andrea
Posts: 109
Joined: Fri Mar 25, 2016 11:23 am
Contact:

PHP Guestbook: Drop down values and functions

Post by Andrea »

Hello! I've been trying to tackle this problem myself over the last few days or so, and haven't really been able to come up with an elegant solution. So instead of breaking more pages, I thought I'd come to all you code-gurus out there for some much needed guidance! (Seriously... ; A;)

I'm using PHP Guestbook to set up a theoretical guestbook for my site (I'm not sure if I'm going to implement it just yet, haha). Everything's working great so far, but there's one problem that's bothering me a lot.

I set up a drop down menu so visitors would be able to pick and choose a site they'd like to comment on. I can display the text of the selected option just fine, but I'd also like to find a way to link the text to the URL of the site. So if someone selected "lost-boy.org," it would show up as "lost-boy.org" and if someone selected "And they shall fall," it would show up as "And they shall fall."

So, uh, I think what I'm asking is: How can I get the selected option value of the drop down menu recognized and displayed the way I would like?

Is what I'm trying to do even possible with one value? Or do I have to create multiple values and functions?

Here's my coding so far:


the drop down menu as it is on the form:

Code: Select all

<div class="gbook_left"><span class="gbook_entries"><b>Comment on:</b></span></div>
<div class="gbook_right">
 <select type="dropdown" name="from" value="<?php echo $from; ?>">
  <option value="lost-boy.org">lost-boy.org</option>
  <option value="fanlistings">&mdash; fanlistings (please specify which one)</option>
  <option value="And they shall fall">And they shall fall (Shadow of the Colossus)</option>
</select>
</div>
the PHP coding connected to the form value ($from):

Code: Select all

if ($from)


          {

            $from = '<a href="/// SITE URL HERE /// '.$settings['target'].' rel="nofollow">'.$from.'</a>';
        }


        else
        {
            $from = '<a href="http://lost-boy.org" '.$settings['target'].' rel="nofollow">lost-boy.org</a>';
        }
The "/// SITE URL HERE ///" is just a placeholder because I'm not sure what to do, haha. Also, I don't know if this is important, but the value I'm currently using is one that was provided by the script for a different purpose. I just thought it'd be easier to hijack it for my own uses instead of creating a new one.

I know my explanation is probably not the greatest and it's suuuper late as I'm typing this, so I probably missed something important. Feel free to ask me to clarify things or provide more coding as needed!

Alsooo... I'm terrible at PHP, so assume you're explaining things to a rock (I'm almost positive I've butchered all the PHP semantics I could possibly butcher with this one post!). Thanks for any assistance you can provide! :heart:
Lysianthus
Posts: 23
Joined: Sun May 17, 2015 9:34 pm
Location: Philippines
Contact:

Re: PHP Guestbook: Drop down values and functions

Post by Lysianthus »

Hello, Andrea! The quickest solution that came to my head is this:

Code: Select all

switch ($from) :
    case 'lostboy.org':
        $url = 'http://lostboy.org';
        break;

    case 'sample text':
        $url = 'http://sa.mp.le';
        break;

    case 'My Site':
        $url = 'http://mysite.com';
        break;
What we did is use the switch control structure to assign a value to $url depending on the value of $from. Just follow this format and add more cases.

Insert this code after

Code: Select all

if ($from) {
Then, replace "/// SITE URL HERE ///" with $url, so:

Code: Select all

$from = '<a href="' . $url . $settings['target'] . ' rel="nofollow">'.$from.'</a>';
The downside to this solution is that you have to edit the PHP file every time you make changes to the options in the HTML document.

I hope it's helpful. :heh: Let me know if it works! ^^
Affelius (creative) ☆ Asclaria (network) ☆ Lysianthus (personal)
Andrea
Posts: 109
Joined: Fri Mar 25, 2016 11:23 am
Contact:

Re: PHP Guestbook: Drop down values and functions

Post by Andrea »

YES, IT DID!!!

Ugh, thank you so much for your help, Lysianthus! I tried things from arrays to echos to $_GET/$_POST to this method you provided, but I could never get any of it to work because I had no clue how to arrange or implement anything.

I had to edit the code you provided just a bit as $url was a value already assigned to something else. For some reason the "switch" code also wouldn't work for me without brackets, haha. But other than that, it's PERFECT. Here's what I used in my coding if anyone is having trouble!



for gbook.php or the page you set as your guestbook:

Code: Select all

switch ($from) {
    case 'lost-boy.org':
        $furl = 'http://lost-boy.org';
        break;

    case 'fanlistings':
        $furl = 'http://lost-boy.org';
        break;

    case 'And they shall fall':
        $furl = 'http://lost-boy.org/sotc';
        break;

}


 if ($from)


          {

            $from = '<a href="'.$furl.'" '.$settings['target'].' rel="nofollow">'.$from.'</a>';
        }


        else
        {
            $from = '<a href="http://lost-boy.org" '.$settings['target'].' rel="nofollow">lost-boy.org</a>';
        }

Again, THANK YOU SO MUCH! This was such a headache for me to figure out, but now it's solved all thanks to you. YOU'RE AWESOME. :inlove: :heart: :heart: :heart:
Lysianthus
Posts: 23
Joined: Sun May 17, 2015 9:34 pm
Location: Philippines
Contact:

Re: PHP Guestbook: Drop down values and functions

Post by Lysianthus »

Andrea wrote:YES, IT DID!!!

Ugh, thank you so much for your help, Lysianthus! I tried things from arrays to echos to $_GET/$_POST to this method you provided, but I could never get any of it to work because I had no clue how to arrange or implement anything.

I had to edit the code you provided just a bit as $url was a value already assigned to something else. For some reason the "switch" code also wouldn't work for me without brackets, haha. But other than that, it's PERFECT. Here's what I used in my coding if anyone is having trouble!



for gbook.php or the page you set as your guestbook:

Code: Select all

switch ($from) {
    case 'lost-boy.org':
        $furl = 'http://lost-boy.org';
        break;

    case 'fanlistings':
        $furl = 'http://lost-boy.org';
        break;

    case 'And they shall fall':
        $furl = 'http://lost-boy.org/sotc';
        break;

}


 if ($from)


          {

            $from = '<a href="'.$furl.'" '.$settings['target'].' rel="nofollow">'.$from.'</a>';
        }


        else
        {
            $from = '<a href="http://lost-boy.org" '.$settings['target'].' rel="nofollow">lost-boy.org</a>';
        }

Again, THANK YOU SO MUCH! This was such a headache for me to figure out, but now it's solved all thanks to you. YOU'RE AWESOME. :inlove: :heart: :heart: :heart:
Oops, sorry, I gave you the incomplete syntax. :pale: It's supposed to be closed with `endswitch;`, but brackets work, too. But, yay, it worked!!! :D You're welcome, Andrea! :heart:
Affelius (creative) ☆ Asclaria (network) ☆ Lysianthus (personal)
Post Reply