0

I know that it's kind of wrong to ask a question that's almost been answered in the past, but even though I'm trying to use this solution, it doesn't work.

Basically, I have a php table with a search link created in a HTML button from the strings inside the table.

Let's say that this string is creating a search in the website called Discogs and that the information I'm searching through a created link is the word "Beatles".

www.discogs.com/search/?q=Beatles

You notice the ?q=

Everytime I click on the link through a php code which looks like this, the opened link brings me to www.discogs.com/search/? WITHOUT THE REST OF STRINGS (in that case, the word Beatles)

I tried to rawurlencode the ? to have it as a %3F.

Here is what my code looks like

$discogslink = 'http://www.discogs.com/search/'.rawurlencode('?').'q='.'Beatles' ;
  $form = "<form action='$discogslink' >";
  $form .= "<input type='submit' value='Discogs'>";
  $form .= "</form>";      
 echo '<td class="'.$lps->type.'">'.$form.'&nbsp;</td>'; 

The link that I would like to open is http://www.discogs.com/search/?q=Beatles

The link that opens is : http://www.discogs.com/search/? (nothing after the '?'...)

Do you have any idea why it does that?

BONUS QUESTION : How can I make the button open in a new tab instead of the same one?

Community
  • 1
  • 1
Patrick
  • 11
  • 2
  • 2
    As for your “bonus question”: `form` has a `target` attribute. // But since you don’t need to submit actual form data here, you might as well use a normal link in the first place – if you want it to _look_ like a button, then format is using CSS. – CBroe Jun 26 '15 at 20:55

1 Answers1

2

The reason for this is the default behaviour of HTML forms. They send data as a GET request, just like links do. So in your case the content of the form (which is empty) overwrites your q-parameter because the form content wins over the parameters specified in the action link.

The solution is to add a hidden field:

<input type="hidden" name="q" value="Beatles"></input> 

And concerning the new window:

<form target="_blank" ...
Tobias Müller
  • 145
  • 1
  • 8
  • Now you can see the hidden field :) – Tobias Müller Jun 26 '15 at 20:54
  • Thanks for your help. I'm sorry for my lack of programming skills haha. I should put it before the form? By the way, my created link doesn't have "Beatles" as a value, but something created from multiples strings (the code actually looks like this : `$createdlink = 'http://www.discogs.com/search/'.rawurlencode('?').'q='.rawurlencode($lps->description).'%20'.rawurlencode($lps->catno).'&type=release&layout=med'` ;) Is that what I should put inside the "value=" string? – Patrick Jun 26 '15 at 21:03
  • No, you should use a hidden input field for every value, i.e.: ` ` Depending on the values of $lps->descr‌​iption and $lps->catno it actually might be smart to use a separate field for each of them. – Tobias Müller Jun 26 '15 at 21:22
  • I wish I understood where to put it. Should I totally remove the $createdlink from the code? Everything I'm trying is making my .php page stop responding. – Patrick Jun 26 '15 at 22:14