Php button onclick redirect to another page

I tried with this code but it's not working,

<input type="button" value="Home" class="homebutton" id="btnHome" onClick="<?php header("Location: /index.php"); ?>" />

Php button onclick redirect to another page

asked Dec 25, 2013 at 6:10

Php button onclick redirect to another page

1

You can't use php code client-side. You need to use javascript.

<input type="button" value="Home" class="homebutton" id="btnHome" 
onClick="document.location.href='some/page'" />

However, you really shouldn't be using inline js (like onclick here). Study about this here: https://www.google.com/search?q=Why+is+inline+js+bad%3F

Here's a clean way of doing this: Live demo (click).

Markup:

<button id="myBtn">Redirect</button>

JavaScript:

var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
  document.location.href = 'some/page';
});

If you need to write in the location with php:

  <button id="myBtn">Redirect</button>
  <script>
    var btn = document.getElementById('myBtn');
    btn.addEventListener('click', function() {
      document.location.href = '<?php echo $page; ?>';
    });
  </script>

answered Dec 25, 2013 at 6:12

m59m59

42.6k14 gold badges115 silver badges134 bronze badges

Why do people keep confusing php and javascript?

PHP is SERVER SIDE
JAVASCRIPT (like onclick) is CLIENT SIDE

You will have to use just javascript to redirect. Otherwise if you want PHP involved you can use an AJAX call to log a hit or whatever you like to send back a URL or additional detail.

answered Dec 25, 2013 at 6:13

JakubJakub

20.3k8 gold badges64 silver badges92 bronze badges

Please try this

    <input type="button" value="Home" class="homebutton" id="btnHome" onClick="Javascript:window.location.href = 'http://www.website.com/index.php';" />

window.location.href example:

 window.location.href = 'http://www.google.com'; //Will take you to Google.

window.open() example:

     window.open('http://www.google.com'); //This will open Google in a new window.

answered Dec 25, 2013 at 6:16

Php button onclick redirect to another page

Sudheesh.RSudheesh.R

3362 silver badges8 bronze badges

you are using onclick which is javascript event. there is two ways

Javascript

<input type="button" value="Home" class="homebutton" id="btnHome" 
onClick="window.location = 'http://google.com'" />

Or PHP

create another page as redirect.php and put

<?php header('location : google.com') ?>

and insert this link on any page within the same directory

<a href="redirect.php">google<a/>

hope this helps its simplest!!

Php button onclick redirect to another page

answered Dec 25, 2013 at 6:16

NullNull

1541 silver badge14 bronze badges

Php button onclick redirect to another page

Problem
I build some kind of interactive menu using PHP, HTML and JavaScript. I need redirect the page on click to some URL. Itried the following code, but it does not work:

" />
What is wrong with my code?

Solution
The root cause of the issue is, that you are trying to call PHP (server-side) code from JavaScript (the client-side). When JavaScript is acting – PHP (the server-side code) does not exist. It can be called during the server-side page rendering page only. Getting back to the original problem code sample, the statement:

<?php header("Location: /start.php");
will cause a JavaScript error, since it is not a valid JavaScript. What you can do? In case the “Start” button should always redirect to start.php you can redirect the browser to the static URL, like that:

Markup

Start
JavaScript

var btn = document.getElementById('btnStart');
btn.addEventListener('click', function() {
document.location.href = 'start.php';
});
Another approach will be to use inline JavaScript

Start
Incase the URL is dynamic – it is up to server to decide what should be the redirect URL, the inline approach will be easier to implement:

’”>Start
This technique called inline PHP, the server will generate the output, the browser will receive the following line:

Start
You can use the inline PHP in the JavaScript section (the first example) as well:

Markup

Start
JavaScript

var btn = document.getElementById('btnStart');
btn.addEventListener('click', function() {
document.location.href = '<?php echo "Stasrt.php"?>';
});
Tip:
There is a short cut for <?php echo … , you can use <?=’text’?> instead. For example:

'">Start
So, there are several ways to redirect pages from the client-side, choose the one you like more.

How do I make a button redirect to another page?

To make button type submit redirect to another page, You can use HTML Anchor tags <a>. Where you need to write your HTML Button [button type submit] between these HTML Anchor Tag's starting <a> and Closing </a> Tags. or you can use HTML Form Tags to do the Same thing.
Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How do I redirect a clicked button?

How to Redirect to Another Page in JavaScript on button click.
document. getElementById("myButton"). onclick = function () { location. href = "example.com"; };.
function redirect() { let url = "http://example.com"; window. location(url); }.
myFun(){ $('form'). attr('action','new path'); }.
To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page. The <head> section of an HTML document contains metadata that is useful for the browser, but invisible to users viewing the page.