Redirect not working combining PHP with JS

I am trying with a combination of JS and PHP

To redirect the user to a new page.

I have the following code:

<button type=“submit” class=“btn btn-primary” onclick=“location.href=‘my_thoughts.php?user=<?php echo $user_id_present?>’;”

However it is not redirecting any ideas why.

I am able to confirm that the variable $user_id_present is getting it’s right information.

What is wrong with my syntax?

Right click on the button > inspect. Lookup if the variable is filled.

Press F12 and go the network tab, lookup if the URLs called are correct.

At least i think you have to disable the default behaviour of the button, otherwise you would make a form submit.

Why are you using a button to redirect like that? Just use a link and style it to look like a button.

That sounds like a good idea.

How can I change the default behavior of the submit button?

I was able to find a solution to my problem here’s the snippet of code I use.

<a href= "my_thoughts.php?user=<?php echo $id?>" class="btn btn-primary" role="button">View your thoughts</a>

Thank you for your help

ARIA is for accessibility, as the button role. CSS stands for Cascading Style Sheets. Styling a link to look like a button is a much better idea. Keep buttons for button work and hyperlink references for related text.

https://css-tricks.com/css-basics-styling-links-like-boss/

If php file, you can use this:

echo '<article>
		<form method="post" action="new_page.html"><p>This will redirect you to a new page. Click: <button class="" type="submit">Go</button></p></form></article>';

If html file, use

    <article>
		<form method="post" action="new_page.html">
			<p>This will redirect you to a new page. Click: <button class="" type="submit">Go
			</button>
			</p>
		</form>
	</article>

I prefer this method to <a href: >( hyperlink) which is best if on same page and text oriented page e.g. news, blogs, etc.

That is horrible semantically.

I dont undertsand what you mean by the word, “horrible semantically”. I am using HTML5. Guide is <section><header></header><article><header></header> ....... </article></section>
<input>, <button>, etc can be wrapped inside <p>. Please check or enlighten me further. Thank you

It is using the form specifically for redirection with a button. If you just want to send the user somewhere else, use an anchor link and style it as a button. A form is to take in data, not to redirect, to the point now that redirecting to process a form is bad practice.

1 Like

Hello colleagues, i’m sure that my opinion doesn’t matter much to anyone but i also think that the button is overkill. I use a ‘redirect’ concept on my login page if a token is expired: I show the error and follow the error message with a hyperlink reference named Refresh, which simply redirects the user back to the login to 1. clear the error, 2. get a new token: <a href="/login/">Refresh</a>

if you are trying to use a redirect based upon user_id, which is a logged in session member, then why not use a session variable to detect the user? then still use a hyperlink with a path starting from root?

Sponsor our Newsletter | Privacy Policy | Terms of Service