PHP : How to redirect to the same page while retaining data from another page
When you submit the form, the $_GET['gameID']
is lost. Change your form URL to include the gameID
.
Example:
<form action="/action_page.php?gameID=<%= $_GET['gameID'] %>">
...
However as @Ajeenckya already said. You should use something like the session. By using sessions, you don't have to pass the parameters all the time.
When i click the url its redirecting to the same page, i want its to redirect to the link
You might want to replace the href='#'
attribute with url.
Replace the line before the comment with this line
$("#news").append("<a href='" + url +"' id = 'url'>" + url + "</a>");
Hope this works!
How do I redirect to same page
When you submit a form, a submit
event is called. That event contains some information and actions that are stored in an SubmitEvent
object. The object is available when you listen for the event through the onsubmit
attribute, onsubmit
property or through addEventListener
.
I would highly recommend to use addEventListener
as the attribute version is just an old technology that does not want to seem to die and has weird side effects that addEventListener
has not.
When you don't want a form to submit while submitting, call the preventDefault()
method on the event
object. That will make sure that your form will not send and not reload the page when you want it to.
var form = document.getElementById('insert_form');
function onSubmit(event) {
var x = event.target.elements["id"].value;
if (isNaN(x)) {
alert("Please check ID!");
event.preventDefault();
return false;
}
var check = confirm("Are You Confirm To Submit?");
if (check == true) {
console.log("yes");
} else {
event.preventDefault();
}
}
form.addEventListener('submit', onSubmit);
<form name="table_field" id="insert_form" method="post" action="apply.inc.php">
<hr>
<h1 class="text-center">Overtime Table</h1>
<hr>
<table class="table table-bordered" id="myTable">
<tr>
<th>ID</th>
<th>Full Name</th>
</tr>
<tr>
<td><input class="form-control" name="id[]" type="text" id="id" required></td>
<td><input class="form-control" name="name[]" type="text" id="name" required></td>
</tr>
</table>
<b>Send To:</b>
<select class="form-control" name="options">
<option value="-----" id="-----" hidden>-----</option>
<option value="Table_A">Table A</option>
<option value="Table_B">Table B</option>
</select>
<input type="submit" name="save" id="save" value="Submit">
</form>
Redirect user to same page after login in a specific page
Try this..
In your authenticated method, check the previous request url is equal to route('congresses.registration'). If yes redirect to route('congresses.registration') with parameters.
LoginController:
use Illuminate\Support\Facades\Route;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated(Request $request, $user)
{
//check if the previous page route name is 'congresses.registration'
if(Route::getRoutes()->match(Request::create(\URL::previous()))->getName() == 'congresses.registration') {
//redirect to previous page with parameters
return redirect(Request::create(\URL::previous())->getRequestUri());
}
return redirect()->intended($this->redirectTo);
}
}
Hope it helps.. Let me know the results..
Reference here
How to redirect to the same page in PHP
There are a number of different $_SERVER
(docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']
:
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);
where $location
is the path after the domain, starting with /
.
Make a link redirect to the same page
To make your custom PHP blog post template be able to display a title link that points back to the page, one way is to make use of your $row[...
. variable.
Provided that,
- your URLS will look like your screenshots, such as
http://localhost/viewpost.php?id=8
when running locally and for examplehttp://www.yourwebsite.com/viewpost.php?id=8
when online - you know how to refer to the post's id that is used in the
...viewpost.php?id=8
, for example$row['postID']
- you don't yet have any variable or means to refer to your current domain
http://localhost
when local orhttp://www.yourwebsite.com
when online
Then, I recommend a two-part approach:
Somewhere at the top of your code, or perhaps in an include you might use for such code-reuse purposes, define for example $host
:
$host='http://' . $_SERVER['SERVER_NAME'];
Then, for your actual title link::
echo '<a href="' . $host . '/viewpost.php?id=' . $row['postID'] . '"> <h1 class="entry-title">' . $row['postTitle'] . '</h1></a>';
Explanation
- Separated HTML from the concatenation dots
.
with spaces, to be easier to read, as well as to support any helper programs such asfmt
that you might use for wrapping long lines, so they have spaces to use for wrapping lines. - Uses PHP's predefined
$_SERVER
variable'sSERVER_NAME
, which, combined with thehttp://
, the$host
will behttp://localhost
when local andhttp://www.yourwebsite.com
when online. - Define
$host
as a variable once at the top of the page, because it is clearer that way and likely you will have a use for it elsewhere on the page, so this helps avoid having to repeat yourself writing'http://'.$_SERVER['SERVER_NAME']
everywhere else you may need to start forming the absolute URL $host
is then combined with the pattern for the rest of the URL, to assemble the absolute URL- Absolute URL is helpful so that if a user saves your article to their computer, then later clicks the title link on their locally saved article, the user can still correctly reach the original online page
- As the article author, setting a link this way also means it can serve as a permalink, which helps with search engine optimization (SEO)
Related Topics
Sending Post Requests Without Waiting for Response
Validating Base64 Encoded Images
How to Get Only Date from Datetime in Codeigniter
How to Check, If a PHP String Contains Only English Letters and Digits
Regex Optionally Match a Pattern Multiple Times
Api to Get All the Reviews and Rating from Google for Business
What Is the Easiest Way to Check If Time Is Set in Datetime Variable
A Way to Determine Browser Width in PHP Without JavaScript
How to Get Last Insert Id in Sqlite
How to Use Two Foreach to Insert Data in Database
How to Add Dynamic Filter Using PHP MySQL Ajax
Get Selected Option in PHP Without Pressing Submit
How to Update Field to Add Value to Existing Value
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
How to Install Imagick/Imagemagick PHP Extension on Windows 7
Dompdf Not Rendering Images from the Server But Is Rendering from External Source