How come my form doesn't work?

I’ve been trying to get this form to submit:

synonyms.php

<form action="synonyms-script.php" method="post">
    <input type="text" value="<?php echo $var?>" name="var" />
    <input type="submit" value="Get Synonyms!" />
</form>

synonyms-script.php

<?php
$var = 'http://words.bighugelabs.com/api/2/api-numbers-here/'.$_POST['var'].'/';
echo $var;
?>

It outputs the correct url on the page but doesn’t actually get anything.

Now this form works:

<form name="myform" action="http://words.bighugelabs.com/api/2/api-numbers-here/word/" method="POST">
<div align="center">
<br><br>
<input type="text" size="25" value="Enter your word here!">
<br><input type="submit" value="Get Synonyms!"><br>
</div>
</form>

This one actually gets the information and outputs it on the page.

How can I get the above form to work? Maybe I might need javascript?

Thank you

Where is $var set?

Please define “works”. What is it that you want the form to do?

I don’t know. It’s on the synonyms-script.php page?

But doesn’t that come after the synonyms.php page?

@SamA74

It gets the synonyms for the word “word”

Outputs this:

noun|syn|news
noun|syn|intelligence
noun|syn|tidings
noun|syn|discussion
noun|syn|give-and-take
noun|syn|parole
noun|syn|word of honor
noun|syn|Son
noun|syn|Word
noun|syn|Logos
noun|syn|password
noun|syn|watchword
noun|syn|countersign
noun|syn|Bible
noun|syn|Christian Bible
noun|syn|Book
noun|syn|Good Book
noun|syn|Holy Scripture
noun|syn|Holy Writ
noun|syn|Scripture
noun|syn|Word of God
noun|syn|arcanum
noun|syn|computer memory unit
noun|syn|hypostasis
noun|syn|hypostasis of Christ
noun|syn|info

The first one outputs this on the page:

http://words.bighugelabs.com/api/2/api-numbers-here/word/

exactly the link I want it to use but it doesn’t “do” the link but just echos it on the page.

Well, yes, that’s all you told it to do.
Do you want it to be a link?

@Gandalf

That’s why I think some kind of javascript might work. Just to somehow echo what I write into the input box into the action link.

<?php
$var = 'http://words.bighugelabs.com/api/2/api-numbers-here/'.$_POST['var'].'/';
echo '<a href="'.$var.'">'.$var.'</a>';
?>

@SamA74

I don’t care as long as it works. I just need to be able to change the “word” in the link to something else. :slight_smile:

The above should echo it out as a link. Though I would advise some validation and sanitisation in the processing.

@SamA74

Thanks you, it works.Looking these things up on the internet is so slow with all the ads on the dictionary websites!

I’m curious to why you want a form to output a link from a word that is input. I can’t help but feel that this may be a case for using GET, although I am not party to your intentions for the form.

@SamA74

Well I would like it to work where it returns the synonyms in one click and formats them nice. But I’m just using it to look up words when I’m writing product descriptions and when people do searches I can add synonyms for their searches in the admin section.

I just thought that if your intention was to take the user directly to the url, you could use the GET method, as it adds whatever variable from the form to the url.
Something like:-

http://words.bighugelabs.com/api/2/api-numbers-here/?var=MyWord

Not sure if that’s what you want.

@SamA74

The url has the be exactly formatted. I don’t think ?var= will work.

One day I might fix it up better. It works fine. Thanks

It is not wise to use the $_GET variable on a form. Anyone who says otherwise supports legacy codes. You should forget this whole ?var= thing because anyone can change it to like ?var=This website uses legacy code and the webmaster of this website doesn't know a thing he is doing and then break your logic. Forms should always relate to $_POST, BUT not for form submission as many new beginners tend to do and just copy&always codes from 1990’s article. I am starting to dislike PHP for this fact. The people who’ve had x amount of years using PHP don’t want to help new users so they troll for their enjoyment and tell new users hat it’s ok to use legacy codes. And new users are too naive to do research on stuff.

This may be true. But if you want code to work with an API any “rules” the API may have need to be followed.

Interestingly, if you look at words.bighugelabs.com the sites own form is GET

  <form id="inputform" action="/" method="get" onsubmit="return lookup()">
    <input id="q" name="q" class="form-control" value="">
    <input type="submit" value="Lookup" class="btn btn-primary">
  </form>

Though it looks like with JavaScript the “q” is passed as

function lookup() {
  document.location = '/'+escape($('#q').val());
  return false;
}

@spaceshiptrooper

Yeah,

It’s secure the way I’m using it. Anyway, who’s going to want to break into the synonyms database a steal all the synonyms? :slight_smile: