سؤال

<input type='submit' name='submit' value='Register' class='register' />

how do I make this link to a website on click?

هل كانت مفيدة؟

المحلول

Here's one way of doing it with your present code (submit-type button) using PHP's header() function.

(handler.php)

<?php
if(isset($_POST['submit'])){
header("Location: http://www.example.com/page.php");
exit;
}

And I'm assuming with the code you have in your question, resembling something to the affect of:

<form action="handler.php" method="post">
Username: 
<input type='text' name='username' />
<input type='submit' name='submit' value='Register' class='register' />
</form>

Of course I didn't include the possible $username=$_POST['username']; that could be in your PHP, depending on how you will be using it.


EDIT

Upon reading mplungjan's comment have made a slight name change. I've yet to know why using the name submit is considered unsafe, after trying to find the (or a) reason why on Google. I'm hoping to get or find an answer to this affect.

(Edit-findings) See further information below that I found to date.

(handler.php)

<?php
if(isset($_POST['reg_button'])){
header("Location: http://www.example.com/page.php");
exit;
}

And I'm assuming with the code you have in your question, resembling something to the affect of:

<form action="handler.php" method="post">
Username: 
<input type='text' name='username' />
<input type='submit' name='reg_button' value='Register' class='register' />
</form>

Findings:

Article(s) I've come across on the subject that mplungjan mentioned in comments:


If you're going to use a PHP (server-side) method, consider using the following, as borrowed from this website's article on Cross site scripting.

<input name="foo" value="<?php print htmlspecialchars($foo); ?>">

and in your case:

<input type='submit' name='reg_button' value='<?php print htmlspecialchars($reg_button); ?>' class='register' />

Borrowed from mplungjan's comment:

1) never call a submit button name="submit"
2) use a link or a button <input type="button" onclick="location='somepage.html'" />
3) Just use name="Submit" or submitted and problems will be avoided.

(Thanks for the extra input mplungjan).

نصائح أخرى

You have to understand the default behavior of the tag you're using. The submit input tag, sends the user to the form action. I'm not sure what you're trying to do, so I'm not sure if you're even using the right tag. Perhaps consider anchor tags?

My best guess, given the vague question is:

<form action="{URL}" method="post">
  <input type='submit' name='submit' value='Register' class='register' />
</form>

or

<a href="{URL}">Register</a>
<input type="button" onclick="window.location='YourUrlHere'" class="register" value="Register"/>

You can use anchor tag for this problem, An anchor tag is used to redirect from one page to another page by just one click. Here you can use this as follow:

 <a href="url"><input type='submit' name='submit' value='Register' class='register' /></a>

Note: href is the tag which contains the path of your desired destination. That's it, Keep coding... :)

You can go for a <a href="{URL}">Register</a> as just said or if you want you can also use the button tag <button onclick="myFunction()">Click me</button> where myFunction is your JavaScript code to an other page

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top