-1

I've been trying to use a button as a link to add some interactivity to my site, however for some reason it doesn't work.

I've been using this code for a search bar:

<span>
<input type="text" id="search" placeholder="Zoek naar een boek of auteur" name="search" method="get" autocomplete="off" style="width:25%;" value="<?php echo htmlspecialchars($search); ?>"></input>
        <button type='submit' style="vertical-align:middle" id='filterButton' onclick="<?php echo htmlspecialchars("'?search=$_GET[search]'");?>">
    <span>Zoek</span>
</button>
</span>

This works perfectly fine, when I click the button, I go to the search page with the variable. In the search results, I use the same method to make a "more info" button, redirecting the user to a page with, well, more information about a book. This is the code in this part

<span>
    <button type='submit' style="vertical-align:middle" id='infoButton' onclick="<?php echo htmlspecialchars("book.phph?id=$resultsid");?>">
    <span>Meer info</span>
</button>
</span>

But this doesn't work, the button does respond when I click it (I get the animation), but I don't get redirected to the page. I've tried a different method:

<span>
<form action="<?php echo htmlspecialchars('book.php?id=$resultsid');?>">
    <button type='submit' style="vertical-align:middle" id='infoButton' />
        <span>Meer info</span>
    </button>
</form>
</span>

This redirects me to "book.php?", but that's it. It ignored the id=$resultsid behinf the question mark. I can't get it to work, can somebody help please?

Thanks ~DagelijksGamer

  • 1
    JS and PHP are two different animals. `onclick=""` you're trying to call a PHP function with onclick; won't work. Use ajax. – Funk Forty Niner Jan 03 '17 at 19:48
  • @Fred-ii- And what does that have to do with my issue? – DagelijksGamer Jan 03 '17 at 19:49
  • `book.phph` is that a typo? – Funk Forty Niner Jan 03 '17 at 19:51
  • @JayBlanchard , apologies, comment wasn't loaded completely, it just sais "JS and PHP are two different animals". – DagelijksGamer Jan 03 '17 at 19:51
  • 1
    What is the actual resulting HTML for any of the "working" and "non-working" versions of this? This seems like an odd way to accomplish what you're trying to accomplish, and I suspect something in the past may have *coincidentally* "worked" without you really knowing what was going on. – David Jan 03 '17 at 19:51
  • look at your developer console then and use error reporting; what does that show you? if that `book.phph` with the added `h` is part of your real code, well there you go; the file doesn't exist. – Funk Forty Niner Jan 03 '17 at 19:53
  • What sets `$resultsid`? When you view the source of your page after it is loaded do you see something like `
    `?
    – Jay Blanchard Jan 03 '17 at 19:55
  • Plus, we don't know if you're using the `$_GET['id']` array. – Funk Forty Niner Jan 03 '17 at 19:56
  • @Fred-ii- Even with the typo corrected, it doens't work. Console gives me 1 error: Uncaught SyntaxError: Unexpected token } on line 112. which corresponds to the Meer info. I'm using a database for the IDs, I don't think that's the problem because it shows that it's redirecting me to the correct page including the variables in the console. – DagelijksGamer Jan 03 '17 at 19:56
  • I didn't understood what you're trying to do, or what you're expecting your button to do – leoap Jan 03 '17 at 19:56
  • @leo_ap When I click the button, I want to redirect someone to a specific page. – DagelijksGamer Jan 03 '17 at 19:58
  • @DagelijksGamer Ok, but Isn't this the default behavior of a link? Why do you want to make a button to behave like a link? – leoap Jan 03 '17 at 20:02
  • @leo_ap I think a button looks better, a lot of sites use buttons, when you click on "Add comment" next to this text window is a input type=submit as well – DagelijksGamer Jan 03 '17 at 20:03
  • @DagelijksGamer The "Add Comment" is a submit button, but it submits data via dynamic request (aka ajax). If you want to "redirect" a user to other page, then youre not doing a dynamic request, youre doing a standard request, that reloads the page in the user browser. If you just like the buttons visuals, you can transform your links into buttons with css – leoap Jan 03 '17 at 20:07

2 Answers2

0

You shouldn't need to use htmlspecialchars to output a URL into HTML.

The onclick event of a button does not take the user to the new page, it executes Javascript inside it. You either need to encapsulate your button inside an <a> tag and set the link there, or write some javascript to redirect the browser inside your onclick event.

Your search bar example looks like it is part of a <form> element which could be the only reason that it works, but that button also has the same problem:

You're trying to execute a URL as pure Javascript

Here are some other alternatives to make links that look like buttons: Stack Overflow

Community
  • 1
  • 1
0

Your last example may work, removing the parameter from the form action and adding an hidden input field:

<form action="<?php echo htmlspecialchars("book.php");?>" method="GET">
    <input name="id" type="hidden" value="<?php echo $resultsid; ?>" />
    <button type='submit' style="vertical-align:middle" id='infoButton' />
            <span>Meer info</span>
    </button>
</form>

These answers explain why the part after "?" gets ignored in your last example:

https://stackoverflow.com/a/1116026/3052648

https://stackoverflow.com/a/9882750/3052648

Note that you didn't specify a method in your form, and it defaults to GET.

Community
  • 1
  • 1
MazzCris
  • 1,812
  • 1
  • 14
  • 20